【发布时间】:2021-01-12 18:28:20
【问题描述】:
我有这个创建表脚本
CREATE TABLE Categories (
Id int IDENTITY(1,1) not null,
ParentId int null,
[Order] int default 0,
Published bit not null default 0,
Deleted bit not null default 0,
CONSTRAINT PK_Categories_Id PRIMARY KEY CLUSTERED (Id),
CONSTRAINT FK_Categories_ParentId FOREIGN KEY (ParentId) REFERENCES Categories(Id) on delete cascade,
Title nvarchar(255) NOT NULL,
CreatedAt DateTime not null default GETDATE(),
UpdatedAt DateTime not null default GETDATE()
);
我得到一个错误
Msg 1785, Level 16, State 0, Line 1
Introducing FOREIGN KEY constraint 'FK_Categories_ParentId' on table 'Categories' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 1, Line 1
Could not create constraint or index. See previous errors.
如何将删除级联添加到外键约束?
【问题讨论】:
标签: sql sql-server foreign-keys sql-delete self-reference