为SQL Server数据库生成数据字典

发布于 2024-11-17 15:25:00 字数 84 浏览 6 评论 0原文

我正在尝试为数据库中的表生成数据字典。

理想情况下,我想导出列名称、数据类型、限制和扩展属性描述。

如何才能实现这一目标?

I am trying to generate a data dictionary for a table in my database.

Ideally I would like to export the column names, data type, restrictions and extended property descriptions.

How can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巷雨优美回忆 2024-11-24 15:25:00

您可以尝试此查询:

SELECT
    IC.COLUMN_NAME,
    IC.Data_TYPE,
    EP.[Value] as [MS_Description],
    IKU.CONSTRAINT_NAME, 
    ITC.CONSTRAINT_TYPE,
    IC.IS_NULLABLE
 FROM
    INFORMATION_SCHEMA.COLUMNS IC
    INNER JOIN sys.columns sc ON OBJECT_ID(QUOTENAME(IC.TABLE_SCHEMA) + '.' + QUOTENAME(IC.TABLE_NAME)) = sc.[object_id] AND IC.COLUMN_NAME = sc.name
    LEFT OUTER JOIN sys.extended_properties EP ON sc.[object_id] = EP.major_id AND sc.[column_id] = EP.minor_id AND EP.name = 'MS_Description' AND EP.class = 1 
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE IKU ON IKU.COLUMN_NAME = IC.COLUMN_NAME and IKU.TABLE_NAME = IC.TABLE_NAME and IKU.TABLE_CATALOG = IC.TABLE_CATALOG
    LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ITC ON ITC.TABLE_NAME = IKU.TABLE_NAME and ITC.CONSTRAINT_NAME = IKU.CONSTRAINT_NAME
WHERE IC.TABLE_CATALOG = 'Database'
  and IC.TABLE_SCHEMA = 'dbo'
  and IC.TABLE_NAME = 'Table'
order by IC.ORDINAL_POSITION

或架构文档生成器,例如 Dataedo (我是其产品经理)

You can try this query:

SELECT
    IC.COLUMN_NAME,
    IC.Data_TYPE,
    EP.[Value] as [MS_Description],
    IKU.CONSTRAINT_NAME, 
    ITC.CONSTRAINT_TYPE,
    IC.IS_NULLABLE
 FROM
    INFORMATION_SCHEMA.COLUMNS IC
    INNER JOIN sys.columns sc ON OBJECT_ID(QUOTENAME(IC.TABLE_SCHEMA) + '.' + QUOTENAME(IC.TABLE_NAME)) = sc.[object_id] AND IC.COLUMN_NAME = sc.name
    LEFT OUTER JOIN sys.extended_properties EP ON sc.[object_id] = EP.major_id AND sc.[column_id] = EP.minor_id AND EP.name = 'MS_Description' AND EP.class = 1 
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE IKU ON IKU.COLUMN_NAME = IC.COLUMN_NAME and IKU.TABLE_NAME = IC.TABLE_NAME and IKU.TABLE_CATALOG = IC.TABLE_CATALOG
    LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ITC ON ITC.TABLE_NAME = IKU.TABLE_NAME and ITC.CONSTRAINT_NAME = IKU.CONSTRAINT_NAME
WHERE IC.TABLE_CATALOG = 'Database'
  and IC.TABLE_SCHEMA = 'dbo'
  and IC.TABLE_NAME = 'Table'
order by IC.ORDINAL_POSITION

or schema documentation generator like Dataedo (which I am the Product Manager of).

凉月流沐 2024-11-24 15:25:00

您可以通过 SELECT * FROM INFORMATION_SCHEMA.COLUMNS 和使用 fn_listextendedproperty

You can get at this via a combination of SELECT * FROM INFORMATION_SCHEMA.COLUMNS and using fn_listextendedproperty.

北笙凉宸 2024-11-24 15:25:00

要生成 SQL Server 数据库的数据字典,我建议您使用 ERBuilder 数据建模器,只需按照以下步骤操作:

要生成 ER 图,必须首先对数据库进行逆向工程,选择:菜单 ->文件->逆向工程 ER 图将显示在 ERBuilder 中。
要生成数据库的数据字典,请选择:菜单 ->工具->生成模型文档

To generate a data dictionary of your SQL Server database, I suggest you use ERBuilder data modeller, just follow these steps:

To generate an ER diagram it is necessary to first, reverse engineer your database select: Menu -> File -> Reverse Engineer the ER diagram will be displayed in ERBuilder.
To generate a data dictionary of your database select: Menu -> Tool -> Generate model documentation

辞别 2024-11-24 15:25:00

由于这是“mssql 数据字典”的第一个搜索结果,因此这里有一个简单的查询来获取数据库中的表和列的列表 -

select table_name, ordinal_position, column_name, data_type 
from information_schema.columns 
order by table_name, ordinal_position;

给出例如

"table_name","ordinal_position","column_name","data_type"
Account,1,Account,varchar
Account,2,Type,varchar
Account,3,Description,varchar
Account,4,Rollup_Account,varchar
Account,5,Last_Updated,datetime
Account,6,Section_Name,varchar
Account,7,QB_ID,varchar
Additional_Charge,1,Additional_ChargeKey,int
Additional_Charge,2,Additional_Charge,int
...

Since this is the first search result for 'mssql data dictionary', here's a simple query to get the list of tables and columns in a db -

select table_name, ordinal_position, column_name, data_type 
from information_schema.columns 
order by table_name, ordinal_position;

giving eg

"table_name","ordinal_position","column_name","data_type"
Account,1,Account,varchar
Account,2,Type,varchar
Account,3,Description,varchar
Account,4,Rollup_Account,varchar
Account,5,Last_Updated,datetime
Account,6,Section_Name,varchar
Account,7,QB_ID,varchar
Additional_Charge,1,Additional_ChargeKey,int
Additional_Charge,2,Additional_Charge,int
...
悲凉≈ 2024-11-24 15:25:00

此查询(改编自 Bad Pit 的答案)给出数据库中的所有“dbo”表(名为“myDatabaseName”):

declare @DBNAME varchar(128) ='myDatabaseName';   -- put your database name here

SELECT
    concat(IC.TABLE_NAME, '.', IC.COLUMN_NAME, ' (', ic.DATA_TYPE, ')', (case(itc.constraint_type) when('PRIMARY KEY') then (' PRIMARY KEY') else ('') end), (case(ic.IS_NULLABLE) when('YES') then (' NULLABLE') else ('') end)) as 'Data Dictionary'
 
 FROM
    INFORMATION_SCHEMA.COLUMNS IC
    INNER JOIN sys.columns sc ON OBJECT_ID(QUOTENAME(IC.TABLE_SCHEMA) + '.' + QUOTENAME(IC.TABLE_NAME)) = sc.[object_id] AND IC.COLUMN_NAME = sc.name
    LEFT OUTER JOIN sys.extended_properties EP ON sc.[object_id] = EP.major_id AND sc.[column_id] = EP.minor_id AND EP.name = 'MS_Description' AND EP.class = 1 
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE IKU ON IKU.COLUMN_NAME = IC.COLUMN_NAME and IKU.TABLE_NAME = IC.TABLE_NAME and IKU.TABLE_CATALOG = IC.TABLE_CATALOG
    LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ITC ON ITC.TABLE_NAME = IKU.TABLE_NAME and ITC.CONSTRAINT_NAME = IKU.CONSTRAINT_NAME
    WHERE
    
    IC.TABLE_CATALOG = @DBNAME  

    and IC.TABLE_SCHEMA = 'dbo'

order by IC.TABLE_NAME, IC.ORDINAL_POSITION
;```

This query (adapted from Bad Pit's answer) gives all the 'dbo' tables in your database (named 'myDatabaseName'):

declare @DBNAME varchar(128) ='myDatabaseName';   -- put your database name here

SELECT
    concat(IC.TABLE_NAME, '.', IC.COLUMN_NAME, ' (', ic.DATA_TYPE, ')', (case(itc.constraint_type) when('PRIMARY KEY') then (' PRIMARY KEY') else ('') end), (case(ic.IS_NULLABLE) when('YES') then (' NULLABLE') else ('') end)) as 'Data Dictionary'
 
 FROM
    INFORMATION_SCHEMA.COLUMNS IC
    INNER JOIN sys.columns sc ON OBJECT_ID(QUOTENAME(IC.TABLE_SCHEMA) + '.' + QUOTENAME(IC.TABLE_NAME)) = sc.[object_id] AND IC.COLUMN_NAME = sc.name
    LEFT OUTER JOIN sys.extended_properties EP ON sc.[object_id] = EP.major_id AND sc.[column_id] = EP.minor_id AND EP.name = 'MS_Description' AND EP.class = 1 
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE IKU ON IKU.COLUMN_NAME = IC.COLUMN_NAME and IKU.TABLE_NAME = IC.TABLE_NAME and IKU.TABLE_CATALOG = IC.TABLE_CATALOG
    LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ITC ON ITC.TABLE_NAME = IKU.TABLE_NAME and ITC.CONSTRAINT_NAME = IKU.CONSTRAINT_NAME
    WHERE
    
    IC.TABLE_CATALOG = @DBNAME  

    and IC.TABLE_SCHEMA = 'dbo'

order by IC.TABLE_NAME, IC.ORDINAL_POSITION
;```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文