?????????A????????????B?е???Σ??????γ????B?????????A??????????B???????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????ɡ?
??????????MySQL???????????3???????????????????????
?????????????????????????????????????????????????????????????????顣
??????????????????
????create table t_group(
????id int not null??
????name varchar(30)??
????primary key(id)
????);
???????????????????
??????????
????insert into t_group values (1?? 'Group1');
????insert into t_group values (2?? 'Group2');
???????洴?????????????????????????????????ù????
????1??????(cascade)???
???????????
????create table t_user(
????id int not null??
????name varchar(30)??
????groupid int??
????primary key(id)??
????foreign key(groupid) references t_group(id) on delete cascade on update cascade
????);
?????????????????
????insert into t_user values (1?? 'qianxin'?? 1); #???????
????insert into t_user values (2?? 'yiyu'?? 2); #???????
????insert into t_user values(3??'dai'??3); #????????????????3?????????????????????????
????insert into t_user values (3?? 'dai'?? 2);
??????????????
????insert into t_user values (1?? 'qianxin'?? 1); #???????
????insert into t_user values (2?? 'yiyu'?? 2); #???????
????insert into t_user values (3?? 'dai'?? 2);#???????
????delete from t_group where id=2; #????t_user?е?2??3??????????
????update t_group set id=2 where id=1; #????t_user?е?1?????groupid????????2
????2?????(set null)???
?????????
????create table t_user (
????id int not null??
????name varchar(30)??
????groupid int??
????primary key (id)??
????foreign key (groupid) references t_group(id) on delete set null on update set null
????);
?????????????????
????insert into t_user values (1?? 'qianxin'?? 1); #???????
????insert into t_user values (2?? 'yiyu'?? 2); #???????
????insert into t_user values (3?? 'dai'?? 3); #????????????????3?????????????????????????
??????????????
????insert into t_user values (1?? 'qianxin'?? 1);
????insert into t_user values (2?? 'yiyu'?? 2);
????insert into t_user values (3?? 'dai'?? 2);
????delete from t_group where id=2; #????t_user?е?2??3?????groupid???????NULL
????update t_group set id=2 where id=1; #????t_user?е?1?????groupid???????NULL
????3?????(no action / restrict)???
??????????
????create table t_user (
????id int not null??
????name varchar(30)??
????groupid int??
????primary key (id)??
????foreign key (groupid) references t_group(id) on delete no action on update no action
????);
?????????????????
????insert into t_user values (1?? 'qianxin'?? 1); #???????
????insert into t_user values (2?? 'yiyu'?? 2); #???????
????insert into t_user values (3?? 'dai'?? 3); #????????????????3?????????????????????????
??????????????
????insert into t_user values (1?? 'qianxin'?? 1);
????insert into t_user values (2?? 'yiyu'?? 2);
????insert into t_user values (3?? 'dai'?? 2);
????delete from t_group where id=2; #??????????????????????????????????
????update t_group set id=2 where id=1; #??????????????????????????????????
?????????MySQL?У?restrict?????no action????????????