更改具有外键的表(sql server)中的记录? [英] Changing a record in a table (sql server) that has foreign keys?

查看:137
本文介绍了更改具有外键的表(sql server)中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如果有更快的方式编辑一个表中有外键(在sql server)的记录..我会解释..我有约5表有自己的ID,但链接在一起使用外键...



因此,我需要更改外键(我的情况下的合同号),但我不得不将每个记录复制到一个新的记录,以这种方式编辑...



如果我试图编辑合同号,它会给我标准的关联错误,违反外键等。



任何想法?

与删除一样,您可以启用级联删除



ON DELETE CASCADE



指定如果尝试使用外键在其他表的现有行中引用的键来删除行,则包含这些外键的所有行也将被删除。如果还在目标表上定义了级联引用操作,那么还会对从这些表中删除的行执行指定的级联操作。



ON UPDATE CASCADE



指定如果尝试更新行中的键值,其中键值由其他表中的现有行中的外键引用,则所有外键值也更新为为键指定的新值。如果级联参考操作


Does anyone know if there is a quicker way of editing a record that has foreign keys in a table (in sql server).. i will explain.. i have approx 5 tables that have there own ID but are linked together using a foreign key...

Hence i needed to change the foreign key (the contract number in my case), but i had to copy each record to a new record and edit it that way...

As if i try to edit the contract number it gives me the standard error of being associated and violates a foreign key etc

Surly there must be a better way?

ANy ideas?

解决方案

are you talking about changing the PK and then updating all the Fks? In that case enable cascade updates and this will be done automagically same with deletes, you enable cascade deletes

ON DELETE CASCADE

Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted. If cascading referential actions have also been defined on the target tables, the specified cascading actions are also taken for the rows deleted from those tables.

ON UPDATE CASCADE

Specifies that if an attempt is made to update a key value in a row, where the key value is referenced by foreign keys in existing rows in other tables, all of the foreign key values are also updated to the new value specified for the key. If cascading referential actions

这篇关于更改具有外键的表(sql server)中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆