实际上,您可能不会Trigger
在这里使用,这是创建设置了标志的FOREIGN KEY约束的唯一方法DELETE CASCADE
CREATE TABLE Category
(
CategoryID INT,
Categoryname VARCHAR(50),
CONSTRAINT tb_pk PRIMARY KEY (CategoryID),
CONSTRAINT tb_uq UNIQUE (Categoryname)
)
GO
CREATE TABLE Product
(
ProductID INT,
ProductName VARCHAR(50) ,
CategoryID INT ,
Price DECIMAL(10,2),
CONSTRAINT p_pk PRIMARY KEY (ProductID),
CONSTRAINT p_uq UNIQUE (ProductName),
CONSTRAINT p_fk FOREIGN KEY (CategoryID)
REFERENCES Category(CategoryID) ON DELETE CASCADE
)
GO
当您尝试删除表Category
上的某些内容时Category
,它会自动删除其子行,因为表Product
依赖于表Category