
- 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 an existing field Unique in MySQL?
To make an existing field unique in MySQL, we can use the ALTER command and set UNIQUE constraint for the field. Let us see an example. First, we will create a table.
mysql> create table AddingUnique -> ( -> Id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.44 sec)
Syntax to add UNIQUE to an existing field.
alter table yourTableName add UNIQUE(yourColumnName);
Applying the above syntax in order to add UNIQUE to column ‘name’.
mysql> alter table AddingUnique add UNIQUE(name); Query OK, 0 rows affected (0.60 sec) Records: 0 Duplicates: 0 Warnings: 0
Now we cannot insert duplicate records into the table, since we have set the field to be unique. If we try to add duplicate records then it raises an error.
mysql> alter table AddingUnique add UNIQUE(name); Query OK, 0 rows affected (0.60 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into AddingUnique values(1,'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into AddingUnique values(1,'John'); ERROR 1062 (23000): Duplicate entry 'John' for key 'name' mysql> insert into AddingUnique values(2,'Carol'); Query OK, 1 row affected (0.18 sec) mysql> insert into AddingUnique values(3,'John'); ERROR 1062 (23000): Duplicate entry 'John' for key 'name' mysql> insert into AddingUnique values(4,'Smith'); Query OK, 1 row affected (0.18 sec)
To display all records.
mysql> select *from AddingUnique;
The following is the output.
+------+-------+ | Id | name | +------+-------+ | 1 | John | | 2 | Carol | | 4 | Smith | +------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- Making an existing field Unique in MySQL?
- How can we apply UNIQUE constraint to the field of an existing MySQL table?
- How to make a unique field in MongoDB?
- How to update field to add value to existing value in MySQL?
- How to add a random number between 30 and 300 to an existing field in MySQL?
- Which statement, other than ALTER TABLE statement, can be used to apply UNIQUE constraint to the field of an existing MySQL table?
- How to make a pair of columns unique in MySQL?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How add unique key to existing table (with non-unique rows)?
- How to add columns to an existing MySQL table?
- How to rename a column in an existing MySQL table?
- How to make all values in an R data frame unique?
- How to add current date to an existing MySQL table?
- How to make duplicate factor levels unique in an R data frame?

Advertisements