SQL Server:如何从 information_schema 获取外键引用?
在SQL Server中,如何从外键中获取引用的表+列名?
注意:不是键所在的表/列,而是它引用的键。
示例:
当表[T_ALV_Ref_FilterDisplay]
中的键[FA_MDT_ID]
时。 指的是 [T_AP_Ref_Customer].[MDT_ID]
例如在创建这样的约束时:
ALTER TABLE [dbo].[T_ALV_Ref_FilterDisplay] WITH CHECK ADD CONSTRAINT [FK_T_ALV_Ref_FilterDisplay_T_AP_Ref_Customer] FOREIGN KEY([FA_MDT_ID])
REFERENCES [dbo].[T_AP_Ref_Customer] ([MDT_ID])
GO
我需要获取 [T_AP_Ref_Customer].[MDT_ID]
当给定 [T_ALV_Ref_FilterAnzeige].[FA_MDT_ID]
作为输入时
In SQL Server, how can I get the referenced table + column name from a foreign key?
Note: Not the table/column where the key is in, but the key it refers to.
Example:
When the key [FA_MDT_ID]
in table [T_ALV_Ref_FilterDisplay]
.
refers to [T_AP_Ref_Customer].[MDT_ID]
such as when creating a constraint like this:
ALTER TABLE [dbo].[T_ALV_Ref_FilterDisplay] WITH CHECK ADD CONSTRAINT [FK_T_ALV_Ref_FilterDisplay_T_AP_Ref_Customer] FOREIGN KEY([FA_MDT_ID])
REFERENCES [dbo].[T_AP_Ref_Customer] ([MDT_ID])
GO
I need to get [T_AP_Ref_Customer].[MDT_ID]
when given [T_ALV_Ref_FilterAnzeige].[FA_MDT_ID]
as input
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没关系,这是正确的答案:
http://msdn.microsoft.com/en-us/ Library/aa175805(SQL.80).aspx
注意:
Information_schema 不包含索引(它确实找到唯一约束)。
因此,如果您想根据唯一索引查找外键,则需要查看微软专有表:
边缘情况的证明测试:
Never mind, this is the correct answer:
http://msdn.microsoft.com/en-us/library/aa175805(SQL.80).aspx
Note:
Information_schema doesn't contain indices (it does find unique-contraints).
So if you want to find foreign-keys based on unique-indices, you need to go over the microsoft proprietary tables:
Proof-test for edge-cases:
如果您可以接受使用 SQL Server 特定架构目录视图,此查询将返回您正在寻找的内容:
不确定如何 - 如果有的话 - 您可以从 INFORMATION_SCHEMA 视图中获取相同的信息....
If you can live with using the SQL Server specific schema catalog views, this query will return what you're looking for:
Not sure how - if at all - you can get the same information from the INFORMATION_SCHEMA views....
我想要一个可以让我找到所有具有/缺少约束的“Key”和“ID”列的版本。所以我想要将所有列与所有 PK OR FK OR Null 的列表进行比较,这是我的查询。希望它对其他人有帮助!
格式由以下内容提供:http://www.dpriver.com/pp/sqlformat.htm
I wanted a a version that would let me find all the "Key" and "ID" columns having/missing a constraint. So i wanted all columns compared to the list of all PK OR FK OR Null, here is my query. Hope it helps someone else!
formatting courtesy of: http://www.dpriver.com/pp/sqlformat.htm