6

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

Koray Tugay
  • 21,794
  • 41
  • 171
  • 299
Vinod
  • 30,565
  • 34
  • 92
  • 117
  • Check also this related question: [Can foreign key constraints be temporarily disabled using TSQL?](http://stackoverflow.com/questions/159038) – kristof Apr 08 '09 at 07:58

2 Answers2

25

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
  • 31,567
  • 5
  • 48
  • 61
1

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
  • 1,397
  • 9
  • 15