mysql怎么设置级联删除
2个回答
展开全部
这个在建表的时候就可以指定了
CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL
);
CREATE TABLE rooms (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
当执行
DELETE FROM buildings
WHERE
building_no = 2;的时候,rooms表中的building_no =2的记录都会被删掉。
CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL
);
CREATE TABLE rooms (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
当执行
DELETE FROM buildings
WHERE
building_no = 2;的时候,rooms表中的building_no =2的记录都会被删掉。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
通过触发器来级联删除:
具体的触发器代码如下:
Create TRIGGER [dbo].[DeleteRelatedProducts] ON [dbo].[ProductCategory]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
delete from [dbo].[product] where categoryId in
(
select id from deleted
)
END
具体的触发器代码如下:
Create TRIGGER [dbo].[DeleteRelatedProducts] ON [dbo].[ProductCategory]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
delete from [dbo].[product] where categoryId in
(
select id from deleted
)
END
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |