Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change value of primary key and update foreign key in the same time

I have a record in table with wrong primary key. I want change it to correct value, but this value is used in many other tables.

Is there any simple way to update primary key and foreign key at the same tim?

like image 554
user278618 Avatar asked Jun 17 '11 15:06

user278618


2 Answers

If the foreign keys are set to cascade changes then the value should change automatically.

like image 133
Ray Avatar answered Sep 28 '22 19:09

Ray


Make sure that your foreign key relationships have ON UPDATE CASCADE specified, and the foreign key will automatically update to match the primary key.

From Books Online: http://msdn.microsoft.com/en-us/library/ms174123%28v=SQL.90%29.aspx

ON UPDATE {CASCADE | NO ACTION | SET DEFAULT | SET NULL}

Specifies what action happens to a row in the table that is created when that row has a referential relationship, and the referenced row is updated in the parent table. The default is NO ACTION. See the "Remarks" section later in this topic for more information.

like image 43
Michael Fredrickson Avatar answered Sep 28 '22 17:09

Michael Fredrickson