Question

If I execute this query:

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

I will get all indexes for table MY_TABLE. One of them is unique index affecting three columns, which I can check in SQL Developer.

However, from my query results I can't tell which index affects which columns and how many columns are affected.

How should I change my query to get only unique index affecting more then one column along with list of that columns?

Was it helpful?

Solution

Try like this,

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);

OTHER TIPS

You need to join to sys.all_ind_columns to get the columns in the index.

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top