Is there is any way in TSQL to Drop a table with it's all Foreign Keys Constraint? I have search a lot but could not find any?
Asked
Active
Viewed 136 times
2 Answers
1
ALTER TABLE tablename NOCHECK CONSTRAINT all

Deepanshu Goyal
- 2,738
- 3
- 34
- 61
-
http://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint – Deepanshu Goyal Apr 01 '13 at 10:50
-
check for the answers in above link rather than the question – Deepanshu Goyal Apr 01 '13 at 10:51
1
To get all foreign key relationships referencing your table, you could use this SQL (if you're on SQL Server 2005 and up):
Use Below Script
SELECT *
FROM sys.foreign_keys
WHERE referenced_object_id = object_id(TableName)
SELECT
'ALTER TABLE ' + OBJECT_NAME(parent_object_id) +
' DROP CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id(TableName)

Munir Vora
- 51
- 11