
- 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
Correct MySQL INSERT ... ON DUPLICATE KEY syntax?
The syntax is as follows −
insert into yourTableName values(yourValue1,yourValue2,..N) on duplicate key update yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N;
Let us first create a table −
mysql> create table DemoTable1539 -> ( -> EmployeeName varchar(20), -> EmployeeSalary int, -> UNIQUE KEY(EmployeeName) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500; Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1539 values('David',5500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500; Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1539 values('Chris',4500) on duplicate key update EmployeeName='Robert',EmployeeSalary=8500; Query OK, 2 rows affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1539;
This will produce the following output −
+--------------+----------------+ | EmployeeName | EmployeeSalary | +--------------+----------------+ | Robert | 8500 | | David | 5500 | +--------------+----------------+ 2 rows in set (0.00 sec)
- Related Articles
- Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
- Implementing INSERT… ON DUPLICATE KEY UPDATE in MySQL
- MySQL Syntax to create Foreign Key?
- Add constraint for on duplicate key update in MySQL
- How to prevent duplicate INSERT in MySQL?
- Finding Duplicate Rows in MySQL (Composite Key)?
- How to prevent MySQL double insert (duplicate entry)?
- How to prevent duplicate rows in MySQL INSERT?
- What is the correct syntax for NOT LIKE in MySQL?
- Get the new record key ID from MySQL insert query?
- How to insert data to MySQL having auto incremented primary key?
- What is correct syntax to create Python tuples?
- What is correct syntax to create Python lists?
- What is correct syntax to create Python dictionary?
- What is correct syntax of Python if statement?

Advertisements