Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

oracle11g - Create index if not exist

ALL,

SQL> SELECT 1 FROM all_indexes WHERE table_name = UPPER( 'abcatcol' ) AND index_name = UPPER( 'abcatcol_tnam_ownr_cnam' );

no rows selected

SQL> CREATE INDEX abcatcol_tnam_ownr_cnam ON abcatcol(abc_tnam, abc_ownr, abc_cnam);
CREATE INDEX abcatcol_tnam_ownr_cnam ON abcatcol(abc_tnam, abc_ownr, abc_cnam)
                                                 *
ERROR at line 1:
ORA-01408: such column list already indexed


SQL> SELECT 1 FROM all_indexes WHERE table_name = UPPER( 'abcatcol' );

         1
----------
         1

SQL> SELECT index_name FROM all_indexes WHERE table_name = UPPER( 'abcatcol' );

INDEX_NAME
------------------------------
SYS_C007087

SQL >

What am I missing? Why can't I create an index?

EDIT:

SQL> select index_name, listagg(column_name, ', ') within group(order by 1)-- over(partition by index_name)
  2    from dba_ind_columns
  3   where table_name = 'ABCATCOL'
  4   group by index_name;

INDEX_NAME
------------------------------
LISTAGG(COLUMN_NAME,',')WITHINGROUP(ORDERBY1)--OVER(PARTITIONBYINDEX_NAME)
--------------------------------------------------------------------------------
SYS_C007087
ABC_CNAM, ABC_OWNR, ABC_TNAM


SQL> SELECT index_name FROM all_indexes WHERE table_name = UPPER( 'abcatcol' );

INDEX_NAME
------------------------------
SYS_C007087

SQL>

EDIT2:

The suggested question utilizes PL/SQL. I want to understand how to do that using standard SQL and why my queries do not work as expected.

EDIT3:

This is the table definition:

CREATE TABLE abcatcol(abc_tnam char(129) NOT NULL, abc_tid integer, abc_ownr char(129) NOT NULL, abc_cnam char(129) NOT NULL, abc_cid smallint, abc_labl char(254), abc_lpos smallint, abc_hdr char(254), abc_hpos smallint, abc_itfy smallint, abc_mask char(31), abc_case smallint, abc_hght smallint, abc_wdth smallint, abc_ptrn char(31), abc_bmap char(1), abc_init char(254), abc_cmnt char(254), abc_edit char(31), abc_tag char(254), PRIMARY KEY( abc_tnam, abc_ownr, abc_cnam ));

So I guess since those fields are part of the PK Otacle already made the index, right?

question from:https://stackoverflow.com/questions/65934684/create-index-if-not-exist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are looking for an index by it's name whereas oracle says to you this set of columns has been already indexed.

It means there is already an index with another name over that column set.

You need to check against dba_ind_columns table to get the index name over that column set

UPD. Here is the query to help you out to find the columns indexed

select index_name, listagg(column_name, ', ') within group(order by 1)-- over(partition by index_name)
  from dba_ind_columns
 where table_name = 'TABLE_NAME'
 group by index_name;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...