日期:2014-05-16 浏览次数:20662 次
create table a ( id varchar(20) primary key, password varchar(20) not null ) create table b ( id int identity(1,1) primary key, name varchar(50) not null, userId varchar(20), foreign key (userId) references a(id) on delete cascade ) 表B创建了外码userId 对应A的主码ID,声明了级联删除
或者
alter table? b add constraint fk_b_userId foreign key (userId) references a(id) on delete cascade
测试数据: insert into a values ('11','aaa') insert into a values('23','aaa') insert into b values('da','11') insert into b values('das','11') insert into b values('ww','23') 删除A表内id为‘11’的数据,发现B表内userId 为“11”也被数据库自动删除了,这就是级联删除 delete from a where id='11'
?
<!-- 导入 在此 参考资料--><!-- end 参考资料-->