日期:2014-05-18  浏览次数:20559 次

sql server多表删除问题 在线等,快点
创建三个表如下:
SQL code

create table a(
    aid varchar(10) primary key,
    shuxing_a varchar(10)
)
create table b(
    bid varchar(10) primary key,
    shuxing_b varchar(10)
)
create table a_b(
    aid varchar(10) foreign key references a(aid) , 
    bid varchar(10)  foreign key references b(bid),
)
--插入数据:
insert into a values('01', 'aa')
insert into a values('02', 'bb')
insert into a values('03', 'cc')

insert into b values('10', 'dd')
insert into b values('20', 'ee')
insert into b values('30', 'ff')

insert into a_b values('01', '10')
insert into a_b values('02', '20')
insert into a_b values('02', '30')



此时我要删除a表中的‘01’,‘aa’怎么删除?还有,我要同时删除‘01’,‘aa’,和a_b表中的‘01’,‘10’又怎么删除?求大侠指点,在线等待

------解决方案--------------------
这样创建就可以直接删除了
SQL code

create table a_b(
    aid varchar(10) constraint [fk_a_b_aid] foreign key  references a(aid) on delete cascade, 
    bid varchar(10) constraint [fk_a_b_bid] foreign key  references b(bid) on delete cascade
)