
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - Unique Indexes
SQL Unique Indexes
The SQL Unique Index ensures that no two rows in the indexed columns of a table have the same values (no duplicate values allowed).
A unique index can be created on one or more columns of a table using the CREATE UNIQUE INDEX statement in SQL.
Following are the points to be noted before creating a Unique Index on a table −
- If the unique index is only created on a single column, the rows in that column will be unique.
- If a single column contains NULL in multiple rows, we cannot create a unique index on that column.
- If the unique index is created on multiple columns, the combination of rows in these columns will be unique.
- We cannot create a unique index on multiple columns if the combination of columns contains NULL in more than one row.
Syntax
Following is the syntax for creating a UNIQUE INDEX in SQL −
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ..., columnN);
Here,
- index_name is the name of the index that you want to create.
- table_name is the name of the table on which you want to create the index.
- (column1, column2, ...., columnN) are the names of one or more columns on which the unique index is being created.
Example
First of all, let us create a table named CUSTOMERS using the following query −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR(15) NOT NULL, AGE INT NOT NULL, ADDRESS VARCHAR(25), SALARY DECIMAL(10, 4), PRIMARY KEY(ID) );
Insert some values into the above-created table using the following query −
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', '32', 'Ahmedabad', 2000), (2, 'Khilan', '25', 'Delhi', 1500), (3, 'kaushik', '23', 'Kota', 2000), (4, 'Chaitali', '26', 'Mumbai', 6500), (5, 'Hardik','27', 'Bhopal', 8500), (6, 'Komal', '22', 'Hyderabad', 9000), (7, 'Muffy', '24', 'Indore', 5500);
Once the table is created, let us create a unique index for the column named SALARY in the CUSTOMERS table using the following query −
CREATE UNIQUE INDEX UNIQUE_ID ON CUSTOMERS (SALARY);
But, when we execute the above query, the output is obtained as follows −
ERROR 1062 (23000): Duplicate entry '2000.00' for key 'customers.UNIQUE_ID'
Since a unique index could not be created on SALARY column (due to duplicate values), let us create Unique Index on the NAME column of the same table, using the following query −
CREATE UNIQUE INDEX UNIQUE_ID ON CUSTOMERS (NAME);
Output
When we execute the above query, the output is obtained as follows −
Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0
Verification
Let's verify whether the unique index for the column NAME is created or not using the following query −
SHOW INDEX FROM CUSTOMERS;
As you observe the output below, you can find the column NAME along with the ID (PRIMARY KEY) in the list of indexes.
Table | Non_unique | Key_name | Seq_in_index | Column_name |
---|---|---|---|---|
customers | 0 | PRIMARY | 1 | ID |
customers | 0 | UNIQUE_ID | 1 | NAME |
Updating with Duplicate Values
If we try to update the columns that have unique index with duplicate values, the database engine generates an error.
Example
Assume the previously created CUSTOMERS table and create a unique index on the column named ADDRESS using the following query −
CREATE UNIQUE INDEX ADD_UNIQUE_INDEX ON CUSTOMERS(ADDRESS);
Now, let us update the value in the column named ADDRESS with a duplicate (already existing data) value using the following query −
UPDATE CUSTOMERS SET ADDRESS = 'Mumbai' WHERE ADDRESS = 'Delhi';
Output
On executing the above query, the output is displayed as follows −
ERROR 1062 (23000): Duplicate entry 'Mumbai' for key 'customers.ADD_UNIQUE_INDEX'
Creating a unique index on Multiple Fields
We can also create a unique index on multiple fields or columns of a table using the CREATE UNIQUE INDEX statement. To do so, you just need to pass the name of the columns (you need to create the index on) to the query.
Example
Instead of creating a new table, let us consider the previously created CUSTOMERS table. We will create a unique index on the columns NAME and AGE using the following query −
CREATE UNIQUE INDEX MUL_UNIQUE_INDEX ON CUSTOMERS(NAME, AGE);
Output
When we execute the above query, the output is obtained as follows −
Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0.
Verification
Now, let us list all the indexes that are created on the CUSTOMERS table using the following query −
SHOW INDEX FROM CUSTOMERS;
As you observe you can find the column names NAME, and AGE along with the ID (PRIMARY KEY) in the list of indexes.
Table | Non_unique | Key_name | Seq_in_index | Column_name |
---|---|---|---|---|
customers | 0 | PRIMARY | 1 | ID |
customers | 0 | MUL_UNIQUE_INDEX | 1 | NAME |
customers | 0 | MUL_UNIQUE_INDEX | 2 | AGE |