create table student(
id int primary key auto_increment,
name varchar(15) not null,
gender varchar(10) not null,
cid int,
foreign key(cid) references class(id)
);
-- 實驗表結(jié)構(gòu)
create table class(
id int primary key auto_increment,
cname varchar(15)
);
create table student2(
id int primary key auto_increment,
name varchar(15) not null,
gender varchar(10) not null,
cid int,
foreign key(cid) references class(id) on delete set null on update cascade
);
-- 實驗表數(shù)據(jù):
insert into class(cname) values("python"),("linux"),("java"),("html5");
insert into student2(name,gender,cid) values("Alice","female",1);
insert into student2(name,gender,cid) values("John","female",2);
insert into student2(name,gender,cid) values("Jack","female",3);
insert into student2(name,gender,cid) values("Amy","female",4);
select * from student2;
select * from class;
-- 嘗試更新級聯(lián)
update class set id = 6 where cname="python";
select * from student2; -- 結(jié)果原來的python的cid=6
-- 嘗試刪除置空
delete from class where cname="java";
select * from student2; -- 結(jié)果原來的java的cid=null
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》