oracle查看当前用户表结构、主键、索引
2024-09-04 23:56:44
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(),很好的列合并工具。
最新文章
- Android开发之XUtils框架使用和报错处理
- 文件大小K、M、G、T
- 02、AngularJs的数据绑定
- iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
- 反编译CHM文件
- 启动Tomcat出现Using CATALINA_BASE
- winform 对话框控件,打印控件
- 国外.net学习资源网站
- QT 4.7支持中文(QT4.7)(中文)(makeqpf)
- node.JS中配置http-server
- Mysql表名区分大小写
- 微信小程序之:wepy(二)
- Linux一些常用操作命令
- 阿里云服务器搭建SS代理教程!!!
- g4e基础篇#5 创建分支和保存代码
- blackeye部署
- U3D学习004——核心类和代码运行
- js文件,同样的路径,拷贝过来的为什么不能访问
- lodop 代码注释
- [arc068E]Snuke Line-[树状数组]