如何列出引用SQLServer中给定表的所有外键?我需要删除SQLServer数据库中引用很高的表。如何获得删除表所需的所有外键约束的列表?(SQL回答比在Managementstudio的GUI中单击更好。)
3 回答
慕后森
TA贡献1802条经验 获得超5个赞
sp_fkeys
EXEC sp_fkeys 'TableName'
EXEC sp_fkeys @pktable_name = 'TableName', @pktable_owner = 'dbo'
如果未指定pktable_Owner,则应用基础DBMS的默认表可见性规则。
在SQLServer中,如果当前用户拥有具有指定名称的表,则返回该表的列。如果未指定pktable_Owner,且当前用户没有拥有具有指定pktable_name的表,则该过程将查找数据库所有者拥有的具有指定pktable_name的表。如果存在,则返回该表的列。
慕工程0101907
TA贡献1887条经验 获得超5个赞
select t.name as TableWithForeignKey, fk.constraint_column_id as FK_PartNo, c. name as ForeignKeyColumn from sys.foreign_key_columns as fkinner join sys.tables as t on fk.parent_object_id = t.object_idinner join sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_idwhere fk.referenced_object_id = (select object_id from sys.tables where name = 'TableOthersForeignKeyInto')order by TableWithForeignKey, FK_PartNo
select distinct name from sys.objects where object_id in ( select fk.constraint_object_id from sys.foreign_key_columns as fk where fk.referenced_object_id = (select object_id from sys.tables where name = 'TableOthersForeignKeyInto'))
添加回答
举报
0/150
提交
取消