
- 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
Display an error while inserting duplicate records in a MySQL table
For this, you can use UNIQUE KEY. Let us first create a table −
mysql> create table DemoTable1553 -> ( -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(20), -> EmployeeSalary int -> ); Query OK, 0 rows affected (0.47 sec)
Here is the query to add unique key −
mysql> alter table DemoTable1553 add unique(EmployeeSalary); Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Chris',45000); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('David',35000); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Sam',25000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1553(EmployeeName,EmployeeSalary) values('Carol',45000); ERROR 1062 (23000): Duplicate entry '45000' for key 'EmployeeSalary'
Display all records from the table using select statement −
mysql> select * from DemoTable1553;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 45000 | | 2 | David | 35000 | | 3 | Sam | 25000 | +------------+--------------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
- Format date while inserting records in MySQL
- Ignore MySQL INSERT IGNORE statement and display an error if duplicate values are inserted in a table
- Find and display duplicate records in MySQL?
- Inserting records into a MySQL table using PreparedStatement in Java?
- Escaping quotes while inserting records in MongoDB?
- No error while inserting record in child table with no match in master table in SAP
- Display table records from a stored procedure in MySQL
- How to delete all the duplicate records in a MySQL table?
- Display selected records from a MySQL table with IN() operator
- Inserting Records into Table using Node
- How to fix the incorrect datetime value while inserting in a MySQL table?
- MySQL query to display the count of distinct records from a column with duplicate records
- How to delete a single value from a MySQL table with duplicate records?
- How to avoid inserting duplicate rows in MySQL?

Advertisements