简体   繁体   English

在数据库中如何查找表之间的连接

[英]In a database how to find connections between tables

I have a database and it has lot of tables. 我有一个数据库,它有很多表。 I want to find column names from tables that link them to each other. 我想从链接它们的表中找到列名。 So as to join them. 以便加入他们。

I simply want to know what columns are common between the tables so I will know how to go ahead and join them to get the desired result. 我只想知道表之间共有哪些列,所以我将知道如何继续进行连接并将它们连接以得到所需的结果。

Is there a way to know how to find the column names similar or do I have to check each and every table? 有没有办法知道如何查找相似的列名,或者我必须检查每个表?

This is for SQL Server 2008 这是针对SQL Server 2008

sys.foreign_keys will contain information about the columns which are explicitly related as keys. sys.foreign_keys将包含与作为键显式相关的列的信息

Aside from that, there is no structural relationship in the schema between any given pair of columns. 除此之外,任何给定列对之间的架构中都没有结构关系。 If any two columns on separate tables are supposed to be connected but aren't actually connected in the schema, that's more of a failure of the schema than anything else. 如果应该将单独表上的任何两列连接在一起,但实际上在架构中并未将它们连接起来,那么架构失败比其他任何事情都更多。

There is no mechanism which will reveal what columns the system's creators intended to be related but didn't actually relate. 没有机制可以揭示系统创建者打算关联哪些列,但实际上并未关联。

My personal favorite is to look at column names if the FK method doesnt pan out. 我个人最喜欢的是查看列名(如果FK方法不能成功显示)。

SELECT c.name AS column_
, t.name AS table_

FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
WHERE c.name LIKE '%userID%'
ORDER BY t.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM