6

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

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Vinod
  • 31,933
  • 35
  • 96
  • 119
  • 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
  • 32,199
  • 5
  • 49
  • 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,472
  • 9
  • 15