TSQL 更改表为删除和更新的级联添加约束 [英] TSQL alter table adding constraint for both cascade on delete and update
本文介绍了TSQL 更改表为删除和更新的级联添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 tsql 中创建对更新和删除的约束.我尝试了几种不同的方法,现在我有点卡住了&沮丧 - 似乎很简单.我知道您无法更改现有约束,因此我不确定如何执行此操作;
I'm trying to create a constraint with both on update and delete in tsql. I've tried a couple of different methods, and now I'm a little stuck & frustrated - seems so simple. I know you can't alter an existing constraint so I'm not sure about how to do this;
alter table AllowedCars
add constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(LocusID) on delete cascade,
constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(CarID) on update cascade
或者这个;
alter table AllowedCars add constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(CarID) on delete cascade and on update cascade
推荐答案
您需要先删除约束,然后重新创建它.您的第二次尝试是正确的,但您需要删除 和
.
You need to drop constraint first, and then recreate it. Your second attempt was right, but you needed to remove and
.
alter table AllowedCars
drop constraint FK_AllowedCars_CarID
alter table AllowedCars
add constraint FK_AllowedCars_CarID
foreign key (CarID)
references Cars(CarID)
on delete cascade
on update cascade
这篇关于TSQL 更改表为删除和更新的级联添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文