You are working as SQL Server developer, You wrote an update statement for one of the table and getting below error. Msg 547, Level 16, State 0, Line 32 The UPDATE statement conflicted with the REFERENCE constraint "FK_". The conflict occurred in database "YourDatabaseName", table "SchemaName.YourTableName", column 'ColumnName'. The statement has been terminated. How to resolve this issue? Solution:Let's create this error first by using below script. We are going to create two tables dbo.Customer and dbo.Orders. The tables has Primary-Foreign Key Relationship. USE YourDatabaseName GO CREATE TABLE dbo.Customer (
How to update record when Column is referenced by Foreign Key Constraint in SQL ServerNow let's say if you feel that CustomerId value is incorrect in dbo.Customer and need to be updated. You wrote below update statement to update CustomerId to 100.
You will get below error.Msg 547, Level 16, State 0, Line 33 The UPDATE statement conflicted with the REFERENCE constraint "FK__Orders__Customer__1ED998B2". The conflict occurred in database "YourDatabaseName", table "dbo.Orders", column 'Customer_id'. The statement has been terminated. As there is no Customer_id value=100 in dbo.Orders table, You can't update the record in reference table. Now you thought that let's fix the Parent table first ( dbo.Orders) and then I can update the dbo.Customer table.
Again you got the error as shown below, because we don't have CustomerId=100 available in dbo.Customer table.Msg 547, Level 16, State 0, Line 36 The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__Orders__Customer__1ED998B2". The conflict occurred in database "YourDatabaseName", table "dbo.Customer", column 'Customerid'. The statement has been terminated. From here we can come with with multiple solutions
Check the records in table now.How to update Column Value when referenced by Foreign Key Constraint in SQL Server
Another solution can be, disable the Foreign Key constraint, update the records and finally enable the Foreign key again. You don't need to use the on delete cascade. Somebody (the schema design author) had made sure you cannot delete a person that is still referenced by an article. It succeeded, you were just trying to do this and was blocked, kudos to the designer. Now go and talk with that somebody that designed the schema and knows the constraints and ask him how to properly delete the records you're trying to delete, in the correct order and taking the proper precautions to keep the database consistent. answered Apr 4, 2013 at 12:02 Remus RusanuRemus Rusanu 51.7k3 gold badges94 silver badges172 bronze badges You have two real choices here, you can disable constraints on the table. This usually not a great idea as you can end up with a bad data condition if you're messing with data that relates to other tables, but not know the full extent of your schema and it may suit your purposes:
Remember to turn the constraint back on after the delete with
The second choice would be to drop and re-add the constraint with the ON DELETE CASCADE option using:
Based on your FK name it looks like your parent table is M02ArticlePersons and the parent column is M06Persons. If you did not author this schema please try to consider why the constraints may be present, and understand that violating them in this manner may have unintended side effects. answered Apr 4, 2013 at 12:10 0 dbo.M02ArticlePersons table of column M06PersonId is reffered in another table. So before delete statement, disable this relationships and try again below is for disbling the foreign key
and this is to enable it
Hope this will work answered Apr 4, 2013 at 12:04 1 There is another manual option too: You can go to the child table and delete the child rows referenced by the parent key. Then you can delete the parent row. This is essentially what the cascade delete does. This way, you do not have to drop/recreate/alter your constraints. answered Apr 4, 2013 at 14:49 StanleyJohnsStanleyJohns 5,9622 gold badges21 silver badges44 bronze badges This little code will help for any table that you want to delete records from. It takes care of referential integrity as well ... |