Joe Webb

Musing and observations about SQL Server, other technogies, and sometimes just life in general
posts - 85, comments - 401, trackbacks - 0

My Links

News

This blog has moved!

Click here for the new location.

Follow me on Twitter
Add to Technorati Favorites

Search this Blog
 




Archives

Post Categories

About me

How to: Disable constraints on a table

Sometimes it's useful to disable one or more constraints on a table, do something significant, and then re-enable the constaint(s) after you're done. This is most often done to improve performance during a bulk load operation.

According to SQL Server Books Online, we can disable constraints using the ALTER TABLE statement.Here's an excerpt from SQL Server Books Online that describes it.

{ CHECK | NOCHECK } CONSTRAINT

    Specifies that constraint_name is enabled or disabled. This option can only be used with FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints cannot be disabled.

ALL

    Specifies that all constraints are either disabled with the NOCHECK option or enabled with the CHECK option.

For more information, see the "Alter Table (Transact-SQL)" topic in SQL Server Books Online (http://technet.microsoft.com/en-us/library/ms190273.aspx).

Let's consider an example.The following script disables a single constraint, does something, and then re-enables the constraint using the ALTER TABLE statement.

--disable the CK_Customer_CustomerType constraint 
ALTER
TABLE Sales.Customer NOCHECK CONSTRAINT CK_Customer_CustomerType

--do something

--enable the CK_Customer_CustomerType constraint
ALTER
TABLE Sales.Customer CHECK CONSTRAINT CK_Customer_CustomerType

 

The following example shows how to disable all constraints on a table.

--disable all constraints for the Sales.SalesOrderHeader table 
ALTER
TABLE Sales.SalesOrderHeader NOCHECK CONSTRAINT ALL

--do something --enable all constraints for the Sales.SalesOrderHeader table
ALTER TABLE Sales.SalesOrderHeader CHECK CONSTRAINT ALL

Cheers!

Joe

kick it on DotNetKicks.com

Print | posted on Wednesday, October 01, 2008 10:57 AM | Filed Under [ T-SQL ]

Feedback

Gravatar

# re: How to: Disable constraints on a table

Joe,

When re-enabling the constraints, it's imperative that you tell SQL Server to check the existing population:
ALTER TABLE Sales.SalesOrderHeader WITH CHECK CHECK CONSTRAINT ALL;
(Note that the double CHECK is *not* a typo!)

Failing to do so, as in your code, makes the constraints non-trusted. The optimizer will now no longer use these constraints to optimize execution plans.

More info at http://sqlblog.com/blogs/hugo_kornelis/archive/2007/03/29/can-you-trust-your-constraints.aspx

Best, Hugo
10/2/2008 3:30 AM | Hugo Kornelis
Gravatar

# re: How to: Disable constraints on a table

Excellent point, Hugo. I would have done well to mention that in the post. Thanks for pointing it out in your comment.

Cheers!

Joe
10/2/2008 7:28 AM | Joe Webb
Gravatar

# re: How to: Disable constraints on a table

great an article, I'm very thank you, I think really usefull, I'm following comments, thanks
10/16/2008 7:49 PM | Turkey

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 8 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET