查询数据库内表的字段及注释

内容:

获取数据库表字段、字段类型、字段注释等。现已汇总数据库:SqlServer、MySql、Oracle


SqlServer数据库

TableName:表名;
ColumnName:字段名;
ColumnType:字段类型;
IsNullable:是否能为空;
DefaultData:默认值;
ColumnDescription:字段注释;

查询某数据库某张表的字段与注释

use 数据库名
select a.name  TableName,b.name  ColumnName,data_type ColumnType,s.IS_NULLABLE IsNullable,s.COLUMN_DEFAULT DefaultData,C.value  ColumnDescription
from sys.tables a 
inner join sys.columns b on b.object_id = a.object_id
left join information_schema.columns s on s.TABLE_NAME = a.name and s.COLUMN_NAME = b.name
left join sys.extended_properties c on c.major_id = b.object_id and c.minor_id = b.column_id
where a.name ='表名'
Order by a.name,s.ORDINAL_POSITION;

go

在这里插入图片描述

查询某数据库所有表的字段与注释

use 数据库名
select a.name  TableName,b.name  ColumnName,data_type ColumnType,s.IS_NULLABLE IsNullable,s.COLUMN_DEFAULT DefaultData,C.value  ColumnDescription
from sys.tables a 
inner join sys.columns b on b.object_id = a.object_id
left join information_schema.columns s on s.TABLE_NAME = a.name and s.COLUMN_NAME = b.name
left join sys.extended_properties c on c.major_id = b.object_id and c.minor_id = b.column_id
where a.name in (select name from sys.tables)
Order by a.name,s.ORDINAL_POSITION;

go

在这里插入图片描述


MySql数据库

DatabaseName:数据库名;
TableName:表名;
ColumnName:字段名;
ColumnType:字段类型;
IsNullable:是否能为空;
DefaultData:默认值;
ColumnDescription:字段注释;

查询某数据库某张表的字段与注释

select a.TABLE_SCHEMA DatabaseName,a.TABLE_NAME TableName,a.COLUMN_NAME ColumnName,a.COLUMN_TYPE ColumnType,a.IS_NULLABLE IsNullable,a.COLUMN_DEFAULT DefaultData,a.COLUMN_COMMENT ColumnDescription
from COLUMNS a
WHERE TABLE_SCHEMA = '数据库名' and TABLE_NAME = '表名'
ORDER BY TABLE_SCHEMA,TABLE_NAME,ORDINAL_POSITION;

在这里插入图片描述

查询某数据库所有表的字段与注释

select a.TABLE_SCHEMA DatabaseName,a.TABLE_NAME TableName,a.COLUMN_NAME ColumnName,a.COLUMN_TYPE ColumnType,a.IS_NULLABLE IsNullable,a.COLUMN_DEFAULT DefaultData,a.COLUMN_COMMENT ColumnDescription
from COLUMNS a
WHERE TABLE_SCHEMA = '数据库名'
ORDER BY TABLE_SCHEMA,TABLE_NAME,ORDINAL_POSITION;

在这里插入图片描述


Oracle数据库

DatabaseName:数据库名;
TableName:表名;
ColumnName:字段名;
ColumnType:字段类型;
IsNullable:是否能为空;
DefaultData:默认值;
ColumnDescription:字段注释;
注意:user_tab_columns表的字段需要全大写

查询某数据库某张表的字段与注释

select a.TABLE_NAME  TableName,a.COLUMN_NAME ColumnName,a.DATA_TYPE ColumnType,a.NULLABLE IsNullable,
a.DATA_DEFAULT DefaultData,b.comments  ColumnDescription
from user_tab_columns a 
left join user_col_comments b on a.TABLE_NAME = b.table_name and a.COLUMN_NAME = b.column_name
where a.TABLE_NAME = '表名'
Order by a.TABLE_NAME,a.COLUMN_ID;

查询某数据库所有表的字段与注释

select a.TABLE_NAME  TableName,a.COLUMN_NAME ColumnName,a.DATA_TYPE ColumnType,a.NULLABLE IsNullable,
a.DATA_DEFAULT DefaultData,b.comments  ColumnDescription
from user_tab_columns a 
left join user_col_comments b on a.TABLE_NAME = b.table_name and a.COLUMN_NAME = b.column_name
Order by a.TABLE_NAME,a.COLUMN_ID ;

### 如何在金仓数据库查询表字段注释 在金仓数据库中,可以通过特定的 SQL 语句来获取字段注释信息。以下是实现这一需求的具体方法: #### 方法一:通过 `sys_description` 和 `sys_attribute` 联查 可以利用系统元数据 `sys_description` 和 `sys_attribute` 来查询表字段及其对应的注释信息。具体 SQL 语句如下: ```sql select a.attnum as 序号, a.attname as 字段名称, a.attlen as 长度, a.atttypmod as 可变长度, case when a.attnotnull = true then 'NOT NULL' else '' end as 是否为空, b.description as 注释 from sys_class c, sys_attribute a left outer join sys_description b on a.attrelid = b.objoid and a.attnum = b.objsubid where c.relname = '目标名' and a.attnum > 0 and a.attrelid = c.oid order by a.attnum; ``` 此语句能够返回指定中的字段名称、长度、可变长度、是否允许为空以及字段注释等信息[^3]。 --- #### 方法二:基于 `user_col_comments` 查看字段注释 如果需要更简洁的方式查看字段注释,也可以尝试以下 SQL 语句: ```sql SELECT column_name AS 字段名称, comments AS 注释 FROM user_col_comments WHERE table_name = '目标名'; ``` 该方式适用于某些版本支持的情况下快速提取字段与其对应注释的关系[^1]。 --- #### 注意事项 - 替换 `'目标名'` 为实际要查询的名。 - 如果发现部分字段注释,则可能是因为这些字段未被显式定义过注释内容。 - 不同版本的金仓数据库可能存在细微差异,在执行前建议确认当前环境所使用的版本特性。 --- ### 示例代码展示 假设有一个名为 `student` 的,下面是一个完整的示例脚本用于打印其所有字段注释: ```sql -- 使用方法一 select a.attnum as 序号, a.attname as 字段名称, a.attlen as 长度, a.atttypmod as 可变长度, case when a.attnotnull = true then 'NOT NULL' else '' end as 是否为空, b.description as 注释 from sys_class c, sys_attribute a left outer join sys_description b on a.attrelid = b.objoid and a.attnum = b.objsubid where c.relname = 'student' and a.attnum > 0 and a.attrelid = c.oid order by a.attnum; -- 或者使用方法二 SELECT column_name AS 字段名称, comments AS 注释 FROM user_col_comments WHERE table_name = 'student'; ``` 以上两种方案均可满足查询需求,可根据实际情况选择适合的一种。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开心,你呢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值