Wednesday, April 20, 2011

Disable Foreign key constraint on a table?

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

From stackoverflow
  • 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
    
    ojblass : be warned this will fail if your data is not referntially in tact.
  • Check also this related question:

    Can foreign key constraints be temporarily disabled using TSQL?

  • 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.

0 comments:

Post a Comment