网站建设资讯

NEWS

网站建设资讯

mysql级联怎么整 mysql级联删除语句

mysql怎么设置级联删除

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

我们提供的服务有:网站制作、成都做网站、微信公众号开发、网站优化、网站认证、师宗ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的师宗网站制作公司

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数据库外键设置级联

CONSTRAINT `外键名` FOREIGN KEY (`外键的字段`) REFERENCES `外键关联的表` (`外键关联的字段`) ON DELETE CASCADE ON UPDATE CASCADE

ON DELETE代表删除时

ON UPDATE代表更新时

CASCADE代表级联

mysql表的级联问题

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 =? 就出发相关记录··~


分享标题:mysql级联怎么整 mysql级联删除语句
网页地址:http://njwzjz.com/article/ddiodhd.html