- /*
- 标题:两表通过字段关联进行级联删除。
- 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
- 时间:2008-11-20
- 地点:广东深圳
- */
-
- create table ta(id int not null)
- create table tb(id int , aid int)
- insert into ta values(1)
- insert into ta values(2)
- insert into tb values(1 , 1)
- insert into tb values(2 , 2)
- insert into tb values(3 , 1)
- go
-
-
-
- select * from ta
- /*
- id
-
- 1
- 2
- */
-
- select * from tb
- /*
- id aid
-
- 1 1
- 2 2
- 3 1
- */
-
-
- delete from ta where id = 1
- select * from ta
- /*
- id
-
- 2
- */
- select * from tb
- /*
- id aid
-
- 1 1
- 2 2
- 3 1
- */
-
-
- insert into ta values(1)
-
- alter table ta add constraint pk_ta_id primary key (id)
- go
-
- alter table tb add constraint fk_tb_aid foreign key (aid) references ta(id) on delete cascade
- go
- delete from ta where id = 1
- select * from ta
- /*
- id
-
- 2
- */
- select * from tb
- /*
- id aid
-
- 2 2
- */
-
-
- alter table tb drop constraint fk_tb_aid
- go
-
- drop table ta , tb
- go
posted @
2012-10-27 08:28
生命周期管理
阅读(
170)
评论()
收藏
举报