This topic describes how to create an index.

Syntax

CREATE [ UNIQUE ] INDEX name ON table
  ( { column | ( expression ) } )
  [ TABLESPACE tablespace ]

Parameters

Parameter Description
UNIQUE Causes the system to check for duplicate values in the table when the index is created if data already exist and each time data is added. Attempts to insert or update data which would result in duplicate entries will generate an error.
name The name of the index to be created. No schema name can be included here. The index is always created in the same schema as its parent table.
table The name of the table to be indexed. The name can be schema-qualified.
column The name of a column in the table.
expression An expression based on one or more columns of the table. The expression usually must be written with surrounding parentheses, as described in the syntax. However, the parentheses may be omitted if the expression has the form of a function call.
tablespace The tablespace in which to create the index. If not specified, default_tablespace is used. If default_tablespace is an empty string, the default tablespace of the database is used.

Description

CREATE INDEX constructs an index on the specified table. Indexes are primarily used to enhance database performance.

The key fields for the index are specified as column names, or alternatively as expressions written in parentheses. Multiple fields can be specified to create multicolumn indexes.

An index field can be an expression computed from the values of one or more columns of the table row. This feature can be used to obtain fast access to data based on some transformation of the basic data. For example, an index computed on UPPER(col) can allow the clause WHERE UPPER(col) = 'JIM' to use an index.

PolarDB for Oracle provides the B-tree index method. The B-tree index method is an implementation of Lehman-Yao high-concurrency B-trees.

Indexes are not used for IS NULL clauses by default.

All functions and operators used in an index definition must be immutable, that is, their results must depend only on their arguments and never on any outside influence such as the contents of another table or the current time. This restriction ensures that the behavior of the index is well-defined. To use a user-defined function in an index expression, remember to mark the function immutable when you create it.

If you create an index on a partitioned table, the CREATE INDEX statement does not propagate indexes to the subpartitions of the table.
  • If you specify the name of the partitioned root, all indexes of partitions and subpartitions of the table are created.
  • If you specify the name of the partitioned backup table, all indexes of subpartitions in the partition of the table are created.
  • If you specify the name of the subpartitioned backup table, only the index of the subpartition of the table is created.
Note Up to 32 fields may be specified in a multicolumn index.

Examples

To create a B-tree index on the ename column in the table emp:

CREATE INDEX name_idx ON emp (ename);

To create the same index as the preceding index, but have it reside in the index_tblspc tablespace:

CREATE INDEX name_idx ON emp (ename) TABLESPACE index_tblspc;