当我尝试在 SQL Server 中重新启用约束时,出现“ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束冲突”
我需要更新数据库表中的几条记录。这些表上几乎没有外键约束。
要禁用约束,我使用这个:-
ALTER TABLE Table_01 NOCHECK CONSTRAINT ALL
ALTER TABLE Table_02 NOCHECK CONSTRAINT ALL
ALTER TABLE Table_01 DISABLE TRIGGER ALL
要启用约束,我使用这个:-
ALTER TABLE Table_01 ENABLE TRIGGER ALL
ALTER TABLE Table_01 WITH CHECK CHECK CONSTRAINT ALL
ALTER TABLE Table_02 WITH CHECK CHECK CONSTRAINT ALL
但由于某些原因,当我尝试放回约束时,它会给出此错误:-
ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_Table_01_AAAAA_AAAA'.
The conflict occurred in database 'DB', table 'Table_02', column 'ID'.
注意:我有很多类似数据库的位置安装。所以这个启用/禁用约束适用于某些数据库,但不是全部。
I need to update few records in tables of a database. There are few foreign key constraints on these tables.
To disable constraint, I am using this:-
ALTER TABLE Table_01 NOCHECK CONSTRAINT ALL
ALTER TABLE Table_02 NOCHECK CONSTRAINT ALL
ALTER TABLE Table_01 DISABLE TRIGGER ALL
To Enable constraint back, I am using this:-
ALTER TABLE Table_01 ENABLE TRIGGER ALL
ALTER TABLE Table_01 WITH CHECK CHECK CONSTRAINT ALL
ALTER TABLE Table_02 WITH CHECK CHECK CONSTRAINT ALL
But for some reasons, when i try to put constraint back, it gives this error:-
ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_Table_01_AAAAA_AAAA'.
The conflict occurred in database 'DB', table 'Table_02', column 'ID'.
Note: I have many locations where similar database is installed. so this enabled / disable constraint is working on some database but not all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您执行的更新之一违反了外键约束。在找到并更正有问题的数据之前,您将无法再次启用它。
One of the updates you performed violated the foreign key constraint. You won't be able to enable it again until you find and correct the offending data.