How can I list all foreign keys referencing a given table in SQL Server? 查找已知表的字段被哪些表,用来做外键关联字段
How can I list all foreign keys referencing a given table in SQL Server?
Not sure why no one suggested but I use sp_fkeys
to query foreign keys for a given table:
EXEC sp_fkeys 'TableName'
You can also specify the schema:
EXEC sp_fkeys @pktable_name = 'TableName', @pktable_owner = 'dbo'
Without specifying the schema, the docs state the following:
If pktable_owner is not specified, the default table visibility rules of the underlying DBMS apply.
In SQL Server, if the current user owns a table with the specified name, that table's columns are returned. If pktable_owner is not specified and the current user does not own a table with the specified pktable_name, the procedure looks for a table with the specified pktable_name owned by the database owner. If one exists, that table's columns are returned.
测试了一下,还是很给力的。直接给出一张表A,会直接找到那些用了这张表A的字段作为外键 的其他表。
注意事项,必须按照如下格式使用
EXEC sp_fkeys @pktable_name = 'CMS_Permission', @pktable_owner = 'dbo' EXEC sp_fkeys @pktable_name = 'CMS_Resource', @pktable_owner = 'dbo'
表名不能多加东西,比如dbo.[CMS_Permission]或者[CMS_Permission],都是无法识别的表名
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-03-14 pass an instance of class to TestCase as parameter
2018-03-14 How to Integrate .NET Projects with Jenkins
2016-03-14 Knowing When to Use Override and New Keywords (C# Programming Guide)
2016-03-14 Versioning with the Override and New Keywords (C# Programming Guide)
2016-03-14 Polymorphism (C# Programming Guide)
2015-03-14 2-Medium下的MultipleCommandAssembly
2015-03-14 如何获取supersocket的源代码