sql server 中实现级联删除和级联更新
1. 触发器方法
create Trigger Trigger_Name
on Table1
after delete,UPDATE
as
begin
set nocount on;
if exists(select * from inserted)
update Table2 set field1 = (select field1 from inserted) where field1 = (select field1 from inserted)
else
delete from Table2 where field1 = (select field1 from deleted)
set nocount off;
end
on Table1
after delete,UPDATE
as
begin
set nocount on;
if exists(select * from inserted)
update Table2 set field1 = (select field1 from inserted) where field1 = (select field1 from inserted)
else
delete from Table2 where field1 = (select field1 from deleted)
set nocount off;
end
2. 创建外键关联关系
ALTER TABLE table1 ADD
CONSTRAINT [FK_Table1_Table2]
FOREIGN KEY
(
field1
) REFERENCES table2 (
field1
) ON UPDATE CASCADE ON DELETE CASCADE
CONSTRAINT [FK_Table1_Table2]
FOREIGN KEY
(
field1
) REFERENCES table2 (
field1
) ON UPDATE CASCADE ON DELETE CASCADE
继续追寻。。。。。。