Sql server

drop all foreign key constraints in a database

drop all foreign key constraints in a database

Using below code you can esaly remove all foreign-key constraints of tables.

SELECT ‘IF EXISTS (
SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N”[dbo].’ + FK +”’) AND
parent_object_id = OBJECT_ID(N”[dbo].’ + PT + ”’))
ALTER TABLE ‘ + PT + ‘ NOCHECK CONSTRAINT ‘ + FK + ‘;’ FROM (SELECT OBJECT_NAME(constraint_object_id) as FK, OBJECT_NAME(parent_object_id) as PT FROM [sys].[foreign_key_columns] ) T ORDER BY FK

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.