使用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)?
我知道我可以
DELETE
,然后RESEED
标识(或)我认为只要在父表之前截断子表,不执行上述任一选项就可以了,但是我收到此错误:
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 约束引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!