CREATE INDEX v11

Name

CREATE INDEX -- define a new index.

Synopsis

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

Description

CREATE INDEX constructs an index, name, on the specified table. Indexes are primarily used to enhance database performance (though inappropriate use will result in slower performance).

The key field(s) for the index are specified as column names, constants, or 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) would allow the clause WHERE UPPER(col) = 'JIM' to use an index.

Advanced Server 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 command does propagate indexes to the table’s subpartitions.

Parameters

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 (possibly schema-qualified) of the table to be indexed.

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 shown 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, or the database’s default tablespace if default_tablespace is an empty string.

Notes

Up to 32 fields may be specified in a multicolumn index.

Examples

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

CREATE INDEX name_idx ON emp (ename);

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

CREATE INDEX name_idx ON emp (ename) TABLESPACE index_tblspc;

See Also

ALTER INDEX, DROP INDEX