mysql怎么设置级联删除

mysql怎么设置级联删除,第1张

这个在建表的时候就可以指定了

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的记录都会被删掉。

mysql:

1、先给B表添加主键

alter table tableB add constraint b_PK primary key(date,channel)

2、将B表的组合主键关联到A表的外键组合键中

alter table tableB add constraint tableB_fk foreign key(date,channel) references tableA (date,channel) on DELETE CASCADE

3 delete from tableA where date =? and channel =? 就出发相关记录··~


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8462903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存