Home  >  Article  >  Database  >  oracle 创建主键反向索引

oracle 创建主键反向索引

WBOY
WBOYOriginal
2016-06-07 15:50:491383browse

前言: 今天在学习oracle索引,看到创建“反向索引”就在想那如何在主键上创建反向索引嘞? 思路:1、 是不是在添加主键时使用reverse关键字: SQL alter table emp2 add constraint pk_emp2 primary key (empno) reverse;alter table emp2 add constraint p

前言:今天在学习oracle索引,看到创建“反向索引”就在想那如何在主键上创建反向索引嘞?

思路:1、是不是在添加主键时使用reverse关键字: 

SQL> alter table emp2 add constraint pk_emp2 primary key (empno)  reverse;
alter table emp2 add constraint pk_emp2 primary key (empno)  reverse
                                                             *
ERROR at line 1:
ORA-14125: REVERSE/NOREVERSE may not be specified in this context
从上面看出明显不是这样创建的。

2、看了看文档也没有这样用的方法,但是直接使用lter table emp2 add constraint pk_emp2 primary key (empno)肯定是不对的。

3、好像在创建主键时可以指定一个索引。是不是可以先创建反向索引,然后在创建主键是指定索引呢?那就试一试呗:

SQL> create unique index re_emp2no_idx on emp2(empno) reverse;

Index created.

SQL> alter table emp2 add constraint pk_emp2 primary key (empno) USING INDEX re_emp2no_idx;

Table altered.

SQL> 
SQL> select * from emp2 where empno = 7369;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80       1000                    20


Execution Plan
----------------------------------------------------------
Plan hash value: 478151573

---------------------------------------------------------------------------------------------
| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |               |     1 |    87 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP2          |     1 |    87 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | RE_EMP2NO_IDX |     1 |       |     0   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("EMPNO"=7369)

SQL> select INDEX_NAME, INDEX_TYPE, TABLE_NAME from user_indexes;

INDEX_NAME                     INDEX_TYPE                  TABLE_NAME
------------------------------ --------------------------- ------------------------------
RE_EMP2NO_IDX                  NORMAL/REV                  EMP2
USER_INDEX_TBS                 FUNCTION-BASED NORMAL       EMP
PK_EMP                         NORMAL                      EMP
PK_DEPT                        NORMAL                      DEPT   
   
SQL> SELECT TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE 
FROM all_constraints WHERE TABLE_NAME = 'EMP2';

TABLE_NAME                     CONSTRAINT_NAME                C
------------------------------ ------------------------------ -
EMP2                           PK_EMP2                        P
从上面看出思路对了,也成功创建主键反向索引。

注意:在创建索引的时候一定要创建唯一索引(unique index),否则在使用索引查询时用的是 index range scan

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn