Oracle查询一个命名空间下所有表和视图的表名、字段名、字段类型、字段大小,是否可为NULL,主键和注释信息

使用SQL查询Oracle一个命名空间下所有表和视图的表名、字段名、字段类型、字段大小,是否可为NULL,主键和注释信息。

SQL如下,注意需要将'CDFLOOD'更换为您要查询的命名空间:

select 
user_tab_cols.table_name 表名,
user_tab_cols.column_name 字段名,
user_tab_cols.data_type 类型,
user_tab_cols.data_length 大小,
user_tab_cols.nullable 是否可为NULL,
CASE
WHEN tpk.COLUMN_NAME  = user_tab_cols.column_name THEN 'Y'  
ELSE 'N' END 是否主键,
user_col_comments.COMMENTS 含意
from user_tab_cols
left join (
-- 查询各表主键字段名称
select table_info.TABLE_NAME,dba_cons_columns.COLUMN_NAME from (
select table_name from user_tab_cols group by table_name order by table_name) table_info
left join dba_constraints on table_info.TABLE_NAME = dba_constraints.TABLE_NAME and dba_constraints.owner='CDFLOOD' and dba_constraints.constraint_type = 'P'
left join dba_cons_columns on dba_constraints.CONSTRAINT_NAME = dba_cons_columns.CONSTRAINT_NAME and dba_constraints.TABLE_NAME = dba_cons_columns.TABLE_NAME and dba_constraints.owner='CDFLOOD' order by TABLE_NAME) tpk on tpk.TABLE_NAME = user_tab_cols.table_name and tpk.COLUMN_NAME = user_tab_cols.column_name
-- 查询字段注释
left join user_col_comments on user_col_comments.TABLE_NAME = user_tab_cols.table_name and user_col_comments.COLUMN_NAME = user_tab_cols.column_name
order by user_tab_cols.table_name,user_tab_cols.column_id;

查询结果如下(值中,'Y'表示是,'N'表示否):

posted @ 2019-08-15 15:46  代码猫  阅读(1376)  评论(0编辑  收藏  举报