This Oracle tutorial explains how to create, rename and drop indexes in Oracle with syntax and examples.
An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed columns. By default, Oracle creates B-tree indexes.
The syntax for creating an index in Oracle/PLSQL is:
It indicates that the combination of values in the indexed columns must be unique.
The name to assign to the index.
The name of the table in which to create the index.
The columns to use in the index.
It tells Oracle to collect statistics during the creation of the index. The statistics are then used by the optimizer to choose a "plan of execution" when SQL statements are executed.
Let's look at an example of how to create an index in Oracle/PLSQL.
For example:
In this example, we've created an index on the supplier table called supplier_idx. It consists of only one field - the supplier_name field.
We could also create an index with more than one field as in the example below:
We could also choose to collect statistics upon creation of the index as follows:
In Oracle, you are not restricted to creating indexes on only columns. You can create function-based indexes.
The syntax for creating a function-based index in Oracle/PLSQL is:
It indicates that the combination of values in the indexed columns must be unique.
The name to assign to the index.
The name of the table in which to create the index.
The functions to use in the index.
It tells Oracle to collect statistics during the creation of the index. The statistics are then used by the optimizer to choose a "plan of execution" when SQL statements are executed.
Let's look at an example of how to create a function-based index in Oracle/PLSQL.
For example:
In this example, we've created an index based on the uppercase evaluation of the supplier_name field.
However, to be sure that the Oracle optimizer uses this index when executing your SQL statements, be sure that UPPER(supplier_name) does not evaluate to a NULL value. To ensure this, add UPPER(supplier_name) IS NOT NULL to your WHERE clause as follows:
The syntax for renaming an index in Oracle/PLSQL is:
The name of the index that you wish to rename.
The new name to assign to the index.
Let's look at an example of how to rename an index in Oracle/PLSQL.
For example:
In this example, we're renaming the index called supplier_idx to supplier_index_name.
If you forgot to collect statistics on the index when you first created it or you want to update the statistics, you can always use the ALTER INDEX command to collect statistics at a later date.
The syntax for collecting statistics on an index in Oracle/PLSQL is:
The index in which to collect statistics.
Let's look at an example of how to collect statistics for an index in Oracle/PLSQL.
For example:
In this example, we're collecting statistics for the index called supplier_idx.
The syntax for dropping an index in Oracle/PLSQL is:
The name of the index to drop.
Let's look at an example of how to drop an index in Oracle/PLSQL.
For example:
In this example, we're dropping an index called supplier_idx.