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

代码示例:

--添加级联更新
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

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

相关文章:

  • 2021-07-11
  • 2022-12-23
  • 2021-11-30
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-25
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案