Roman Rehak

SQL Server and things not related

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910


Navigation

SQL Server Sites

Subscriptions

News

eXTReMe Tracker

Post Categories



Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints

Foreign key constraints and check constraint are very useful for enforcing data integrity and business rules. There are certain scenarios though where it is useful to temporarily turn them off because their behavior is either not needed or could do more harm than good. I sometimes disable constraint checking on tables during data loads from external sources or when I need to script a table drop/recreate with reloading the data back into the table. I usually do it in scenarios where I don't want a time consuming process to fail because one or a few of many million rows have bad data in it. But I always turn the constraints back on once the process is finished and also in some cases I run data integrity checks on the imported data.

If you disable a foreign key constraint, you will be able to insert a value that does not exist in the parent table. If you disable a check constraint, you will be able to put a value in a column as if the check constraint was not there. Here are a few examples of disabling and enabling table constraints:

-- Disable all table constraints
ALTER TABLE MyTable NOCHECK CONSTRAINT ALL

-- Enable all table constraints
ALTER TABLE MyTable CHECK CONSTRAINT ALL

-- Disable single constraint
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint

-- Enable single constraint
ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint

 

posted on Sunday, January 30, 2005 5:51 PM by Roman


# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Tuesday, July 12, 2005 9:41 AM

Indeed, very useful tip for time-consuming data loading.
Thank you!

Slava Brosgol

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Friday, June 23, 2006 11:09 AM

thanks...really helpful

nazar

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Wednesday, July 12, 2006 11:27 AM

Thanks, very helpful when importing or updating

jcurve

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Friday, July 21, 2006 8:32 AM

Thanks for the tip! I've looked all over the Microsoft SQL Server Documentation without any luck. The only way I found to turn off the FK constraints was manually using the GUI tool by adding some kind of exteneded property to the DB...which wasn't a desired approach. I've tried various scenarios with the ALTER...NOCHECK, but didnt' throw in the CONSTRAINT keyword. DULP!. I figured there must be a way--and there was. Thanks again and this was REALLY helpful!

Aaron Harshbarger

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Tuesday, December 12, 2006 5:29 PM

Great trick, thanks.

mark

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Tuesday, April 03, 2007 7:44 AM

very useful article

Suresh

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Thursday, September 06, 2007 4:54 AM

Great ........ pulled us out of trouble

shailesh

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Monday, September 24, 2007 10:40 AM

Perfect, just what we needed. Thank you!

Stefan Mai

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Monday, May 26, 2008 6:14 AM

Thanx a lot . Very Useful

Kris

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Wednesday, July 09, 2008 8:40 AM

Hello Roman,
I am trying to delete a table in my db, and its bringing out error about a foreign constraint referencing on this. I have disable the lookup table with the foreign key with a view to deleteing the table and enabling the lookup table back, all to no avail. Any other clue i can use? i don't want to drop the key.

Richard

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Wednesday, July 16, 2008 5:43 AM

You cannot truncate a table that has Foreign Keys on it..You have to delete the data using "delete from <Table name>"

Newbie

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Friday, October 03, 2008 2:19 AM

Thanks a lot its clear and crisp

Rama charan

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Monday, October 06, 2008 7:37 AM

very helpful

Aakash Sharma

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Sunday, October 19, 2008 11:46 AM

i take my hat off for you. thank you for the help

itzik

# re: Roman's Weekly SQL Server Tip - How to disable foreign key and check constraints @ Wednesday, November 05, 2008 5:37 PM

Nearly four years since you posted this advice and people are still finding it useful. I love Internet.

I just incorporated it into a stored procedure to turn off constraints so I could delete a parent record and insert a new one from a history table without disturbing the FK.

Jim




Powered by Dot Net Junkies, by Telligent Systems