简体   繁体   English

我们如何识别两个SQL Server表之间的关系,一对一或其他关系......?

[英]How can we identify the relationship between two SQL Server tables , either one to one or some other relationship…?

我们如何识别两个SQL Server表之间的关系,一对一或其他一些关系.....?

In SQL Server, create a new view in your database, add the two tables you want to see the relationship with. 在SQL Server中,在数据库中创建一个新视图,添加要查看其关系的两个表。

在此输入图像描述

If you wanna check what all tables are connected to the table, Just right click on table and go to View dependencies 如果您想检查所有表连接到表的内容,只需右键单击表并转到查看依赖项

查看表的依赖关系 and find out. 并找出答案。 And you can check all constrains by checking create query for that table. 您可以通过检查该表的创建查询来检查所有约束。

检查查询中的所有表标签约束

i think it will help you out . 我认为它会帮助你。 thanks 谢谢

You can use Microsoft system views for that purpose: 您可以使用Microsoft系统视图来实现此目的:

SELECT  
    obj.name AS fk
    ,sch.name AS [schema_name]
    ,tabParent.name AS [table]
    ,colParent.name AS [column]
    ,tabRef.name AS [referenced_table]
    ,colRef.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
JOIN sys.tables tabParent ON tabParent.object_id = fkc.parent_object_id
JOIN sys.schemas sch ON tabParent.schema_id = sch.schema_id
JOIN sys.columns colParent ON colParent.column_id = parent_column_id AND colParent.object_id = tabParent.object_id
JOIN sys.tables tabRef ON tabRef.object_id = fkc.referenced_object_id
JOIN sys.columns colRef ON colRef.column_id = referenced_column_id AND colRef.object_id = tabRef.object_id
JOIN sys.schemas schRef ON tabRef.schema_id = schRef.schema_id
WHERE schRef.name = N'dbo'
AND tabRef.name = N'Projects'

It is possible to filter this query by referenced tables or columns, or just to look for everything that references a specific column. 可以通过引用的表或列过滤此查询,或者仅查找引用特定列的所有内容。

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

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