referential-integrity

Is it possible to defer referential integrity checks until the end of a transaction in SQL Server?

末鹿安然 提交于 2019-11-30 21:40:09
I recently read in Fowler's PofEA that some database engines allow you to defer RI checks until the end of the transaction. Is this possible in SQL Server? The problem comes with some scenarios where I am trying to save changes that include insertions and deletions, it can be tricky to determine the correct order in which to perform the operations to avoid RI failures. I know that at the end of the transaction the RI will be good so it would seem ideal to defer these checks. Looks like sql server doesn't allow this, but you aren't the only one who wants it. It's part of the SQL 92 standard, so

Referential Integrity Constraint violation when attempting to set a FK to null

ぃ、小莉子 提交于 2019-11-30 19:25:08
I am trying to update an entity in EF6. I have read that if I wish to change a ForeignKey property, I have to then ensure the Navigation Property is the correct one, or set it to null. I have taken the set to null approach, but I still receive the Referential Integrity Constraint Exception: A referential integrity constraint violation occurred: The property value(s) of 'Contact.idContact' on one end of a relationship do not match the property value(s) of 'Entity.id_EntityContactInfo' on the other end. But you can see in the debugger, that Entity.Contact is null, so I believe this shouldn't be

SET CONSTRAINTS ALL DEFERRED not working as expected in PostgreSQL 9.3

不打扰是莪最后的温柔 提交于 2019-11-30 17:47:20
If I define tables a and b as follows: CREATE TABLE a(i integer); ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i); CREATE TABLE b(j integer); ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE; INSERT INTO a(i) VALUES(1); And then do the following: START TRANSACTION; SET CONSTRAINTS ALL DEFERRED; INSERT INTO b(j) VALUES(2); INSERT INTO a(i) VALUES(2); COMMIT; It produces the error below. Why is SET CONSTRAINTS not having the desired effect? ERROR: insert or update on table "b" violates foreign key constraint "fkey_ij" SQL

mysql Multiple Foreign Keys in a Table to the Same Primary Key

亡梦爱人 提交于 2019-11-30 07:05:26
I have a table user with userID as the primary key. I have another table called Friends . In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user . I want to enforce data integrity. Could I use something like this? ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`) REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE; I want to know is REFERENCES users ( userId , userId ) referencing a column multiple times correctly? The reason I am not creating 2 separate

Is it possible to defer referential integrity checks until the end of a transaction in SQL Server?

末鹿安然 提交于 2019-11-30 05:42:12
问题 I recently read in Fowler's PofEA that some database engines allow you to defer RI checks until the end of the transaction. Is this possible in SQL Server? The problem comes with some scenarios where I am trying to save changes that include insertions and deletions, it can be tricky to determine the correct order in which to perform the operations to avoid RI failures. I know that at the end of the transaction the RI will be good so it would seem ideal to defer these checks. 回答1: Looks like

SET CONSTRAINTS ALL DEFERRED not working as expected in PostgreSQL 9.3

*爱你&永不变心* 提交于 2019-11-30 01:44:45
问题 If I define tables a and b as follows: CREATE TABLE a(i integer); ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i); CREATE TABLE b(j integer); ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE; INSERT INTO a(i) VALUES(1); And then do the following: START TRANSACTION; SET CONSTRAINTS ALL DEFERRED; INSERT INTO b(j) VALUES(2); INSERT INTO a(i) VALUES(2); COMMIT; It produces the error below. Why is SET CONSTRAINTS not having

How to define @OneToMany in parent entity when child has composite PK?

☆樱花仙子☆ 提交于 2019-11-29 16:48:45
My Parent class has two child classes: Child and ParentHobby . The Child class has a singular PK and the @OneToMany mapping on it works. The problem is that I don't know how to map it on the ParentHobby class, which has a composite PK. Parent: //this works @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER) private List<Child> childList; //this DOES NOT work @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER) private List<ParentHobby> hobbyList; Child: @Entity @Table(name="CHILD") public class Child { @Id @SequenceGenerator(name=

SQL Server: how to know if any row is referencing the row to delete

随声附和 提交于 2019-11-29 06:34:10
问题 You cannot delete a row if any row is referencing the row to delete via a FK. Is it possible to know if any row is referencing the row to delete before executing a DELETE statement? 回答1: This script will show all the tables that have rows that reference the row you are trying to delete: declare @RowId int = 1 declare @TableName sysname = 'ParentTable' declare @Command varchar(max) select @Command = isnull(@Command + ' union all ', '') + 'select ''' + object_name(parent_object_id) + ''' where

mysql circular dependency in foreign key constraints

让人想犯罪 __ 提交于 2019-11-29 05:46:44
Given the schema: What I need is having every user_identities.belongs_to reference an users.id . At the same time, every users has a primary_identity as shown in the picture. However when I try to add this reference with ON DELETE NO ACTION ON UPDATE NO ACTION , MySQL says #1452 - Cannot add or update a child row: a foreign key constraint fails ( yap . #sql-a3b_1bf , CONSTRAINT #sql-a3b_1bf_ibfk_1 FOREIGN KEY ( belongs_to ) REFERENCES users ( id ) ON DELETE NO ACTION ON UPDATE NO ACTION) I suspect this is due to the circular dependency, but how could I solve it ( and maintain referential

How to deal with mutually recursive inserts

江枫思渺然 提交于 2019-11-29 02:26:42
I have a model that defines mutually recursive tables: Answer questionId QuestionId text Question text correct AnswerId What do I need to do to actually insert a question? I need to know what the correct answer is first. But to insert an answer, I need to know what question it answers. I'm running Postgres, if it matters. The DDL is: CREATE TABLE answer ( id integer NOT NULL, -- answer id text character varying NOT NULL, -- answer text question_id bigint NOT NULL -- question id ); CREATE TABLE question ( id integer NOT NULL, -- question id question character varying NOT NULL, -- question text