Oracle查询表结构的一些相关语句
Oracle查询表结构的一些相关语句
select * from all_tab_comments;
--查询所有用户的表、视图等
select * from user_tab_comments;
--查询当前用户的所有表、视图等
select * from all_col_comments;
--查询所有用户的表的列名和注释
select * from user_col_comments;
--查询当前用户的表的列名和注释
select * from all_tab_columns;
--查询所有用户的表的列的结构信息(没有备注)
select * from user_tab_columns;
--查询当前户的表的列的结构信息(没有备注)
--查询某张表的相信信息(包括列名、数据类型、长度、是否为空、是否主键、是否外键、备注)
SELECT USER_TAB_COLS.COLUMN_NAME as 列名 , USER_TAB_COLS.DATA_TYPE as 数据类型, USER_TAB_COLS.DATA_LENGTH as 长度, USER_TAB_COLS.NULLABLE as 是否为空, case
when (SELECT col.column_name FROM user_constraints con, user_cons_columns col where con.constraint_name = col.constraint_name and con.constraint_type='P' and col.table_name = '表名' and col.column_name = USER_TAB_COLS.COLUMN_NAME) = USER_TAB_COLS.COLUMN_NAME THEN 'Y'
ELSE 'N'
END
as 是否主键,
case when (select count(*) from user_constraints con, user_cons_columns col, (select t2.table_name,t2.column_name,t1.r_constraint_name from user_constraints t1,user_cons_columns t2 where t1.r_constraint_name=t2.constraint_name and t1.table_name='表名') r where con.constraint_name=col.constraint_name and con.r_constraint_name=r.r_constraint_name and con.table_name='表名' and col.column_name = USER_TAB_COLS.COLUMN_NAME) > 0 then 'Y'
else 'N'
END
as 是否外键
, user_col_comments.comments as 备注 FROM USER_TAB_COLS inner 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 and USER_TAB_COLS.TABLE_NAME = '表名' ORDER BY USER_TAB_COLS.COLUMN_ID;