Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in SQL by (20.3k points)

I'm looking for a way to temporarily turn off all DB's constraints (eg table relationships).

I need to copy (using INSERTs) one DB's tables to another DB. I know I can achieve that by executing commands in the proper order (to not break relationships).

But it would be easier if I could turn off checking constraints temporarily and turn it back on after the operation's finish.

Is this possible?

1 Answer

0 votes
by (40.7k points)

You can try disabling FK and CHECK constraints by using ALTER TABLE

ALTER TABLE foo NOCHECK CONSTRAINT ALL

Or else you can use the below code:

ALTER TABLE foo NOCHECK CONSTRAINT CK_foo_column

Note: Primary keys and unique constraints can not be disabled.

For more, information you can refer to ALTER TABLE. 

Browse Categories

...