开发者

How to recheck primary/foreign key constraint for data already in the table in sql server?

开发者 https://www.devze.com 2023-02-14 15:38 出处:网络
I have a table in SQL Server 2005 with a foreign key and开发者_JS百科 it was disable for huge data loading, and then re-enabled:

I have a table in SQL Server 2005 with a foreign key and开发者_JS百科 it was disable for huge data loading, and then re-enabled:

Example:

alter table table1 nocheck constraint fk_1
go
lots of inserts...
go
alter table table1 check constraint fk_1
go

Now, the question: is there a way to re-check this just inserted data?


The syntax looks a little silly with the word "check" repeated, but what you want is:

alter table table1 with check check constraint fk_1
go

Adding the "with check" option will validate existing data against the constraint. Doing this will also prevent the constraint from becoming untrusted.

If any existing data violates the constraint, you'll get an error that will look like this:

The ALTER TABLE statement conflicted with the CHECK constraint "fk_1".
0

精彩评论

暂无评论...
验证码 换一张
取 消