×

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column primarily assigns with an integer data type. We can create an index in an existing table or a new table. Mostly, the index column assigns the left-most column of the table. The index assigns for either single or multiple columns.

Syntax

The create index syntax in a new table shows below. MySQL index creates using the "INDEX" keyword.

Create table table_name (
Column1 INT NOT NULL PRIMARY KEY,
Column2 INT NOT NULL,
Column3 INT NOT NULL,
Column4 varchar(45),
INDEX (Column1, Column2));

The syntax of a create index with an existing table shows below:

CREATE INDEX index_name ON table_name (
Column1, column2… columnN);

Examples of the create index

Let us understand how to create an index in MySQL with the help of an example.

Example 1: Create an index with the new table.

Execute the below query to create the table and index column. Here, the index assigns to multiple columns. You can apply index on either single column or multiple columns.

mysql> create table index_table(
-> roll_number INT NOT NULL PRIMARY KEY,
-> number INT NOT NULL,
-> mark INT NOT NULL,
-> name varchar(45),
-> INDEX (roll_number, number));

Execute the following query to get the index of the table.

mysql> show index from index_table;
MySQL Create Index

The above output image shows the index column of the table. This output displays index type as per storage index.

2) Example: Create index with an existing table

Execute the below query to create an index column in an existing table. Here, the index assigns to a single column. You can apply index on either single column or multiple columns.

mysql> create index mark on index_table(mark);

OUTPUT

Execute the following query to show the index of the table.

mysql> show index from index_table;
MySQL Create Index

The above output image shows the index column of the table. This output display index type as per storage index.

Storage index and index type

MySQL creates a B-Tree index type by default. This index type supports all storage engines. If you want to change the index type, then you need to specify the storage engine. MySQL table specifies storage engine and index type as per application requirement. The following table shows the storage engine and its index type.

Storage Engine MySQL Index Type
InnoDB storage engine This storage engine supports to B-Tree index type.
MyISAM storage engine This engine allows only B-Tree index type.
MEMORY/HEAP These storage engines allow the HASH index and B-Tree index.

Related Topics

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.