
- 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 avoid inserting duplicate rows in MySQL?
To avoid inserting duplicate rows in MySQL, you can use UNIQUE(). The syntax is as follows −
ALTER TABLE yourTableName ADD UNIQUE(yourColumnName1,yourColumnName2,...N);
To understand the above syntax, let us create a table.
The query to create a table is as follows −
mysql> create table avoidInsertingDuplicateRows -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstValue int, -> SecondValue int -> ); Query OK, 0 rows affected (0.53 sec)
Now check the description of table using desc command.
The query is as follows −
mysql> desc avoidInsertingDuplicateRows;
Sample The following is The output −
+-------------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | FirstValue | int(11) | YES | | NULL | | | SecondValue | int(11) | YES | | NULL | | +-------------+---------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
Here is the query to avoid inserting duplicate rows in MySQL. We will set it with the insert command to insert records in the table −
mysql> insert into avoidInsertingDuplicateRows(FirstValue,SecondValue) values(10,20); Query OK, 1 row affected (0.24 sec) mysql> insert into avoidInsertingDuplicateRows(FirstValue,SecondValue) values(10,20); ERROR 1062 (23000): Duplicate entry '10-20' for key 'FirstValue'
Display all records from the table using select statement.
The query is as follows −
mysql> select *from avoidInsertingDuplicateRows;
Here is the output −
+----+------------+-------------+ | Id | FirstValue | SecondValue | +----+------------+-------------+ | 1 | 10 | 20 | +----+------------+-------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL UNIQUE declaration to avoid inserting duplicate values?
- Inserting multiple rows in MySQL?
- How to prevent duplicate rows in MySQL INSERT?
- Finding Duplicate Rows in MySQL (Composite Key)?
- Display an error while inserting duplicate records in a MySQL table
- How to avoid inserting NULL values to a table with JavaScript?
- How to Autofill Formula When Inserting Rows in Excel?
- Avoid duplicate entries in MongoDB?
- How to drop duplicate rows in pandas series?
- How to find and filter Duplicate rows in Pandas ?
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- How to avoid #ref error while deleting the rows in Excel?
- Return only a single row from duplicate rows with MySQL
- How to prevent duplicate INSERT in MySQL?
- How to remove rows in a Pandas series with duplicate indices?

Advertisements