SQLServer---主键级联更新或删除

如果有主键或外键就不能修改或删除了,我们可以在创建约束的时候就加上级联更新或删除,这样只要主键更新或删除,所有外键都会更新或删除。

代码示例:

--添加级联更新
if exists(select * from sysobjects where name='fk_ProductId_PI')
alter table ProductInventory drop constraint fk_ProductId_PI

alter table ProductInventory add constraint fk_ProductId_PI foreign key (ProductId) references Products (ProductId) on update cascade

如果是级联删除:

on delete cascade

可以直接在创建表的时候就添加好约束。

posted @ 2018-01-05 14:20  EasonDongH  阅读(1183)  评论(0编辑  收藏  举报