I have two tables in my database. TableOne has theKey primary key. TableTwo has a foreign key theKey that refers to the corresponding column in the TableOne. Now I am trying to update theKey in TableTwo:
UPDATE TableTwo
SET theKey = NewIk
FROM TableTwo W
JOIN TableThree D ON W.theKey_backup = D.OldIk
WHERE dt >= '2012-05-01'
But I receive the following error:
The UPDATE statement conflicted with the FOREIGN KEY constraint
“FK_TableTwo_TableOne”. The conflict occurred in database “MyDB”,
table “dbo.TableOne”, column ‘theKey’.
Could you please explain why I get this error if I don’t touch the TableOne?
Thanks.
You’re trying to set
TableTwo.theKeyto a value that doesn’t exist in the referenced table,TableOne– that’s what the error is saying.So you need to check why this is happening – where are you selecting that new value from? Why is it a value that doesn’t (yet) exist in
TableOne?