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(),很好的列合并工具。

最新文章

  1. Android开发之XUtils框架使用和报错处理
  2. 文件大小K、M、G、T
  3. 02、AngularJs的数据绑定
  4. iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
  5. 反编译CHM文件
  6. 启动Tomcat出现Using CATALINA_BASE
  7. winform 对话框控件,打印控件
  8. 国外.net学习资源网站
  9. QT 4.7支持中文(QT4.7)(中文)(makeqpf)
  10. node.JS中配置http-server
  11. Mysql表名区分大小写
  12. 微信小程序之:wepy(二)
  13. Linux一些常用操作命令
  14. 阿里云服务器搭建SS代理教程!!!
  15. g4e基础篇#5 创建分支和保存代码
  16. blackeye部署
  17. U3D学习004——核心类和代码运行
  18. js文件,同样的路径,拷贝过来的为什么不能访问
  19. lodop 代码注释
  20. [arc068E]Snuke Line-[树状数组]

热门文章

  1. MATLAB探索初步问题汇总
  2. ActiveMQ-5.9-笔记-02
  3. Docker——常用命令
  4. snort规则
  5. Python的安装与开发环境的选用
  6. 网络编程-Python的socket库
  7. Oracle SQL Developer.exe双击启动错误信息dll未找到
  8. A5/web项目连接Oracle 12c数据库报:ORA-01017: 用户名/口令无效
  9. ArrayList 与 LinkedList 的不区别?
  10. 云计算:Ubuntu下Vue+Springboot前后端分离项目部署(多节点)