
- 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
Dropping Unique constraint from MySQL table?
First, let us create a table with the UNIQUE constraint. It suggests that we cannot add duplicate values.
Creating a table.
mysql> create table UniqueConstraintDemo -> ( -> Name varchar(200) unique -> ); Query OK, 0 rows affected (1.05 sec)
Now, we can check whether the table has UNIQUE constraint or not with the help of DESC command. The query is as follows.
mysql> DESC UniqueConstraintDemo;
The following is the output.
+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | Name | varchar(200) | YES | UNI | NULL | | +-------+--------------+------+-----+---------+-------+ 1 row in set (0.00 sec)
Now, we have unique constraint on column ‘Name’. Therefore, we cannot add duplicate values. If we try to insert same value twice then we will get an error. The query to insert records are as follows.
mysql> insert into UniqueConstraintDemo values('John'); Query OK, 1 row affected (0.54 sec) mysql> insert into UniqueConstraintDemo values('John'); ERROR 1062 (23000): Duplicate entry 'John' for key 'Name'
Look at the Error 1062 above. It suggests that we tried adding duplicate values to a column with UNIQUE constraint.
Let us now see the syntax for dropping the UNIQUE constraint.
DROP index yourColumnName on yourTableName;
Apply the above syntax to remove the unique constraint.
mysql> DROP index Name on UniqueConstraintDemo; Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0
We can now check whether UNIQUE constraint has been removed or not with the help of DESC command. The query is as follows −
mysql> DESC UniqueConstraintDemo;
The following is the output that displaying we have successfully removed UNIQUE constraint.
+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | Name | varchar(200) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 1 row in set (0.00 sec)
- Related Articles
- How can we drop UNIQUE constraint from a MySQL table?
- Adding unique constraint to ALTER TABLE in MySQL
- Dropping a MySQL Table using NodeJS
- Remove unique key from MySQL table?
- How can we apply UNIQUE constraint to the field of an existing MySQL table?
- How can we check the indexes created by a UNIQUE constraint on a MySQL table?
- How do I remove a uniqueness constraint from a MySQL table?
- What is MySQL UNIQUE constraint and how can we apply it to the field of a table?
- What is the difference between MySQL PRIMARY KEY and UNIQUE constraint?
- 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 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?
- How do I add a check constraint to a table in MySQL?
- Fetch unique records from table in SAP ABAP
