1、查询表的所有列及其属性
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 = 要查询的表
2、查找表的所有索引(包括索引名,类型,构成列)
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 = 要查询的表
形成创建索引的sql语句(可以直接使用):
select ‘CREATE ‘ ||un||‘ INDEX ‘||INDEX_NAME||‘ on ‘||TABLE_NAME||‘ (‘||ltrim(max(sys_connect_by_path(COLUMN_NAME, ‘,‘)),‘,‘)||‘)‘ from(select t.table_name,t.INDEX_NAME,t.COLUMN_POSITION,t.COLUMN_NAME,case when i.UNIQUENESS =‘UNIQUE‘ then ‘UNIQUE ‘ else ‘ ‘ end un, 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 = 表名 and not exists(select 1 from user_cons_columns c where c.constraint_name = t.index_name and c.table_name = t.table_name)) start with COLUMN_POSITION = 1 connect by COLUMN_POSITION- 1 = PRIOR COLUMN_POSITION and INDEX_NAME = PRIOR INDEX_NAME group by table_name,INDEX_NAME,un;
去除了创建主键或者创建含有blod列系统自动形成的索引。同时,在创建索引的过程中也考虑到列的顺序。使用了sys_connect_by_path(),很好的列合并工具。
3、查找表的主键(包括名称,构成列)
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 = 要查询的表
形成创建主键的sql语句(直接可以使用):
SELECT ‘ALTER TABLE ‘ || TABLE_NAME || ‘ ADD CONSTRAINT ‘ || CONSTRAINT_NAME || ‘ PRIMARY KEY (‘ || ltrim(max(sys_connect_by_path(COLUMN_NAME, ‘,‘)),‘,‘) || ‘)‘ FROM(select cu.table_name,cu.constraint_name,cu.column_name,cu.position from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = ‘P‘ and au.table_name = 表名 )start with position = 1 connect by position- 1 = PRIOR position and CONSTRAINT_NAME = PRIOR CONSTRAINT_NAME group by table_name,constraint_name;
在创建主键的过程中考虑了主键列的顺序。使用了sys_connect_by_path(),很好的列合并工具。
oracle中查看当前用户的表结构、主键、索引,布布扣,bubuko.com
原文:http://www.cnblogs.com/iceriver315/p/3708079.html