在Oracle中使用不同的列连接两个表

时间:2014-02-15 12:42:38

标签: sql oracle join indexing

我正在尝试根据以下查询获取index的index_name,constraint_type和唯一性:

select constraint_name index_name, 
       'P', 
       'U' 
  from all_constraints 
 where owner = 'owner_name' 
   and table_name = 'table_name' 
   and constraint_type = 'P'
UNION
select index_name, 
       ' ', 
       substr(uniqueness,1,1) 
  from all_indexes
 where owner = 'owner_name' 
   and table_name = 'table_name'
   and index_name not in (
                          select constraint_name 
                            from all_constraints
                           where owner = 'owner_name' 
                             and table_name = 'table_name'
                          )
order by index_name

现在,我需要使用以下查询获取与上述查询中检索到的index_name相对应的column_name:

如果索引是主要(P):

select column_name
from all_cons_columns
where owner = 'owner_name' and constraint_name = 'index_name'

如果index不是主要的:

select column_name
from all_ind_columns
where index_owner = 'owner_name' and index_name = 'index_name'

有什么方法可以加入查询以获取每个index_name旁边的column_name?如果索引构建在多个列上,我需要index_name的重复条目。

我尝试过不同的连接,但似乎没有一个在我的情况下工作。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不知道这是否正是您所需要的。

> SELECT DECODE(PRIM
>              ,'P',(select column_name 
>                           from all_ind_columns 
>                          where index_owner = ta.owner and index_name= ta.index_name)
>              , (select column_name 
>                   from all_cons_columns
>                  where owner = ta.owner and constraint_name = ta.index_name) ), index_name from ( select constraint_name index_name,
>        'P' PRIM,
>        'U' UNIQ,
>        owner,
>        table_name   from all_constraints  where constraint_type = 'P'    and owner = 'OWNER_NAME'    and table_name = 'TABLE_NAME' UNION select
> index_name,
>        ' ',
>        substr(uniqueness,1,1),
>        owner,
>        table_name   from all_indexes  where owner = 'OWNER_NAME'    and table_name = 'TABLE_NAME'
>     and index_name not in (
>                           select constraint_name
>                             from all_constraints
>                            where owner = 'OWNER_NAME'
>                              and table_name = 'TABLE_NAME'
>                           ) order by index_name ) TA