×

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which is not possible with the primary key constraint.

This query creates a unique index in the table. It can also remove duplicate index columns from the table. You can create a new index using the "UNIQUE" keyword. You can also alter the table to create a new and unique index column.

Syntax

The unique index syntax shows below.

CREATE UNIQUE INDEX index name
ON table name (column1, column2, columnN);

The unique index syntax shows below. Create a new table with a unique key for a unique index column.

CREATE TABLE table name(
column1 data type PRIMARY KEY,
column2 data type NOT NULL,
column3 data type NOT NULL,
column data type NOT NULL,
UNIQUE KEY (column1, column2, columnN)
);

The unique index syntax with the existing table is shown below.

ALTER TABLE table-name
ADD CONSTRAINT [constraint name] UNIQUE KEY (column1, column2, columnN);

Examples of the unique index

1) Example: Create a unique index with a new table.

Execute the below query to create a table with the unique index column. Here, unique index assign to multiple column. You can also apply this 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),
-> UNIQUE INDEX (roll_number, number));

OUTPUT

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

mysql> show index from index_table;
MySQL Unique Index

The above output image shows the unique index column of the table. This output displays index name and index type as per the storage index. The Non_unique column shows 0 values.

2) Example: Create unique index with existing table

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

mysql> create unique index mark on index_table(mark);

OUTPUT

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

mysql> show index from index_table;
MySQL Unique Index

The above output image shows a unique index of the table. This output displays index type as per storage index. Here mark column updates as a unique index.

3) Example: The unique index with constraint example.

Execute the below query to create a unique index column in an existing table. Here, the index assigns to a single column. The "alter table" statement is used to add an index to the column. The key is known as an index of the table.

mysql> ALTER TABLE index_table
ADD CONSTRAINT UNIQUE KEY (mark);

OUTPUT

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

mysql> show index from index_table;
MySQL Unique Index

The above output image shows a unique index of the table. This output displays index type as per storage index. Here mark column updates with a unique index.

4) Example: The unique index with the database name.

Execute the below query to create a unique index column in an existing table. Here, the index assigns to a single column. The database refers to the table name to add a unique index. You apply this index on either a single column or multiple columns.

mysql> create unique index mark on tutorial.index_table(mark);

OUTPUT

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

mysql> show index from index_table;
MySQL Unique Index

The above output image shows a unique index of the table. This output displays index type as per storage index. Here mark column updates with a unique index. This index refers tutorial database with the "index_table" table.


Related Topics

MySQL CONCAT() function

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

3 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 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 Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 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 Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL PI() function

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

2 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 SOUNDEX() function

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

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

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL NOW() function

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

2 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 CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.