Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql error on update : The UPDATE statement conflicted with the FOREIGN KEY constraint

Tags:

sql-server

I have a table called patient_address, which reference a PK key in patient table. But if I try to run one of the following statements :

update patient set id_no='7008255601088' where id_no='8008255601089' update patient_address set id_no='7008255601088' where id_no='8008255601089' 

I get this error message:

"The UPDATE statement conflicted with the REFERENCE constraint "FK__patient_a__id_no__27C3E46E". The conflict occurred in database "PMS", table "dbo.patient_address", column 'id_no'." or "The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__patient_a__id_no__27C3E46E". The conflict occurred in database "PMS", table "dbo.patient", column 'id_no'." .

Does any body know the possible cause ? Thanks.

like image 378
chosenOne Thabs Avatar asked May 25 '14 14:05

chosenOne Thabs


People also ask

How do you fix the update statement conflicted with the foreign key constraint?

Another solution can be, disable the Foreign Key constraint, update the records and finally enable the Foreign key again. I used below statement to disable Foreign Key constraint on dbo. Orders table. I execute below script to Enable Foreign Key Constraint on dbo.

How do you update a table that has a foreign key?

Login to the SQL Server using SQL Server Management Studio, Navigate to the Keys folder in the child table. Right click on the Keys folder and select New Foreign Key. Edit table and columns specification by clicking … as shown in the below image. Select the parent table and the primary key column in the parent table.

What is foreign key conflict error?

That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first. Follow this answer to receive notifications.

How do I delete a foreign key constraint in SQL?

To delete a foreign key constraint In Object Explorer, expand the table with the constraint and then expand Keys. Right-click the constraint and then click Delete.


1 Answers

This error is encountered when the primary key of a table is updated but it is referenced by a foreign key from another table and the update specific is set to No action. The No action is the default option.

If this is your case and No action is set on the update operation you can change the foreign-key definition to Cascade.

Right click your foreign key and select Modify. In the Foreign key relationships dialog under the INSERT and UPDATE specifics set the UPDATE rule on Cascade:

enter image description here

You can also set the rule using T-SQL:

ALTER TABLE YourTable DROP Constraint Your_FK GO  ALTER TABLE YourTable ADD CONSTRAINT [New_FK_Constraint] FOREIGN KEY (YourColumn) REFERENCES ReferencedTable(YourColumn) ON DELETE CASCADE ON UPDATE CASCADE GO  

Hope this helps

like image 120
Milica Medic Kiralj Avatar answered Sep 17 '22 17:09

Milica Medic Kiralj