Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?(无法截断表,因为它正被 FOREIGN KEY 约束引用?)
问题描述
使用MSSQL2005,如果先截断子表(FK关系主键的表),是否可以截断带有外键约束的表?
Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)?
我知道我可以
- 使用不带where子句的
DELETE,然后RESEED标识(或) - 删除 FK,截断表,然后重新创建 FK.
我认为只要在父表之前截断子表,不执行上述任一选项就可以了,但是我收到此错误:
I thought that as long as I truncated the child table before the parent, I'd be okay without doing either of the options above, but I'm getting this error:
无法截断表TableName",因为它正被外键约束引用.
Cannot truncate table 'TableName' because it is being referenced by a FOREIGN KEY constraint.
推荐答案
正确;您不能截断具有 FK 约束的表.
Correct; you cannot truncate a table which has an FK constraint on it.
通常我的流程是:
- 放弃约束
- 截断桌子
- 重新创建约束.
(当然,一切都在交易中.)
(All in a transaction, of course.)
当然,这仅适用于子项已经被截断的情况.否则我会走不同的路线,完全取决于我的数据是什么样的.(变量太多,无法进入.)
Of course, this only applies if the child has already been truncated. Otherwise I go a different route, dependent entirely on what my data looks like. (Too many variables to get into here.)
原始海报确定了为什么会这样;请参阅此答案 了解更多详情.
The original poster determined WHY this is the case; see this answer for more details.
这篇关于无法截断表,因为它正被 FOREIGN KEY 约束引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法截断表,因为它正被 FOREIGN KEY 约束引用?
基础教程推荐
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
