referential-integrity

Maintaining Referential Integrity - Good or Bad?

拈花ヽ惹草 提交于 2019-11-28 18:13:06
We are planning on introducing simple Audit Trail in our database using triggers and separate history table for each table that requires auditing. For example consider table StudentScore, it has few foreign keys (eg. StudentID, CourseID) linking it to corresponding parent tables (Student & Course). Table StudentScore ( StudentScoreID, -- PK StudentID ref Student(StudentID), -- FK to Student CourseID ref Course(CourseID), -- FK to Course ) If StudentScore requires auditing, we are planning to create audit table StudentScoreHistory - Table StudentScoreHistory ( StudentScoreHistoryID, -- PK

How do I disable referential integrity in Postgres 8.2?

喜欢而已 提交于 2019-11-28 16:41:41
Google results on this one are a bit thin, but suggest that it is not easily possible. My specific problem is that I need to renumber the IDs in two tables that are related to each other such that table B has an "table_a_id" column in it. I can't renumber table A first because then its children in B point to the old IDs. I can't renumber table B first because then they would point to the new IDs before they were created. Now repeat for three or four tables. I don't really want to have to fiddle around with individual relationships when I could just "start transaction; disable ref integrity;

Enforce maximum number of child rows

一世执手 提交于 2019-11-28 13:12:14
If a database has a pair of tables in a typical "parent and child" fashion, is there a way to enforce (without using triggers) that each parent can only have a maximum, say, of four children? So we have a Parents table: create table dbo.Parents ( ParentID char(2) not null primary key /* Yada Yada Yada */ ) And the Children table: create table dbo.Children ( ChildID char(2) not null primary key, ParentID char(2) not null references dbo.Parents (ParentID) /* usw */ ) And we populate the Parents table: insert into Parents (ParentID) values ('aa'),('bb') I want this insert to succeed: insert into

Foreign key vs check constraint for integrity

隐身守侯 提交于 2019-11-28 09:15:33
I am building a system that is a central repository for storing data from a number of other systems. A sync process is required to update the central repository when the other systems data is updated. There will be a sync_action table to identify which system the central repo needs to sync with and the type of sync required. There are set of defined actions that is very unlikely to change. A slimmed down system is below. As I see it I can approach this in two ways: Option 1 ) Have an Action table that has the 3 actions available. Have a sync_action table which uses a foreign key to reference

Why do Rails migrations define foreign keys in the application but not in the database?

邮差的信 提交于 2019-11-28 04:54:13
If I define a Customer and Order model in which a Customer "has many" Orders and the Order "belongs to" the Customer , in Rails we talk about Order having a foreign key to the Customer through customer_id but we don't mean that this is enforced in the database. Because Rails does not define this as a database-level constraint, there is a risk of your data's integrity being violated, perhaps outside the application (or inside if you receive simultaneous requests?), unless you enforce the constraint in the database manually. Why does Rails not define the foreign key at the database level or is

(doctrine2 + symfony2) cascading remove : integrity constraint violation 1451

扶醉桌前 提交于 2019-11-27 20:43:55
First, sorry for my poor English... I got four entities : User, Application, Bundle & Entity. Here are their relations (with cascading persist & remove, see code below) : User 1-n Application Application 1-n Bundle Bundle 1-n Entity It's working fine. But an User can have two of his entities as default, and I need to access them directly. So I add on User two fields, entity1 & entity2, with a 1-1 relation. And now my app crashes : An exception occurred while executing 'DELETE FROM bundle WHERE id = ?' with params {"1":13}: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or

How do I disable referential integrity in Postgres 8.2?

烂漫一生 提交于 2019-11-27 09:50:58
问题 Google results on this one are a bit thin, but suggest that it is not easily possible. My specific problem is that I need to renumber the IDs in two tables that are related to each other such that table B has an "table_a_id" column in it. I can't renumber table A first because then its children in B point to the old IDs. I can't renumber table B first because then they would point to the new IDs before they were created. Now repeat for three or four tables. I don't really want to have to

MySQL foreign key to allow NULL?

可紊 提交于 2019-11-27 08:59:57
I'm piecing together an image website. The basic schema's pretty simple MySQL, but I'm having some trouble trying to represent possible admin flags associated with an image ("inappropriate", "copyrighted", etc.). My current notion is as follows: tblImages ( imageID INT UNSIGNED NOT NULL AUTO_INCREMENT, ... ); tblImageFlags ( imageFlagID INT UNSIGNED NOT NULL AUTO_INCREMENT, imageID INT UNSIGNED NOT NULL, flagTypeID INT UNSIGNED NOT NULL, resolutionTypeID INT UNSIGNED NOT NULL, ... ); luResolutionTypes ( resolutionTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT, resolutionType VARCHAR(63) NOT NULL,

Delete parent if it's not referenced by any other child

我怕爱的太早我们不能终老 提交于 2019-11-27 08:58:20
I have an example situation: parent table has a column named id , referenced in child table as a foreign key. When deleting a child row, how to delete the parent as well if it's not referenced by any other child? In PostgreSQL 9.1 or later you can do this with a single statement using a data-modifying CTE . This is generally less error prone. It minimizes the time frame between the two DELETEs in which a race conditions could lead to surprising results with concurrent operations: WITH del_child AS ( DELETE FROM child WHERE child_id = 1 RETURNING parent_id, child_id ) DELETE FROM parent p USING

Enforce maximum number of child rows

馋奶兔 提交于 2019-11-27 07:31:33
问题 If a database has a pair of tables in a typical "parent and child" fashion, is there a way to enforce (without using triggers) that each parent can only have a maximum, say, of four children? So we have a Parents table: create table dbo.Parents ( ParentID char(2) not null primary key /* Yada Yada Yada */ ) And the Children table: create table dbo.Children ( ChildID char(2) not null primary key, ParentID char(2) not null references dbo.Parents (ParentID) /* usw */ ) And we populate the Parents