禁用外键约束,还是不能截断表?(SQL Server 2005)

2022-01-20 00:00:00 constraints foreign-keys sql-server

我有一个名为 PX_Child 的表,它在 PX_Parent 上有一个外键.我想暂时禁用这个 FK 约束,以便截断 PX_Parent.我不确定这是怎么回事.

I have a table called PX_Child that has a foreign key on PX_Parent. I'd like to temporarily disable this FK constraint so that I can truncate PX_Parent. I'm not sure how this goes however.

我已经尝试过这些命令

ALTER TABLE PX_Child NOCHECK CONSTRAINT ALL

ALTER TABLE PX_Parent NOCHECK CONSTRAINT ALL

(truncate commands)

ALTER TABLE PX_Child CHECK CONSTRAINT ALL

ALTER TABLE PX_Parent CHECK CONSTRAINT ALL

但截断仍然告诉我,由于外键约束,它不能截断 PX_Parent.我环顾了整个网络,似乎找不到我做错了什么,对不起这个问题的基本性质.

But the truncate still tells me it can't truncate PX_Parent because of a foreign key constraint. I've looked all around the net and can't seem to find what I'm doing wrong, sorry for the basic nature of this question.

推荐答案

如果有任何外键引用它,包括禁用的约束,则不能截断该表.您要么需要删除外键约束,要么使用 DELETE 命令.

You can't truncate the table if there is any foreign key referencing it, including disabled constraints. You either need to drop the foreign key constraints or use the DELETE command.

相关文章