Skip to content
CS Chetan Patil
About Projects Blog Gallery Contact
Resume
  1. Home
  2. Blog
  3. MySQL

MySQL 3 July 2016 · 4 min read

MySQL INDEXES

Database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns. While creating index, it should be considered that what are the columns which will be used to make…

Chetan S. Patil Software Engineer at Genpact
On this page
  1. Simple and Unique Index:
  2. ALTER command to add and drop INDEX:
  3. ALTER Command to add and drop PRIMARY KEY:
  4. Displaying INDEX Information:

Database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns.

While creating index, it should be considered that what are the columns which will be used to make SQL queries and create one or more indexes on those columns.

Practically, indexes are also type of tables, which keep primary key or index field and a pointer to each record into the actual table.

The users cannot see the indexes, they are just used to speed up queries and will be used by Database Search Engine to locate records very fast.

INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert or update, database need to insert or update index values as well.

Simple and Unique Index: #

You can create a unique index on a table. A unique index means that two rows cannot have the same index value. Here is the syntax to create an Index on a table

1
2
CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...);

You can use one or more columns to create an index. For example, we can create an index on authors_tbl using author_name.

1
2
CREATE UNIQUE INDEX AUTHOR_INDEX
ON authors_tbl (author_name)

You can create a simple index on a table. Just omit UNIQUE keyword from the query to create simple index. Simple index allows duplicate values in a table.

If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name.

1
2
mysql> CREATE UNIQUE INDEX AUTHOR_INDEX
ON authors_tbl  (author_name DESC)

ALTER command to add and drop INDEX: #

There are four types of statements for adding indexes to a table:

  • ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): This statement adds a PRIMARY KEY, which means that indexed values must be unique and cannot be NULL.
  • ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): This statement creates an index for which values must be unique (with the exception of NULL values, which may appear multiple times).
  • ALTER TABLE tbl_name ADD INDEX index_name (column_list):This adds an ordinary index in which any value may appear more than once.
  • ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list): This creates a special FULLTEXT index that is used for text-searching purposes.

Here is the example to add index in an existing table.

1
mysql> ALTER TABLE test_tbl ADD INDEX (c);

You can drop any INDEX by using DROP clause along with ALTER command. Try out the following example to drop above-created index.

1
mysql> ALTER TABLE test_tbl DROP INDEX (c);

You can drop any INDEX by using DROP clause along with ALTER command. Try out the following example to drop above-created index.

ALTER Command to add and drop PRIMARY KEY: #

You can add primary key as well in the same way. But make sure Primary Key works on columns, which are NOT NULL.

Here is the example to add primary key in an existing table. This will make a column NOT NULL first and then add it as a primary key.

1
2
mysql> ALTER TABLE test_tbl MODIFY i INT NOT NULL;
mysql> ALTER TABLE test_tbl ADD PRIMARY KEY (i);

You can use ALTER command to drop a primary key as follows:

1
mysql> ALTER TABLE test_tbl DROP PRIMARY KEY;

To drop an index that is not a PRIMARY KEY, you must specify the index name.

Displaying INDEX Information: #

You can use SHOW INDEX command to list out all the indexes associated with a table. (wraparound by \G):

Try out the following example:

1
mysql> SHOW INDEX FROM table_name\G
Share this
Chetan S. Patil

Written by

Chetan S. Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

About LinkedIn
PreviousDesign Patterns in PHP NextClustered and Secondary Indexes

Related reading

All articles

MySQL · 3 min read

MySQL Table Types, or Storage Engines

It is essential to understand the features of each table type in MySQL so that you can use them effectively to maximise…

6 Jul 2016

MySQL · 2 min read

Clustered and Secondary Indexes

InnoDB table has a special index called the clustered index where the data for the rows is stored. Typically, the clustered index…

6 Jul 2016

Artificial Intelligence · 4 min read

AI: Mind-blowing Machines and What They Mean for Us Mere Mortals

We’re all living in the age of AI – from creepy-accurate face unlock on your phone to those “recommended for you” algorithms…

10 Jun 2024

CS Chetan Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

i@chetanpatil.co.in

Site

About Projects Blog Gallery Resume Contact RSS

Elsewhere

Pune, Maharashtra · IST (UTC+5:30)

© 2026 Chetan S. Patil

Built with PHP & MySQL

Chetan Patil
About Projects Blog Gallery Contact Resume
i@chetanpatil.co.in