Get a skill gap analysis, personalised roadmap, and AI-powered resume optimisation.
Introduction
An index statement in MySQL allows queries to run quickly as your data expands. It stores only a sorted subset of data from the table rows separately. Database indexing is crucial to getting the desired result back and boosts query speed in general.
Lack of necessary indexes frequently leads to high CPU utilization in your database server, sluggish response times, and ultimately dissatisfied users. In this blog, we will discuss Index Statement in MySQL in deep detail!
CREATE INDEX Statement in MySQL
The CREATE INDEX statement creates indexes on one or more columns in the table.
Syntax
The basic syntax of CREATE INDEX Statement is:-
CREATE INDEX index_name ON table_name (column_name);
To prevent duplicate data, a unique index is used. The syntax to create a unique index is:-
CREATE UNIQUE INDEX index_name ON table_name (column_name);
In the above syntax, the description of each keyword is:-
CREATE INDEX: It informs MySQL to create a new index.
UNIQUE: It ensures that the value of the index is unique, which means no duplicate data are allowed.
index_name: The meaningful name of the index the user wants to create.
Table_name: The name of the table on which the index is created.
column_name: It includes the names of the column in which the user want to create the index.
Dropping Index in MySQL
A DROP statement is used to drop a primary and non-primary key index. The syntax for dropping primary and non-primary keys is different in MySQL.
There are two commands to drop the index in MySQL. Let’s discuss both of them.
DROP INDEX Command
It is used to remove an existing index from a table. By dropping the index, it eliminates the data associated with it and increases the performance of operations on the table.
Syntax
The basic syntax to drop the non-primary key is:-
DROP INDEX index_name ON table_name;
The primary keywords are reserved words, so backticks are required to drop the primary key index. The syntax to drop the primary key is:-
DROP INDEX `PRIMARY_Name` ON table_name;
ALTER TABLE Command
The ALTER TABLE Command directly cannot drop an index in MySQL. So, to remove an index, DROP INDEX is used along with ALTER TABLE to remove the existing index.
Syntax
The basic syntax to drop non-primary key using ALTER TABLE is:-
ALTER TABLE table_name DROP INDEX index_name;
The syntax to drop the primary key is:-
ALTER TABLE table_name DROP PRIMARY KEY;
In the above syntax, backticks are not required since, this time, it is a reserved word.
Note: It is necessary to specify the table name since MySQL permits to index name to be reused on multiple tables.
SHOW INDEX Command
SHOW indexes command will provide details about the indexes defined on the "temp" table, such as the index name, the index's included columns' names, the index's type, and other information.
Syntax
The basic syntax to show indexes from Table is:-
SHOW indexes FROM table_name;
Example to Understand Index Statement
Let’s understand creating and dropping index statements with the help of examples.
1. First, Construct a table calledthe employee to contain employee data. Employee_id, first_name, last_name, email, and salary will be columns in the table. To enhance search functionality, we will establish an index on the email column.
3. Now, we will create an index on the email column to optimize the performance of queries.
CREATE INDEX idx_e ON employee (email);
The above command will set the email column as an index in the background.
4. To show the indexes from table employee in MySQL Databases, write the below query.
SHOW indexes FROM employee;
Output:
5. To find the employee with specific mail, the user can write the below query that will enhance the performance of the table.
SELECT *
FROM employee
WHERE email = 'jane.smith@example.com';
Output:
6. Index can be dropped by using DROP Command.
ALTER TABLE employee
DROP INDEX idx_e;
Advantages of using Indexes in MySQL
The advantage of using indexes in MySQL are:-
Indexing allows us to search the data more efficiently and fastly. It is used to improve the query performance.
Indexing improves the performance of the join operation.
It is used to speed up the sorting operation.
It is used to apply unique and primary key constraints.
It provides flexibility and allows users to create more complex and efficient queries.
Disadvantages of using Indexes in MySQL
The disadvantages of using indexes in MySQL are:-
It requires extra storage space depending on the number of rows and area of columns.
Since data changes over time, indexes need to be maintained.
Fragmentation can decrease Indexing performance and increase disk I/O Operations.
Indexing slows down the data modification operation due to the updation of the index each time.
Frequently Asked Questions
What does the MySQL CREATE INDEX statement serve?
The speed of database activities like searching, sorting, and filtering data is improved by an index. It makes queries more effective by enabling the database to find matching rows rapidly.
How can I employ MySQL's CREATE INDEX statement?
The table name, the desired index column(s), and the index name must all be specified to build an index. CREATE INDEX index_name ON table_name (column_name) is the syntax.
What distinguishes a non-unique index from a unique index?
A unique index assures that the indexed column(s) have unique values, allowing no duplicate entries. A non-unique index, on the other hand, permits duplicate values in the indexed column(s).
How can an index be deleted once it has been created?
The DROP INDEX command can be used to delete an index. To delete an index, specify its name and the table it belongs to before running the query.
Conclusion
Index Statement in MySQL is a valuable tool for improving the efficiency of database queries by building indexes on one or more table fields. In this article, we have discussed the creation and dropping of the index in the table.
Do not stop learning! We recommend you read some of our related articles:
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problems, interview experiences, and interview bundles.