设为首页 收藏本站
查看: 660|回复: 0

[经验分享] Oracle中查询表的信息

[复制链接]

尚未签到

发表于 2016-7-27 11:14:22 | 显示全部楼层 |阅读模式
查询oracle表的信息:http://hi.baidu.com/winlei/blog/item/01ef088817852293a5c27209.html
http://topic.csdn.net/u/20070424/09/013c794e-3548-4a50-b9b7-606fef460348.html
根据表名查询外键表,外键名称
select u1.CONSTRAINT_NAME, u1.TABLE_NAME as P_TABLE, u2.TABLE_NAME
from user_constraints u1, user_constraints u2  
where   
u1.constraint_type='R' and  
u1.R_CONSTRAINT_NAME = u2.CONSTRAINT_NAME and  
u2.table_name='HR_TITLE'

select b.CONSTRAINT_NAME as Foreign_Key,b.COLUMN_NAME,u3.TABLE_NAME as Refer_Table,u3.COLUMN_NAME as Refer_Column,'' as remark--,au.*
from user_cons_columns b, user_constraints au ,
(select u1.CONSTRAINT_NAME, u1.TABLE_NAME as P_TABLE, u2.TABLE_NAME,u3.COLUMN_NAME
from user_constraints u1, user_constraints u2  ,user_cons_columns u3
where   
u1.constraint_type='R' and  
u1.R_CONSTRAINT_NAME = u2.CONSTRAINT_NAME and  u2.CONSTRAINT_NAME=u3.CONSTRAINT_NAME
) u3
where b.constraint_name = au.constraint_name
and b.CONSTRAINT_NAME=u3.CONSTRAINT_NAME
and au.constraint_type = 'R'
and au.table_name ='HRST_PERSONAL_INFO'

查询用户所有表
select   table_name   from   user_tables;
select   table_name   from   dba_tables   where   owner= 'USERNAME ';

在Oracle中改如何以下的SQL语句:
1.   查看一个用户下的所有序列。
2.   查看一个用户下的所有存储过程。
3.   查看一个用户下的所有触发器。
select   object_name   from   dba_objects   where   owner= ' '   and   object_type   in   ( 'SEQUENCE ', 'PROCEDURE ', "TRIGGER ')

通过搜索摸索,总结了一下oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助:
1、查询出所有的用户表
select   *   from   user_tables   可以查询出所有的用户表
通过表名过滤需要将字母作如下处理
select * from user_tables where table_name = upper('表名')
因为无论你建立表的时候表名名字是大写还是小写的,create语句执行通过之后,对应的user_tables表中的table_name字段都会自动变为大写字母,所以必须通过内置函数upper将字符串转化为大写字母进行查询,否则,即使建表语句执行通过之后,通过上面的查询语句仍然查询不到对应的记录。
2、查询出用户所有表的索引
select   *   from   user_indexes
3、查询用户表的索引(非聚集索引):
select   *   from   user_indexes where   uniqueness='NONUNIQUE'
4、查询用户表的主键(聚集索引):
select   *   from   user_indexes where   uniqueness='UNIQUE'
5、查询表的索引
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and
t.table_name='NODE'
6、查询表的主键
select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and
au.constraint_type = 'P' AND cu.table_name = 'NODE'
7、查找表的唯一性约束(包括名称,构成列):
select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name=au.constraint_name and
cu.table_name='NODE'
8、查找表的外键
select * from user_constraints c where c.constraint_type = 'R' and c.table_name='STAFFPOSITION'
查询外键约束的列名:
select * from user_cons_columns cl where cl.constraint_name = 外键名称
查询引用表的键的列名:
select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名
9、查询表的所有列及其属性
方法一:
select * from user_tab_columns where table_name=upper('表名');
方法二:
select cname,coltype,width from col where tname=upper('表名');;


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 = 要查询的表
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 = 要查询的表

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-250221-1-1.html 上篇帖子: Oracle(递归查询) Connect By简单用法 下篇帖子: oracle 常用命令(适合初学者)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表