How can I change the constraints of FOREIGN KEY in code first mode? - sql

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

Related

Linking Two Foreign Keys to one Primary Key

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

I added a foreign key to an existing database and when I tried to publish it this error came up

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?

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.

Foreign Key constraint issue

What could possibly caused a FK error? I'm inserting an 'Activity' record into a database that has a 'StaffId' field on (FK with Staff table), I've looked for the staffId in question (no white spaces etc.) and it DOES exist. What else can cause an error with a foreign key field?
EDIT: Error:
The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_Activities_Staff". The conflict occurred in database
"DataWarehouseB", table "dbo.Staff", column 'StaffId'. The statement
has been terminated.
SQL Foreign Key documentation says
If the database schema contains foreign key errors that require
looking at more than one table definition to identify, then those
errors are not detected when the tables are created. Instead, such
errors prevent the application from preparing SQL statements that
modify the content of the child or parent tables in ways that use the
foreign keys. Errors reported when content is changed are "DML errors"
and errors reported when the schema is changed are "DDL errors". So,
in other words, misconfigured foreign key constraints that require
looking at both the child and parent are DML errors. The English
language error message for foreign key DML errors is usually "foreign
key mismatch" but can also be "no such table" if the parent table does
not exist. Foreign key DML errors are may be reported if:
The parent table does not exist.
The parent key columns named in the foreign key constraint do not exist.
The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a
unique constraint using collating sequence specified in the CREATE
TABLE.
The child table references the primary key of the parent without specifying the primary key columns and the number of primary key
columns in the parent do not match the number of child key columns.
Some DBs might also support using a non-unique index as a foreign key reference,

Changing a record in a table (sql server) that has foreign keys?

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).