我试图弄清楚这两种方法用于检查是否存在然后删除约束是完全相同的,还是每种方法都给出了某种结果差异。代码如下:
方法1:
if OBJECT_ID('fk_Copy_Item', 'F') is not null
alter table Rentals.Copy
drop constraint fk_Copy_Item;
go
方法2:
if exists
(
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_SCHEMA = 'Rentals'
and CONSTRAINT_NAME = 'fk_Copy_Item'
and CONSTRAINT_TYPE = 'foreign key'
)
alter table Rentals.Copy
drop constraint fk_Copy_Item;
go
谢谢!
答案 0 :(得分:0)
是的,它们是一样的。针对OBJECT_ID()
表(MSDN documentation)和sys.objects
的{{1}}函数查询只是针对相同的视图:
INFORMATION_SCHEMA.TABLE_CONSTRAINTS