码迷,mamicode.com
首页 > 数据库 > 详细

How can I list all foreign keys referencing a given table in SQL Server?

时间:2017-01-10 19:31:59      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:column   can   from   const   reference   constrain   exec   lis   rom   

1. EXEC sp_fkeys ‘TableName‘

2. SELECT obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

How can I list all foreign keys referencing a given table in SQL Server?

标签:column   can   from   const   reference   constrain   exec   lis   rom   

原文地址:http://www.cnblogs.com/bi-info/p/6264941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!