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

99封情书 提交于 2019-12-04 05:10:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!