批量插入时忽略外键约束
我想批量插入大量具有循环依赖关系的生成数据(每个表中的列是约束到另一个表的外键)。为了解决这个问题,我想关闭外键约束,插入数据,然后重新打开约束。
谷歌搜索后,我找到了很多解决方案,但没有一个有效。现在我有:
ALTER TABLE TableName NOCHECK CONSTRAINT ALL
命令运行并且不会产生任何错误,但是当我尝试清除表以准备插入数据时,我收到以下错误:
System.Data:0:in `OnError': The DELETE statement conflicted with the REFERENCE constraint "FK_1_2_ConstraintName". The conflict occurred in database "DatabaseName", table "dbo.SomeOtherTable", column 'PrimaryKey'.\r\nThe statement has been terminated.\r\nChecking identity information: current identity value '0', current column value '0'.\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator. (System::Data::SqlClient::SqlException)
我当前的理论是这是由外键约束引起的在另一个表上,这取决于正在更改的表。
对于这个问题,我可以提出两种解决方案:
遍历所有与我插入的表有依赖关系的表,并禁用它们的外键约束。这似乎不必要地复杂。
禁用数据库中所有表的外键约束。
两种解决方案都可以,但我不确定从哪里开始使用这两种解决方案。有什么想法吗?
I want to batch insert a large amount of generated data which has a circular dependency (a column in each table is foreign key constrained to the other table). To get around this, I want to just turn off the foreign key constraints, insert the data, and then turn the constraints back on.
Googling around, I found a bunch of solutions, but none of them worked. Right now I have:
ALTER TABLE TableName NOCHECK CONSTRAINT ALL
The command runs and doesn't produce any errors, but when I attempt to clear the table in preparation for inserting the data, I get the following error:
System.Data:0:in `OnError': The DELETE statement conflicted with the REFERENCE constraint "FK_1_2_ConstraintName". The conflict occurred in database "DatabaseName", table "dbo.SomeOtherTable", column 'PrimaryKey'.\r\nThe statement has been terminated.\r\nChecking identity information: current identity value '0', current column value '0'.\r\nDBCC execution completed. If DBCC printed error messages, contact your system administrator. (System::Data::SqlClient::SqlException)
My current theory is that this is caused by a foreign key constraint on the other table which depends on the table being changed.
There are two solutions I can come up with to this problem:
Go through all the tables with dependencies on the table I'm inserting into and disable their foreign key constraints. This seems unnecessarily complicated.
Disable foreign key constraints on all of the tables in the database.
Either solution would work, but I'm not sure where to start on either solution. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我用于此类工作的方法。
This is what I used for this kind work.
您还可以在 FK 约束上启用
ON DELETE CASCADE
,这将导致只要主表上的 PK 被删除,它们的记录就会被删除。这将是一次性更改,不需要您在每次加载时重新运行它。编辑:
更多信息,这里是链接到来自 Pinal Dave 博客 (SQLAuthority) 的脚本,其中列出了所有FK 约束。如果需要,底部的
WHERE
子句可让您将其限制为某个 PK 和 FK 表集。You could also enable
ON DELETE CASCADE
on the FK Constraints, which will cause their records to be deleted whenever the PK on your main table is deleted. This would be a one-time change and wouldn't require you to rerun it each load.EDIT:
More info, here is a link to a script from Pinal Dave's blog (SQLAuthority) that lists all FK constraints. The
WHERE
clause at the bottom lets you restrict it to a certain PK and FK table set if desired.禁用约束和触发器
请参阅“禁用所有外键”部分
Disabling Constraints and Triggers
See the section "Disabling All Foreign Keys"