简体   繁体   English

SQL Server 2008-添加具有约束的自引用外键并获得“找不到数据类型”错误

[英]SQL Server 2008 - Adding self-referencing foreign key with constraint and getting “Cannot find data type” error

I am working in SQL Server 2008 and am trying to add a self referencing foreign key with an ON DELETE CASCADE constraint but am running into some issues. 我正在使用SQL Server 2008,并且尝试添加具有ON DELETE CASCADE约束的自引用外键,但是遇到了一些问题。 Basically I want the system to automatically delete all children of a node all the way down the hierarchy when a node is deleted. 基本上,我希望系统在删除节点时自动删除节点的所有子级。

I have a table, Node with the following basic structure: 我有一个具有以下基本结构的表Node

  • PK_Node_Id (primary key) PK_Node_Id(主键)
  • ParentNodeId (want to make this the self-referencing foreign key with ON DELETE CASCADE constraint. ParentNodeId(想使它成为具有ON DELETE CASCADE约束的自引用外键。

The problem lies when I try to add the constraint. 问题出在我尝试添加约束时。 If I use the GUI Designer, the INSERT and UPDATE Specification selections are grayed out and I can't do anything. 如果使用GUI设计器,则“ 插入”和“更新规范”选项将显示为灰色,并且我无法执行任何操作。

If I try to run an SQL statement to add the constraint: 如果我尝试运行SQL语句来添加约束:

ALTER TABLE Node
ADD CONSTRAINT
FK_Node_Node
FOREIGN KEY (ParentNodeId)
REFERENCES Node (PK_Node_Id)
ON DELETE CASCADE;

I get the following error: 我收到以下错误:

Cannot find data type FK_Node_Node.

FK_Node_Node is the name of my foreign key. FK_Node_Node是我的外键的名称。 Not sure why it is asking for or looking for a datatype... 不确定为什么要查询或寻找数据类型...

Thanks for your help! 谢谢你的帮助!

I get the following error: Cannot find data type FK_Node_Node. 我收到以下错误:找不到数据类型FK_Node_Node。

I suspect you misspelled the word "CONSTRAINT" when you ran the ALTER TABLE statement. 我怀疑您在运行ALTER TABLE语句时拼错了“ CONSTRAINT”一词。 The resulting statement is still syntactically valid, but instead of adding a foreign key constraint, it adds a column named (for example) "CONSTAINT" of type FK_Node_Node, plus an unnamed foreign key constraint. 该声明结果依然是语法上有效,但不是增加一个外键约束,它增加了一个名为(例如)型FK_Node_Node的“CONSTAINT”一 ,外加一个未命名的外键约束。

I want the system to automatically delete all children of a node all the way down the hierarchy when a node is deleted. 我希望系统在删除节点时自动删除节点的所有子级。

Unfortunately, this isn't possible: SQL Server doesn't allow cascading referential actions to form cycles. 不幸的是,这是不可能的:SQL Server不允许级联引用动作形成循环。 The error the ALTER TABLE statement should have given you is: ALTER TABLE语句应该给您的错误是:

Introducing FOREIGN KEY constraint 'FK_Node_Node' on table 'Node' may cause cycles or multiple cascade paths. 在表“节点”上引入外键约束“ FK_Node_Node”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Instead of using ON DELETE CASCADE, you need to perform the recursive deletion yourself: 代替使用ON DELETE CASCADE,您需要自己执行递归删除:

;WITH Nodes (PK_Node_Id) AS
(
    SELECT PK_Node_Id
    FROM Node
    WHERE PK_Node_Id = @NodeId  -- @NodeId is the root node you want to delete

    UNION ALL

    SELECT Child.PK_Node_Id
    FROM Node Child JOIN Nodes Parent ON Parent.PK_Node_Id = Child.ParentNodeId
)
DELETE FROM Node
WHERE PK_Node_Id IN (SELECT PK_Node_Id FROM Nodes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM