×

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 vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 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 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 MOD() 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 Basically, in MySQL, the MOD() function is...

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

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 ELT() function

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

2 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

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

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

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

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

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