通过Oracle中的user_tab_cols, user_col_comments, user_constraints, user_cons_columns表联合查询。
user_tab_cols用来获取对应用户表的列信息;
user_col_comments用来获取对应用户表列的注释信息;
user_constraints用来获取用户表的约束条件;
user_cons_columns约束中用户可访问列。
select a.column_id as 列号, a.column_name as 列名, a.data_type as 类型,
decode(a.data_type,'NUMBER',a.data_precision,a.data_length) as 长度, a.data_scale as 小数位,
decode(e.uniqueness,'UNIQUE','Y','N') as 是否是唯一的, decode(e.key,'Y','Y','N') 是否是主键,
f.comments as 注释, a.nullable as 是否允许空, a.data_default as 默认值
from user_tab_columns a, user_col_comments f,
(select b.table_name, b.index_name,b.uniqueness, c.column_name,
decode(d.constraint_name,NULL,'N','Y') key
from user_indexes b, user_ind_columns c,
( select constraint_name from user_constraints where constraint_type='P' ) d
where b.index_name=c.index_name and b.index_name=d.constraint_name(+) ) e
where a.table_name='TEMPTABLE' and a.table_name=e.table_name(+) and a.column_name=e.column_name(+)
and a.table_name=f.table_name and a.column_name=f.column_name
order by a.column_id
select TC.table_name , TC.column_name ,TC.DATA_TYPE,TC.data_length,TC.data_precision,TC.nullable,
TC.char_col_decl_length,
decode(C.constraint_type,'P','PK','U','Unique','R','FK','C','Check',C.constraint_type)
from USER_TAB_COLUMNS TC,USER_CONS_COLUMNS CC ,user_constraints C
where TC.table_name = 'EMP'
and TC.table_name = CC.table_name(+)
and TC.column_name = CC.column_name (+)
and CC.constraint_name = C.constraint_name(+)
select A.column_name 字段名,A.data_type 数据类型,A.data_length 长度,A.data_precision
整数位, A.Data_Scale 小数位,A.nullable 允许空值,A.Data_default 缺省值,B.comments
备注 from user_tab_columns A,user_col_comments B where a.COLUMN_NAME=b.column_name and A.Table_Name = B.Table_Name and A.Table_Name='TEST'