开发者

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 开发者_Python百科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?


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


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.


Updating a primary key does not update related foreign keys, it only deletes the related records on other tables as Sql Server treats update as delete and insert. This is Sql Server 2000, not sure later versions. Using "on cascading update on cascading delete", the cascading effects of the "delete and insert:aka update", deletes the related records on other tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜