对Sql Server中的表添加级联更新和级联删除

以前用Sql Server只会对图形界面进行操作,现在发现自己的Sql语言功底是越来越差了,例如如何为两个表添加关联,让他们级联更新和级联删除。
到晚上查了一下,发现可以用两种办法

触发器方式:
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 [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)