SQL外键级联更新、删除的设置

create table t1(id int identity primary key,col int)
create table t2(id int references t1(id) on update cascade on delete cascade,col int)
–alter table t2 add foreign key (id) references t1(id) ON UPDATE CASCADE ON DELETE CASCADE

insert t1 values(0)
insert t1 values(2)

insert t2 select 2,9
–delete t2
delete t1

select * from t2

drop table t2
drop table t1

 

上面ON UPDATE CASCADE,ON DELETE CASCADE两个选项,指明以后author表的id字段有delete,update操作时,myBBS表中的id也会被级联删除或更新。如果没有选中,是不可以对author表中已被myBBS表关联的id进行update或者delete操作的。

 

 

    原文作者:SQL
    原文地址: https://blog.csdn.net/htl258/article/details/4033466
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞