Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Check/NoCheck difference in generated scripts

I am trying to sync up the schemas between to different databases. Basically, I ran tasks->Generate Scripts with SQL Server Management Studio (2005) on both databases and am comparing the output with a diff tool.

For some reason, one script adds the constraint WITH CHECK and one WITH NO CHECK, followed by both constraints being re-enabled.

I for the first database I get:

ALTER TABLE [dbo].[Profile]  WITH CHECK ADD  CONSTRAINT [FK_Profile_OrganizationID] FOREIGN KEY([OrganizationID]) REFERENCES [dbo].[Organization] ([OrganizationID]) GO ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_Profile_OrganizationID] GO 

The second database generates as

ALTER TABLE [dbo].[Profile]  WITH NOCHECK ADD  CONSTRAINT [FK_Profile_OrganizationID] FOREIGN KEY([OrganizationID]) REFERENCES [dbo].[Organization] ([OrganizationID]) GO ALTER TABLE [dbo].[Profile] CHECK CONSTRAINT [FK_Profile_OrganizationID] GO 

So I have two questions:

  1. Is the end result the same? (Edit: It seems that a lot of people are picking up on only the first statement of the two scripts. I am interested in the end result of the entirety of both scripts.)

  2. If the end result is the same, why does Management Studio generate them differently for different databases?

like image 782
Nathan Avatar asked Aug 26 '09 22:08

Nathan


People also ask

How do you know if a FOREIGN KEY is disabled?

So to see which foreign keys are enabled or disabled, you can check the sys. foreign_keys table on 2005. It has a is_disabled column that has the value of 1 if the foreign key is disabled. Similarly you can get the information about whether or not a constraint is trusted.

What does Nocheck constraint do?

WITH CHECK | WITH NOCHECK Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.

How use Nocheck constraint in SQL Server?

The syntax for disabling a check constraint in SQL Server (Transact-SQL) is: ALTER TABLE table_name NOCHECK CONSTRAINT constraint_name; table_name. The name of the table that you wish to disable the check constraint.

What is Nocheck in SQL Server?

WITH CHECK will check existing data against the new constraint. WITH NOCHECK will not check existing data against the new constraint. This will allow you to have child records without a corresponding parent.


2 Answers

The end result is not the same!

SQL Server will not trust the uniqueness of the FK is it is not checked. This means additional processing is required if you use the column in a query.
Long story short is that you should get SQL Server to check the column so it's considered trusted.

As for why they're different from different servers, check the isnottrusted column in sys.foreign_keys. This may affect what SSMS is generating?

For more of a rant on this, check my other answer that relates to FK & NO CHECK/ CHECK options.

like image 88
Nick Kavadias Avatar answered Sep 21 '22 19:09

Nick Kavadias


Yes the two scripts are different

WITH CHECK will check existing data against the new constraint.
WITH NOCHECK will not check existing data against the new constraint. This will allow you to have child records without a corresponding parent.

EDIT: As for why SSMS is doing this I have no idea

like image 44
Cory Avatar answered Sep 22 '22 19:09

Cory