且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL DROP TABLE外键约束

更新时间:2022-06-15 08:55:48

要获取所有的外键关系引用你的表,你可以使用这个SQL(如果你在SQL Server 2005和更高版本):

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('Student')

并且如果有,使用此语句可以创建SQL语句实际上删除那些FK关系:

and if there are any, with this statement here, you could create SQL statements to actually drop those FK relations:

SELECT 
    'ALTER TABLE ' +  OBJECT_SCHEMA_NAME(parent_object_id) +
    '.[' + OBJECT_NAME(parent_object_id) + 
    '] DROP CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Student')