SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Dropping Foreign Keys The Lazy Way

Hello!

So, on another blog a long time ago, I wrote a couple of posts about foreign keys and what step can be taken to suspend them when we are trying to load/delete data into them. There’s the elegant way and the brute force way.

Hang On…

“But aren’t they there to stop this sort of thing?!” I hear the sensible people cry. And yes, this is true. BUT! Say we are inserting static data into a database at the point of creation using a tool like SSDT, we may not care about what order the data is loaded in, just that it is in. So, with some justification we can drop foreign keys post deploy, load the data and re-create.

Trouble is that elegant solution is totally over-engineered for this. And brute force is just bad. So is there an easier way?

Come to find out yes, there is an easier way. In just two lines:

View the code on Gist.

So the first line removes all checks, as well as foreign keys. The second one rectifies the first one. And in between those two lines all manner of chaos can be scripted.

Richie Lee's SQL Stuff

Full time computer guy, part time runner. Full time Dad, part time blogger. Knows a thing or two about Pokémon. Knows too much about SQL Agent. Writer of fractured sentences. Maker of the best damn macaroni cheese you've ever tasted.

Comments

Leave a comment on the original post [bzzzt.io, opens in a new window]

Loading comments...