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

时间:2016-01-13 05:23:15

标签: sql sql-server

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

3 个答案:

答案 0 :(得分:1)

在SQL Server中,在数据库中创建一个新视图,添加要查看其关系的两个表。

enter image description here

答案 1 :(得分:1)

如果你想检查所有表连接到表的内容,只需右键单击表并转到查看依赖项

View dependencies of a table 并找出答案。您可以通过选中该表的创建查询来检查所有约束。

check all table label constraints in its query

我认为它会帮助你。谢谢

答案 2 :(得分:1)

您可以将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'

可以通过引用的表或列过滤此查询,或只是查找引用特定列的所有内容。