SQL Server::从没有约束的数据库创建ERD

我有一个给我们的数据库,没有约束,没有PK或FK。

我正在尝试反向工程,以找出表之间的连接。为此,我的方法是创建具有相同名称和数据类型的所有列的列表。所以我创建了这个查询:

SELECT schema_name(tab.schema_id) AS schema_name
    ,tab.name AS table_name
    ,col.name AS column_name
    ,t.name AS data_type
    ,SUM([Partitions].[rows]) AS [TotalRowCount]
    
FROM sys.tables AS tab
INNER JOIN sys.columns AS col ON tab.object_id = col.object_id
LEFT JOIN sys.types AS t ON col.user_type_id = t.user_type_id
JOIN sys.partitions AS [Partitions] ON tab.[object_id] = [Partitions].[object_id]
    AND [Partitions].index_id IN (
        0
        ,1
        )

GROUP BY schema_name(tab.schema_id)
    ,tab.name
    ,col.name
    ,t.name
    
ORDER BY col.name 

结果是:

相关文章