SQL 服务器 :: 从数据库无约束地创建 ERD
SQL Server :: create an ERD from a database without constraints
我有一个数据库,没有任何限制,没有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
结果我得到了这个:
schema_name
table_name
column_name
data_type
TotalRowCount
H513WEB
ALPRETAR
IDREVI
numeric
1439
H513WEB
ALPRETAR
IDREVN
numeric
1439
H513WEB
CPDOENP
IDRLPP
numeric
4156
H513WEB
ALPRETAR
IDRNG
numeric
1439
H513WEB
FAAFACP
IDROLDEBCOM
numeric
100541
H513WEB
FAAFACP_OLD
IDROLDEBCOM
numeric
1513
H513WEB
CACPTAP
IDROLE
numeric
22109
H513WEB
CARROLP
IDROLE
numeric
5
H513WEB
FAENTFP
IDROLE
numeric
26084
H513WEB
CACPTCP
IDRUBRIQUE
numeric
48
H513WEB
CACPTRP
IDRUBRIQUE
numeric
12942
H513WEB
CAIMDCP
IDRUBRIQUE
numeric
6637
H513WEB
CAIMGIP
IDRUBRIQUE
numeric
5
H513WEB
CAIMRSP
IDRUBRIQUE
numeric
40
H513WEB
CAOREXP
IDRUBRIQUE
numeric
5
H513WEB
CAPLARP
IDRUBRIQUE
numeric
3
H513WEB
CARERUP
IDRUBRIQUE
numeric
175
H513WEB
CARUBRP
IDRUBRIQUE
numeric
312
但我只想列出具有相同 column_name
和 data_type
的行,结果应如下所示:
schema_name
table_name
column_name
data_type
TotalRowCount
H513WEB
FAAFACP
IDROLDEBCOM
numeric
100541
H513WEB
FAAFACP_OLD
IDROLDEBCOM
numeric
1513
H513WEB
CACPTAP
IDROLE
numeric
22109
H513WEB
CARROLP
IDROLE
numeric
5
H513WEB
FAENTFP
IDROLE
numeric
26084
H513WEB
CACPTCP
IDRUBRIQUE
numeric
48
H513WEB
CACPTRP
IDRUBRIQUE
numeric
12942
H513WEB
CAIMDCP
IDRUBRIQUE
numeric
6637
H513WEB
CAIMGIP
IDRUBRIQUE
numeric
5
H513WEB
CAIMRSP
IDRUBRIQUE
numeric
40
H513WEB
CAOREXP
IDRUBRIQUE
numeric
5
H513WEB
CAPLARP
IDRUBRIQUE
numeric
3
H513WEB
CARERUP
IDRUBRIQUE
numeric
175
H513WEB
CARUBRP
IDRUBRIQUE
numeric
312
怎么做?
如果您要尝试识别 PK 和 FK 候选项,那么不仅需要匹配数据类型,还需要匹配长度、小数位数和精度。例如,varchar(10)
不能是 varchar(8)
的外键。实际上,名称根本不需要匹配,但如果不包含该名称,您最终会得到大量误报。
我已经猜到了你想要的东西,但我认为这可能是正确的。另一方面,(估计的)行数似乎根本没有必要,所以我从查询中删除了它:
WITH ColumnCount AS(
SELECT s.name AS SchemaName,
t.name AS TableName,
c.name AS ColumnName,
ct.[name] AS DataType,
c.max_length,
c.precision,
c.scale,
COUNT(c.column_id) OVER (PARTITION BY c.[name], ct.[name], c.max_length, c.precision, c.scale) AS Duplicates
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
JOIN sys.types ct ON c.user_type_id = ct.user_type_id)
SELECT *
FROM ColumnCount CC
WHERE CC.Duplicates > 1
ORDER BY CC.ColumnName,
CC.SchemaName,
CC.TableName;
我有一个数据库,没有任何限制,没有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
结果我得到了这个:
schema_name | table_name | column_name | data_type | TotalRowCount |
---|---|---|---|---|
H513WEB | ALPRETAR | IDREVI | numeric | 1439 |
H513WEB | ALPRETAR | IDREVN | numeric | 1439 |
H513WEB | CPDOENP | IDRLPP | numeric | 4156 |
H513WEB | ALPRETAR | IDRNG | numeric | 1439 |
H513WEB | FAAFACP | IDROLDEBCOM | numeric | 100541 |
H513WEB | FAAFACP_OLD | IDROLDEBCOM | numeric | 1513 |
H513WEB | CACPTAP | IDROLE | numeric | 22109 |
H513WEB | CARROLP | IDROLE | numeric | 5 |
H513WEB | FAENTFP | IDROLE | numeric | 26084 |
H513WEB | CACPTCP | IDRUBRIQUE | numeric | 48 |
H513WEB | CACPTRP | IDRUBRIQUE | numeric | 12942 |
H513WEB | CAIMDCP | IDRUBRIQUE | numeric | 6637 |
H513WEB | CAIMGIP | IDRUBRIQUE | numeric | 5 |
H513WEB | CAIMRSP | IDRUBRIQUE | numeric | 40 |
H513WEB | CAOREXP | IDRUBRIQUE | numeric | 5 |
H513WEB | CAPLARP | IDRUBRIQUE | numeric | 3 |
H513WEB | CARERUP | IDRUBRIQUE | numeric | 175 |
H513WEB | CARUBRP | IDRUBRIQUE | numeric | 312 |
但我只想列出具有相同 column_name
和 data_type
的行,结果应如下所示:
schema_name | table_name | column_name | data_type | TotalRowCount |
---|---|---|---|---|
H513WEB | FAAFACP | IDROLDEBCOM | numeric | 100541 |
H513WEB | FAAFACP_OLD | IDROLDEBCOM | numeric | 1513 |
H513WEB | CACPTAP | IDROLE | numeric | 22109 |
H513WEB | CARROLP | IDROLE | numeric | 5 |
H513WEB | FAENTFP | IDROLE | numeric | 26084 |
H513WEB | CACPTCP | IDRUBRIQUE | numeric | 48 |
H513WEB | CACPTRP | IDRUBRIQUE | numeric | 12942 |
H513WEB | CAIMDCP | IDRUBRIQUE | numeric | 6637 |
H513WEB | CAIMGIP | IDRUBRIQUE | numeric | 5 |
H513WEB | CAIMRSP | IDRUBRIQUE | numeric | 40 |
H513WEB | CAOREXP | IDRUBRIQUE | numeric | 5 |
H513WEB | CAPLARP | IDRUBRIQUE | numeric | 3 |
H513WEB | CARERUP | IDRUBRIQUE | numeric | 175 |
H513WEB | CARUBRP | IDRUBRIQUE | numeric | 312 |
怎么做?
如果您要尝试识别 PK 和 FK 候选项,那么不仅需要匹配数据类型,还需要匹配长度、小数位数和精度。例如,varchar(10)
不能是 varchar(8)
的外键。实际上,名称根本不需要匹配,但如果不包含该名称,您最终会得到大量误报。
我已经猜到了你想要的东西,但我认为这可能是正确的。另一方面,(估计的)行数似乎根本没有必要,所以我从查询中删除了它:
WITH ColumnCount AS(
SELECT s.name AS SchemaName,
t.name AS TableName,
c.name AS ColumnName,
ct.[name] AS DataType,
c.max_length,
c.precision,
c.scale,
COUNT(c.column_id) OVER (PARTITION BY c.[name], ct.[name], c.max_length, c.precision, c.scale) AS Duplicates
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
JOIN sys.types ct ON c.user_type_id = ct.user_type_id)
SELECT *
FROM ColumnCount CC
WHERE CC.Duplicates > 1
ORDER BY CC.ColumnName,
CC.SchemaName,
CC.TableName;