0

While generating the Create DB script for the existing Foreign Key constraint through SSMS, we could able to see two set of alter statements. one with NOCHECK ADD and another with CHECK.

ALTER TABLE [dbo].[claim]  WITH NOCHECK ADD  CONSTRAINT [FK_CLAIM_COPCID] 
FOREIGN KEY([copcid])
REFERENCES [dbo].[copc] ([CopcId])
GO

ALTER TABLE [dbo].[claim] CHECK CONSTRAINT [FK_CLAIM_COPCID]
GO

Why we are getting two set of scripts to create a new Constraint?.

1

1 Answer 1

4

The first alter statement is creating a constraint and instructing that constraint to be added without checking whether the existing rows obey the new constraint.

When you add a constraint without first checking the rows, it will not be fully active until you enable it. That's what the second statement does, it enables the new constraint. If there are rows that break the constraint, you won't be able to enable it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.