×

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_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 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 RADIANS() function

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

2 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL DAY() function

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

2 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 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 MONTHNAME() function

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

3 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL CEIL() function

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

3 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL DATEDIFF() function

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

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL LOCATE() function

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

3 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.

MySQL LIKE() function

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

4 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.