就是一方依赖另一方,被依赖的发生改变,依赖者也会跟着改变。所以当被依赖着发生更新、删除依赖方也会同样的同步它的内容。

-- 创建学生表
create table stu(
sid int UNSIGNED primary key auto_increment,
name varchar(20) not null);

-- 创建成绩表,并把学号关联上更新和删除的级联
create table sc(
scid int UNSIGNED primary key auto_increment,
sid int UNSIGNED not null,
score varchar(20) default '0',
index (sid),
FOREIGN KEY (sid) REFERENCES stu(sid) ON DELETE CASCADE ON UPDATE CASCADE);

-- 插入学生数据和成绩数据
insert into stu (name) value ('zxf');
insert into stu (name) value ('ls');
insert into stu (name) value ('zs');
insert into stu (name) value ('ww');

insert into sc(sid,score) values ('1','98');
insert into sc(sid,score) values ('1','98');
insert into sc(sid,score) values ('2','34');
insert into sc(sid,score) values ('2','98');
insert into sc(sid,score) values ('2','98');
insert into sc(sid,score) values ('3','56');
insert into sc(sid,score) values ('4','78');
insert into sc(sid,score) values ('4','98');

接下来只要对学生表中的学号进行修改那么成绩表中的学号也会跟着修改了

01-21 01:49