即使使用约束也更新

Update even though using constraints

我正在尝试更新 SQL Server 2014 数据库中的值(小拼写错误)。

我喜欢这样:

update t_Table set funID = 'References' where funID = 'Referencies'

执行此操作时出现错误

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_t_Table_Language_t_Table". The conflict occurred in database "db", table "dbo.t_Table".

如果更新外键,我会得到类似的错误。

有什么方法可以同时更新所有的值吗? 我模糊地记得有人在管理工作室展示了一种方法,但我不记得是怎么做到的。

在外键上启用更新级联

ALTER TABLE t_Table_Language DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table_Language ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table(funID)
ON UPDATE CASCADE

编辑: 或者反过来,我不确定哪个 table 有 foreing key

ALTER TABLE t_Table DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table_Language(funID)
ON UPDATE CASCADE

然后对引用的table(主table)进行更新。

UPDATE t_Table_Language 
SET funID = 'References'
WHERE funID = 'Referencies'