Oracle获取表结构信息:表名、是否视图、字段名、类型、长度、非空、主键

时间:2022-09-11 13:46:14
select
       a.TABLE_NAME as "TableName",
       case when (select count(*) from user_views v where v.VIEW_NAME =a.TABLE_NAME )>0 then 'V' else 'U'end as "TableType",
       a.COLUMN_NAME as "ColumnName",
       A.COLUMN_ID as "ColumnIndex",
       a.DATA_TYPE as "DataType",
       case  
         when a.DATA_TYPE = 'NUMBER' then
           case when a.Data_Precision is null then
             a.Data_Length
             else 
               a.Data_Precision
               end 
         else
          a.Data_Length
       end as "Length",
       case when a.nullable = 'N' then  '0' else '1' end as "IsNullable",
       b.comments as "Description", 
       case
          when (select count(*) from user_cons_columns c 
            where c.table_name=a.TABLE_NAME and c.column_name=a.COLUMN_NAME and c.constraint_name=
                (select d.constraint_name from user_constraints d where d.table_name=c.table_name and d.constraint_type   ='P')
                 )>0 then '1' else '0'end as "IsPK"
            
  from USER_TAB_COLS a,
       sys.user_col_comments b
 where a.table_name = b.table_name      
       and b.COLUMN_NAME = a.COLUMN_NAME       
       order by a.TABLE_NAME, a.COLUMN_ID