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.
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.
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With