views:

687

answers:

3

Can i temporaryly disable a foreign key constraint. How do i do this?

+6  A: 

To temporarily disable a constraint (foreign keys are constraints):

ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint

To re-enable a constraint

ALTER TABLE MyTable CHECK CONSTRAINT MyConstraint
Chris Shaffer
be warned this will fail if your data is not referntially in tact.
ojblass
+1  A: 

Check also this related question:

Can foreign key constraints be temporarily disabled using TSQL?

kristof
A: 

Incidentally, this is why you need "Alter table" permissions when you BCP or Bulk Insert data into a table. Using the default configuration, check constraints and foreign keys are not checked.

Andy Jones