oracle中如何查询所有用户表的表名、主键名称、索引及外键等

来自:网络
时间:2024-03-31
阅读:
免费资源网 - https://freexyz.cn/

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

附:oracle中查看一张表是否有主键,主键在哪个字段上

1. 利用Oracle中系统自带的两个视图可以实现查看表中主键信息,

语句如下:

select a.constraint_name,  a.column_name 
from user_cons_columns a, user_constraints b 
where a.constraint_name = b.constraint_name 
and b.constraint_type = 'P' and a.table_name = '大写的表名'

查看表的主键约束名称,以及主键约束的字段名称。如果没有,则返回空

2. 顺便给出创建主键和删除主键的sql

向表中添加主键

alter table 表名 add constraint 主键名 primary key(列名);

删除表中已有的主键约束

​​​​​​​alter table 表名 drop constraint 主键名;

总结 

免费资源网 - https://freexyz.cn/
返回顶部
顶部