Get all foreign key relationships referencing your table and drop them

Hi Folks,

Its back to back post today.The script I am sharing today is very useful and I actually

found it way back last year,so thought to share it.

 

–To get all foreign key relationships referencing your table, you could use this SQL (if you’re on SQL Server 2005 and up):
SELECT *
FROM sys.foreign_keys
WHERE referenced_object_id = object_id(TableName)

–and if there are any, with this statement here, you could create SQL statements to actually drop those FK relations:
SELECT
    ‘ALTER TABLE ‘ + OBJECT_NAME(parent_object_id) +
    ‘ DROP CONSTRAINT ‘ + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id(‘Student’)

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s