级联更新和级联删除:alter table mylocation add constraint fk_mylocation_myname foreign key(myname) references myname(name) on update cascade on delete cascade
给你一个例题吧我也是刚刚学习的create table ai ( i int primary key , ii int ) go insert into ai select 1,1 insert into ai select 2,2 insert into ai select 3,3 insert into ai select 4,4 go create table aii ( i int constraint fk_aii foreign key(i) references ai(i) on update cascade, ii int ) go insert into aii select 1,1 insert into aii select 2,2 goupdate ai set i=5 where ii=2 /*更新表ai,把ii=2的行的i更新为5,这时级联更新发挥作用啦,表aii的第2行的i会被一起更新为5*/
更新主键值的操作,该值由其它表的现有行中的外键列引用。在级联更新中,更新所有外键值以与新的主键值相匹配。
级联删除 (cascading delete)
删除包含主键值的行的操作,该值由其它表的现有行中的外键列引用。在级联删除中,还删除其外键值引用删除的主键值的所有行。
add constraint fk_mylocation_myname foreign key(myname) references myname(name)
on update cascade on delete cascade
i int primary key ,
ii int
)
go
insert into ai select 1,1
insert into ai select 2,2
insert into ai select 3,3
insert into ai select 4,4
go
create table aii (
i int constraint fk_aii foreign key(i) references ai(i) on update cascade,
ii int
)
go
insert into aii select 1,1
insert into aii select 2,2
goupdate ai
set i=5
where ii=2
/*更新表ai,把ii=2的行的i更新为5,这时级联更新发挥作用啦,表aii的第2行的i会被一起更新为5*/
用触发器也可以,但是有一些注意事项,你可以看sql server联机丛书,在索引里面查找
create trigger,很详细