在Oracle中,如何为表和受影响的列选择唯一的索引

时间:2013-09-17 16:00:53

标签: sql oracle indexing unique unique-constraint

如果我执行此查询:

select * 
from SYS.ALL_INDEXES
where table_name='MY_TABLE'
and owner = 'ME'
order by TABLE_OWNER, TABLE_NAME, INDEX_NAME;

我将获取表MY_TABLE的所有索引。其中一个是影响三列的唯一索引,我可以在SQL Developer中查看。

但是,从我的查询结果中,我无法判断哪个索引会影响哪些列以及受影响的列数。

如何更改查询以仅获取影响多列以及列列表的唯一索引?

2 个答案:

答案 0 :(得分:0)

您需要加入sys.all_ind_columns才能获取索引中的列。

select * 
from SYS.ALL_INDEXES
inner join SYS.ALL_IND_COLUMNS
on SYS.ALL_INDEXES.owner = SYS.ALL_IND_COLUMNS.owner
and SYS.ALL_INDEXES.index_name = SYS.ALL_IND_COLUMNS.index_name
where table_name='MY_TABLE'
and owner = 'ME'
order by TABLE_OWNER, TABLE_NAME, INDEX_NAME;

答案 1 :(得分:0)

试试这个,

SELECT i.index_name,  
       c.column_position,  
       c.column_name,  
       i.uniqueness
  FROM sys.all_indexes i, 
       sys.all_ind_columns c  
 WHERE i.table_name  = 'MY_TABLE'  
   AND i.owner       = 'ME'  
   AND i.uniqueness  = 'UNIQUE'
   AND i.index_name  = c.index_name  
   AND i.table_owner = c.table_owner  
   AND i.table_name  = c.table_name  
   AND i.owner       = c.index_owner
   AND c.index_name IN (SELECT index_name FROM sys.all_ind_columns WHERE column_position = 2);