
- 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
Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
The INSERT ... ON DUPLICATE KEY UPDATE works in a way that if it finds a duplicate unique or primary key, it will perform an UPDATE. The UPDATE is performed only when duplicate values occur.
Let us first create a table −
mysql> create table DemoTable733 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentMarks int, UNIQUE KEY Un_Name (StudentName) ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable733(StudentName,StudentMarks) values('John',45) ON DUPLICATE KEY UPDATE StudentMarks=86; Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable733(StudentName,StudentMarks) values('Adam',65) ON DUPLICATE KEY UPDATE StudentMarks=86; Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable733(StudentName,StudentMarks) values('Carol',75) ON DUPLICATE KEY UPDATE StudentMarks=86; Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable733(StudentName,StudentMarks) values('John',45) ON DUPLICATE KEY UPDATE StudentMarks=86; Query OK, 2 rows affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable733;
This will produce the following output -
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 1 | John | 86 | | 2 | Adam | 65 | | 3 | Carol | 75 | +-----------+-------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- Implementing INSERT… ON DUPLICATE KEY UPDATE in MySQL
- Correct MySQL INSERT ... ON DUPLICATE KEY syntax?
- Add constraint for on duplicate key update in MySQL
- Update MySQL table on INSERT command with triggers?
- Update table with duplicate ids in MySQL
- How to prevent duplicate INSERT in MySQL?
- Implement MySQL INSERT MAX()+1?
- Finding Duplicate Rows in MySQL (Composite Key)?
- How to prevent duplicate rows in MySQL INSERT?
- How to update DB2 table with a duplicate primary key?
- Implement GREATEST() in MySQL and update the table?
- How to prevent MySQL double insert (duplicate entry)?
- Can we implement nested insert with select in MySQL?
- How to implement CANDIDATE key in any MySQL table?
- Use a trigger to stop an insert or update in MySQL?

Advertisements