如何重新检查sql server中表中已有数据的主/外键约束?

Jul*_*uim 4 sql-server foreign-keys check-constraints

我在SQL Server 2005中有一个带有外键的表,它禁用了大量数据加载,然后重新启用:

例:

alter table table1 nocheck constraint fk_1
go
lots of inserts...
go
alter table table1 check constraint fk_1
go
Run Code Online (Sandbox Code Playgroud)

现在,问题是:有没有办法重新检查这个刚刚插入的数据?

Joe*_*lli 8

语法看起来有些愚蠢,重复了"check"这个词,但你想要的是:

alter table table1 with check check constraint fk_1
go
Run Code Online (Sandbox Code Playgroud)

添加"with check"选项将根据约束验证现有数据.这样做还可以防止约束变得不可信.

如果任何现有数据违反约束,您将收到如下错误:

The ALTER TABLE statement conflicted with the CHECK constraint "fk_1".
Run Code Online (Sandbox Code Playgroud)