
- 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
What is MySQL UNIQUE constraint and how can we apply it to the field of a table?
As the name suggests, MySQL UNIQUE constraint maintains the uniqueness of a column in the table and does not allow inserting the duplicate value. Basically, UNIQUE constraint creates an index such that all the values in the index column must be unique. It is pertinent to mention here that we can have more than one UNIQUE column in a MySQL table.
We can apply UNIQUE constraint by mentioning the ‘UNIQUE’ keyword at the time of defining a column. It can be understood with the help of the following example −
mysql> Create table test3(ID INT UNIQUE, Name Varchar(20)); Query OK, 0 rows affected (0.16 sec)
The above query creates a table named ‘test3’ having a column ‘ID’ with ‘UNIQUE’ constraint on it. We can check it with DESCRIBE statement as follows −
mysql> DESCRIBE test3; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | UNI | NULL | | | Name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.04 sec)
The UNIQUE constraint can be applied to a column of a table with the following query as well −
mysql> Create table test4(ID INT, Name Varchar(20),UNIQUE(ID)); Query OK, 0 rows affected (0.15 sec)
We can check it with DESCRIBE statement as follows −
mysql> DESCRIBE test4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | UNI | NULL | | | Name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.04 sec)
- Related Articles
- How can we apply UNIQUE constraint to the field of an existing MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How can we drop UNIQUE constraint from a MySQL table?
- Which statement, other than ALTER TABLE statement, can be used to apply UNIQUE constraint to the field of an existing MySQL table?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we check the indexes created by a UNIQUE constraint on a MySQL table?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- Dropping Unique constraint from MySQL table?
- Adding unique constraint to ALTER TABLE in MySQL
- How can we apply BIT_LENGTH() function on the column/s of MySQL table?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- What is the difference between MySQL PRIMARY KEY and UNIQUE constraint?
