October 13, 2016 at 9:02 am
I am doing some tetsing and i wanted to insert data into the table which has foreign keys. How can i disable before insert and enable the constraint after insert?
October 13, 2016 at 10:22 am
komal145 (10/13/2016)
I am doing some tetsing and i wanted to insert data into the table which has foreign keys. How can i disable before insert and enable the constraint after insert?
One way would be to something like:
--disable
ALTER TABLE YourTable
NOCHECK CONSTRAINT ALL
--Re-enable
ALTER TABLE YourTable
CHECK CONSTRAINT ALL
Sue
October 13, 2016 at 11:11 am
--disable specific key
ALTER TABLE dbo.table1
WITH NOCHECK
NOCHECK CONSTRAINT [FK_table1_keyColumn]
GO
-- Enable specific key
ALTER TABLE dbo.table1
WITH CHECK CHECK CONSTRAINT [FK_table1_keyColumn]
GO
October 13, 2016 at 12:30 pm
The short answer is you can do this with an alter check constraint. The right answer is that you are going to destroy your data integrity. Why would you want to test a schema without any data integrity?
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
October 13, 2016 at 3:47 pm
This is development enviornment and since data is not current need to enforce the data for my testing. Anyways i inserted data without enabling or disabling , just by adding data to the foreignkey tables.
October 13, 2016 at 5:56 pm
komal145 (10/13/2016)
This is development enviornment and since data is not current need to enforce the data for my testing. Anyways i inserted data without enabling or disabling , just by adding data to the foreignkey tables.
Development environments represent what will eventually happen in prod. Disabling foreign keys even in Dev is a bad idea. What you ended up doing (correctly adding to the referenced tables) was the better way to do things.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply