oracle和mysql获取表名及其注释

404 阅读2分钟

返回主页

Oracle中查看所有表和字段以及表注释.字段注释

获取表:

复制代码

select table_name from user_tables; //当前用户拥有的表,实际测试发现大多是系统表,没找到当前用户表      
 
select table_name from all_tables; //所有用户的表,可用 
select table_name from dba_tables; //包括系统表,可用
select table_name from dba_tables where owner='用户名' and table_name like '表头%'//可以模糊查询用户下某类表
 
user_tables:
table_name,tablespace_name,last_analyzed等
dba_tables:
ower,table_name,tablespace_name,last_analyzed等
all_tables:
ower,table_name,tablespace_name,last_analyzed等
all_objects:
ower,object_name,subobject_name,object_id,created,last_ddl_time,timestamp,status等

复制代码

获取表字段:

复制代码

select * from user_tab_columns where Table_Name='用户表';
select * from all_tab_columns where Table_Name='用户表';
select * from dba_tab_columns where Table_Name='用户表';
user_tab_columns:
table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等
all_tab_columns :
ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等
dba_tab_columns:
ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等

复制代码

获取表注释:

复制代码

select * from user_tab_comments
user_tab_comments:table_name,table_type,comments
相应的还有dba_tab_comments,all_tab_comments,这两个比user_tab_comments多了ower列。
获取字段注释:
select * from user_col_comments
user_col_comments:table_name,column_name,comments
​
相应的还有dba_col_comments,all_col_comments,这两个比user_col_comments多了ower列。
​
可能有多个用户有相同的表名,需要单独区分
select t.table_name,d.comments from dba_tables t
left join dba_tab_comments d on d.table_name = t.table_name where t.owner = '用户' and d.owner = '用户'

查询mysql所有表数据、字段信息

根据库名获取所有表的信息

复制代码

SELECT
    *
FROM
    information_schema.`TABLES`
WHERE
    TABLE_SCHEMA = 'erp';

复制代码

根据库名获取所有表名称和表说明

复制代码

SELECT
    TABLE_NAME,
    TABLE_COMMENT
FROM
    information_schema.`TABLES`
WHERE
    TABLE_SCHEMA = 'erp';

根据库名获取所有字段名称和字段说明

复制代码

SELECT
    TABLE_NAME,
    COLUMN_NAME,
    COLUMN_COMMENT
FROM
    information_schema.`COLUMNS`
WHERE
    TABLE_SCHEMA = '用户' AND TABLE_NAME = '表名';
SELECT 
    t.TABLE_NAME AS '表名',
    t.TABLE_COMMENT AS '表中文名',
    c.COLUMN_NAME AS '字段名', 
    c.COLUMN_TYPE AS '数据类型',
    c.IS_NULLABLE AS '是否为空',
    c.COLUMN_DEFAULT AS '默认值',
    c.COLUMN_COMMENT AS '字段中文名',
    c.ORDINAL_POSITION AS '字段顺序'
FROM 
    information_schema.`TABLES` t
JOIN 
    information_schema.`COLUMNS` c ON t.TABLE_NAME = c.TABLE_NAME 
    AND t.TABLE_SCHEMA = c.TABLE_SCHEMA
WHERE 
    t.TABLE_SCHEMA = '数据库名' 
    AND t.TABLE_NAME = '表名'
ORDER BY 
    c.ORDINAL_POSITION;
1.  ‌**关联查询**‌:通过JOIN连接TABLES和COLUMNS两个系统表
1.  ‌**表中文名**‌:从TABLES表的TABLE_COMMENT字段获取表的注释(中文名)
1.  ‌**字段中文名**‌:从COLUMNS表的COLUMN_COMMENT字段获取字段注释
1.  ‌**完整信息**‌:包含表名、表注释、字段名、数据类型、是否为空、默认值、字段注释和字段顺序
1.  ‌**排序显示**‌:按照字段在表中的实际位置排序