In SSMS, Tasks > Generate Scripts, I have selected a few tables and in the generated SQL I get:
ALTER TABLE [Project] WITH CHECK ADD CONSTRAINT [FK_Project_aspnet_Users] FOREIGN KEY([UserId])
REFERENCES [aspnet_Users] ([UserId])
GO
ALTER TABLE [Project] CHECK CONSTRAINT [FK_Project_aspnet_Users]
GO
Why does it CHECK in the second statement if WITH CHECK is specified in the first? The second statement appears directly after the first.
TIA
You are correct to observe that it is redundant.
It’s just the way that SSMS generates scripts.
Note that the script could be modified after it is generated. That first statement could be changed to use ‘
WITH NO CHECK‘, and that second statement would not be redundant.The purpose of the first statement is to add the constraint.
The purpose of the second statement is to enable the constraint.
At least, that’s the way I read the script. If I were manually running the statements one by one, I might get a “constraint already exists” exception, but that constraint might not be enabled.
I would still run the second statement, to ensure the existing constraint gets enabled.