
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to make a pair of columns unique in MySQL?
To make a pair of columns unique, use UNIQUE with ALTER TABLE command. Following is the syntax −
alter table yourTableName add unique yourUniqueName(yourColumnName1,yourColumnName2,...N);
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(100), StudentLastName varchar(100), StudentAge int, StudentPhoneNumber varchar(20) ); Query OK, 0 rows affected (0.81 sec)
Following is the query to make a pair of unique columns in MySQL −
mysql> alter table DemoTable add unique DemoTable_unique_StudentFirstName_StudentPhoneNumber(StudentFirstName,StudentPhoneNumber); Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentPhoneNumber) values('John','Doe',21,'9878567878'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentPhoneNumber) values('John','Smith',23,'7654674545'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentFirstName,StudentLastName,StudentAge,StudentPhoneNumber) values('John','Brown',21,'9878567878'); ERROR 1062 (23000): Duplicate entry 'John-9878567878' for key 'DemoTable_unique_StudentFirstName_StudentPhoneNumber'
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+-----------------+------------+--------------------+ | StudentId | StudentFirstName | StudentLastName | StudentAge | StudentPhoneNumber | +-----------+------------------+-----------------+------------+--------------------+ | 1 | John | Doe | 21 | 9878567878 | | 2 | John | Smith | 23 | 7654674545 | +-----------+------------------+-----------------+------------+--------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to make an existing field Unique in MySQL?
- How to make a unique field in MongoDB?
- How to keep two “columns” in MongoDB unique?
- Using UNIQUE for varchar columns with some conditions in MySQL?
- How to make unique objects accessible in other modules of Python?
- How to select unique value in MySQL?
- How to form a composite key to be unique in MySQL?
- How to find the number of columns in a MySQL table?
- How to update two columns in a MySQL database?
- How to rearrange MySQL columns?
- How to find the unique rows based on some columns in R?
- How to concatenate all columns in MySQL?
- How to search between columns in MySQL?
- How to search multiple columns in MySQL?
- How do I show unique constraints of a table in MySQL?

Advertisements