对Sql Server中的表添加级联更新和级联删除的几种方法

create trigger trg_A
on A
for update,delete
as
begin
if exists(select 1 from inserted)
update B set Name=(select Name from inserted) where Name=(select Name from deleted)
else
delete B where Name=(select Name from deleted)
end
go


要使用级联性更新和删除,用户可以在Alter TABLE 语句的CONSTRAINT子句中使用ON Update CASCADE 和/或 ON Delete CASCADE 关键字。注意他们都必须是应用于外键的。



级联更新和级联删除方式:
Alter TABLE [dbo].[T_USERGROUP] ADD
CONSTRAINT [FK_T_USERGROUP_T_ACCTTEMPLATE] FOREIGN KEY
(
[ATNAME]
) REFERENCES [dbo].[T_ACCTTEMPLATE] (
[ATNAME]
) ON Update CASCADE ON Delete CASCADE

你可能感兴趣的:(sql,SQL Server,Go)