This tutorial explains how to create, drop, and rename indexes in MySQL 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.
There are 2 ways to create an index. You can either create an index when you first create a table using the CREATE TABLE statement or you can use the CREATE INDEX statement after the table has been created.
The syntax to create an index using the CREATE TABLE statement in MySQL is:
OR
The syntax to create an index using the CREATE INDEX statement in MySQL is:
Optional. The UNIQUE modifier indicates that the combination of values in the indexed columns must be unique.
Optional. The FULLTEXT modifier indexes the entire column and does not allow prefixing. InnoDB and MyISAM tables support this option.
Optional. The SPATIAL modifier indexes the entire column and does not allow indexed columns to contain NULL values. InnoDB (starting in MySQL 5.7) and MyISAM tables support this option.
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.
Optional. If specified, only a prefix of the column is indexed not the entire column. For non-binary string columns, this value is the given number of characters of the column to index. For binary string columns, this value is the given number of bytes of the column to index.
Optional. The index is sorted in ascending order for that column.
Optional. The index is sorted in descending order for that column.
Let's look at an example of how to create an index in MySQL using the CREATE TABLE statement. This statement would both create the table as well as the index at the same time.
For example:
In this example, we've created the contacts table as well as an index called contacts_idx which consists of the last_name and first_name columns.
Next, we will show you how to create the table first and then create the index using the CREATE INDEX statement.
For example:
In this example, the CREATE TABLE statement would create the contacts table. The CREATE INDEX statement would create an index called contacts_idx that consists of the last_name and the first_name fields.
To create a unique index on a table, you need to specify the UNIQUE keyword when creating the index. Again, this can be done with either a CREATE TABLE statement or a CREATE INDEX statement.
For example:
OR
Both of these examples would create a unique index on the last_name and first_name fields so that the combination of these fields must always contain a unique value with no duplicates. This is a great way to enforce integrity within your database if you require unique values in columns that are not part of your primary key.
You can drop an index in MySQL using the DROP INDEX statement.
The syntax to drop an index using the DROP INDEX statement in MySQL is:
The name of the index to drop.
The name of the table where the index was created.
Let's look at an example of how to drop an index in MySQL.
For example:
In this example, we've dropped an index called contacts_idx from the contacts table.
You can rename an index in MySQL. Depending on your version of MySQL, there are two different syntaxes.
The syntax to rename an index using the ALTER TABLE statement in MySQL 5.6 and later is:
OR
The syntax to rename an index in MySQL 5.7 or newer is:
The name of the table where the index was created.
The name of the index that you wish to rename.
The new name for the index.
Let's look at an example of how to rename an index in MySQL. In older versions of MySQL, you need to use the ALTER TABLE statement to first drop the old index and then recreate the new index.
For example (MySQL 5.6 and older):
In this example, we've renamed the index called contacts_idx to contacts_new_index. This was done by dropping the old index and then adding the new index.
Starting in MySQL 5.7, you can use the ALTER TABLE statement with the RENAME INDEX clause to rename the index.
For example (MySQL 5.7 and newer):
This would also rename the index from contacts_idx to contacts_new_index. If you are unsure which version of MySQL you are running, it is safest to use the first syntax to rename an index.