Had an encounter wherein I need to update the tables but I was not able to do it because of the foreign key constraints. Good thing was that I found this code and it solved my problem.
What this code does what to temporarily remove the checking of the foreign key so you can change the data and then set it back again.
I hope this helps!
Happy coding!!
Source: UPDATE statement conflicted with the REFERENCE constraint by Manoj Pandey
-- Disable FK Constraint
ALTER TABLE emp NOCHECK CONSTRAINT fk_dept_d_fk
-- Perform UPDATE
UPDATE dept SET d_id=3 WHERE d_id=2
UPDATE emp SET d_fk=3 WHERE d_fk=2
-- Enable FK Constraint
ALTER TABLE emp WITH CHECK CHECK CONSTRAINT fk_dept_d_fk
-- Following SELECTS will show the new values in updated records:
SELECT * from dept
What this code does what to temporarily remove the checking of the foreign key so you can change the data and then set it back again.
I hope this helps!
Happy coding!!
Source: UPDATE statement conflicted with the REFERENCE constraint by Manoj Pandey
No comments:
Post a Comment