
- 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
Which technique is more efficient for replacing duplicate records in MySQL?
To replace duplicate records and avoid any error while inserting, use INSERT ON DUPLICATE KEY UPDATE. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20), -> UNIQUE(Id,Name) -> ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(101,'Chris') on duplicate key update Id=10001,Name='Robert'; Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(102,'Mike') on duplicate key update Id=10001,Name='Robert'; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(101,'Chris') on duplicate key update Id=10001,Name='Robert'; Query OK, 2 rows affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+--------+ | Id | Name | +-------+--------+ | 102 | Mike | | 10001 | Robert | +-------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- Get the sum of columns for duplicate records in MySQL
- SUM corresponding duplicate records in MySQL
- Find and display duplicate records in MySQL?
- Display records with more than two occurrences in MySQL?
- How to delete all the duplicate records in a MySQL table?
- How to remove Duplicate Records except a single record in MySQL?
- Count the occurrences of specific records (duplicate) in one MySQL query
- Add records from corresponding duplicate values in another column with MySQL
- Display an error while inserting duplicate records in a MySQL table
- Find duplicate records in MongoDB?
- Which of the two is more energy efficient: filament type electric bulb or CFL? Why?
- Name one medical technique which is based on magnetism produced in human body. For what purpose is this technique used?
- MySQL query to display the count of distinct records from a column with duplicate records
- Which query is efficient to check if MySQL Table is empty? COUNT(*) vs. LIMIT?
- Sort data for duplicate record in MySQL

Advertisements