查询表 字段名称、数据类型、是否为空、默认值、主键、外键

一,查询表基本信息

select
utc.column_name,utc.data_type,utc.data_length,utc.data_precision,
utc.data_Scale,utc.nullable,utc.data_default,ucc.comments
from
user_tab_columns utc,user_col_comments ucc
where
utc.table_name = ucc.table_name
and utc.column_name = ucc.column_name
and utc.table_name = 'ONLINEXLS'
order by
column_id

注意:order by column_id的意义是使得结果按照设计数据结构时的顺序显示。

二,查询表主键

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='ONLINEXLS'
三,查询表外键

select
distinct(ucc.column_name) column_name,rela.table_name,rela.column_name column_name1
from
user_constraints uc,user_cons_columns ucc,
(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='ONLINEXLS') rela
where
uc.constraint_name=ucc.constraint_name
and uc.r_constraint_name=rela.r_constraint_name
and uc.table_name='ONLINEXLS'

 

有了上述几个SQL语句,再结合程序(如:java+jxl),就可以输出如下图所示的Excel报表了。


1、查找表的所有索引(包括索引名,类型,构成列):

select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查询的表

2、查找表的主键(包括名称,构成列)表名大写 :

select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = '要查询的表' ;

仅 查询表主键
select column_name from user_cons_columns wherE CONSTRAINT_NAME in (select CONSTRAINT_NAME from user_constraints where table_name =upper('表名') and CONSTRAINT_TYPE='P');


3、查找表的唯一性约束(包括名称,构成列):

select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查询的表

4、查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询):

select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表

查询外键约束的列名:

select * from user_cons_columns cl where cl.constraint_name = 外键名称

查询引用表的键的列名:

select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名

5、查询表的所有列及其属性

select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查询的表

来源:http://hi.baidu.com/iheejun23/blog/item/b2142b0085f9a6057aec2cb4.html

你可能感兴趣的:(主键,表结构,字段名称)