I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the table?
(SQL answers preferable over clicking about in the GUI of the management studio.)
This gets any foreign key that involves the chosen table. *Assumes a _FIRSTABLENAME_SECONDTABLENAME format.
This is a more general form:
The following solution work for me:
Not sure why no one suggested but I use
sp_fkeys
to query foreign keys for a given table:If you want to get the foreign key relation of all the tables exclude the
where
clause else write your tablename instead oftablename
The original question asked to get a list of all foreign keys into a highly referenced table so that the table can be removed.
This little query returns all the 'drop foreign key' commands needed to drop all foreign keys into a particular table:
Example output:
Omit the WHERE-clause to get the drop commands for all foreign keys in the current database.