Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Foreign key constraint on a table?

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

like image 319
Vinod Avatar asked Apr 08 '09 01:04

Vinod


2 Answers

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
like image 124
Chris Shaffer Avatar answered Sep 22 '22 15:09

Chris Shaffer


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.

like image 33
Andy Jones Avatar answered Sep 24 '22 15:09

Andy Jones