I have two tables, a Photographer and an Influences table. In the Influences table, a Photographer is able to influence another Photographer.
Relational Schema:
Here is my Diagram in SQL:
Instance Example:
My issue is when I try to delete Photographer Tom, since he influences Jason (as shown in Influences table), I get a error stating:
The DELETE statement conflicted with the REFERENCE constraint "FK_Influences_Photographer1". The conflict occurred in database "jma59", table "dbo.Influences"
If Tom was in the column EPName of the Influences table, I have no issue deleting it. I know that this is a foreign key issue but I am not sure how to handle this situation. I created two separate foreign keys that reference to the Photographer primary key. But the problem is that I cannot make it so that both foreign keys cascade on update and delete.
First foreign key is EPName and EPBDate referencing to PName and PBDate of Photographer
Second foreign key is RPName and RPBDate referning to PName and PBDate of Photographer as well.
The error is:
Unable to create relationship 'FK_Influences_Photographer1'. Introducing FOREIGN KEY constraint 'FK_Influences_Photographer1' on table 'Influences' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Any advice is greatly appreciated!
You could consider using a Trigger rather than a cascading delete to enforce referential integrity: https://support.microsoft.com/en-gb/help/321843/error-message-1785-occurs-when-you-create-a-foreign-key-constraint-tha
Related
my error :
Introducing FOREIGN KEY constraint 'FK_Table_ArticleViolations_Table_Users_vUser_ID' on table 'Table_ArticleViolations' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
I need to use an PK in a few other tables as FK
How can I change the constraints of FOREIGN KEY in code first mode ?
Thank you
As you can see in the picture, I have the tables. The mistake I got is as follows: From the airport table, the primary key should go to both fields in the flight table. I get an error when I do this to both the stay airport and the landing airport. How do I solve this?
Error: - Unable to create relationship 'FK_a.ucus_a.havalimanlari1'.
Introducing FOREIGN KEY constraint 'FK_a.ucus_a.havalimanlari1' on table 'a.ucus' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
An error occurred while the batch was being processed
Foreign key should always follow referential integrity constraint. Referential integrity constraint states that whenever adding value to foreign key column in child table, it should have equivalent primary key value in parent table.
Here in your case, as you said, you added a foreign key to an existing database table, so it may be possible that values in that table are violating referential integrity constraint, which is producing error.
Is it possible to have both foreign key constraint and a trigger that keep the referential integrity?
I don't think that is possible and we have to remove the constraint and keep the referential integrity using trigger. To extend my description, suppose you have two tables one of them has a primary key and the other has a foreign key referring to the first table, primary key column. So now is it possible to add the following code
create or replace TRIGGER TABLE1_TABLE2
after update of TABLE1_PRIMARY_KEY on TABLE1
Of course it came to my mind to lose the constraint and continue with the trigger but is it the only way?
By the way this code is just for practice, I know it isn't a good idea to update the primary key of a table.
I found a useful article on one of Microsoft websites confirming my idea that the constraints should be removed. But of course the solution for Oracle may be different from any other database. The article is about Implementing Referential Integrity and Cascading Actions and the part that is related is "Implementing ON DELETE CASCADE and ON UPDATE CASCADE with triggers" and after that comes "Before continuing, you need to drop the existing foreign keys".
There is no problem doing that in Oracle. You could have both the constraint and the trigger at the same time.
Does anyone know if there is a quicker way of editing a record that has foreign keys in a table (in sql server).. i will explain.. i have approx 5 tables that have there own ID but are linked together using a foreign key...
Hence i needed to change the foreign key (the contract number in my case), but i had to copy each record to a new record and edit it that way...
As if i try to edit the contract number it gives me the standard error of being associated and violates a foreign key etc
Surly there must be a better way?
ANy ideas?
are you talking about changing the PK and then updating all the Fks? In that case enable cascade updates and this will be done automagically
same with deletes, you enable cascade deletes
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted. If cascading referential actions have also been defined on the target tables, the specified cascading actions are also taken for the rows deleted from those tables.
ON UPDATE CASCADE
Specifies that if an attempt is made to update a key value in a row, where the key value is referenced by foreign keys in existing rows in other tables, all of the foreign key values are also updated to the new value specified for the key. If cascading referential actions
I'm not an SQL expert, but can't you set something like ON UPDATE CASCADE to automatically update the foreign key when the primary key is changed?
Or try disabling the integrity constraint, do your changes and attempt to re-enable the constraint. Basically, if you didn't do it right you will get an error then (can't enable a constraint that would be violated).