Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 August Insert-Update, Merge statement in MySQL

Insert-Update, Merge statement in MySQL

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on the merge statement for Insert-Update in MySQL.

For all the database developers merge statement is very popular to handle insert, update and delete into a single statement.

Microsoft SQL Server provides the merge statement in which you can manage Insert, update and delete and can make a single statement.

But unfortunately, MySQL server has no any default Merge Statement.
It has a similar alternative option for merging of Insert, Update.

You can use INSERT ON DUPLICATE KEY UPDATE in MySQL.

Let’s first create sample table with data:

1
2
3
4
5
6
7
8
9
CREATE DATABASE Employee;
CREATE TABLE Employee.tbl_TestMerge
(
EmpNumber INTEGER PRIMARY KEY,
EmpName VARCHAR(50)
);
INSERT INTO Employee.tbl_TestMerge
VALUES (1,'ABC'),(2,'XYZ'),(3,'PQR')
,(4,'EFG'),(5,'JKL');

Use INSERT ON DUPLICATE KEY UPDATE:

1
2
3
4
5
INSERT INTO Employee.tbl_TestMerge
(EmpNumber,EmpName)
VALUES (2,'LKM')
ON DUPLICATE KEY UPDATE
EmpNumber=VALUES(EmpNumber),EmpName=VALUES(EmpName);

In the above code, you can see EmpNumber is Primary Key and I am trying to insert same EmpNumber=2 with different EmpName.

As you can check that, I also mentioned ON DUPLICATE KEY UPDATE with Insert Statement.

Now, this statement first checks the value of data and if data with the same key, internally execute update statement otherwise it executes the insert statement.

The Result:
MySQLMerge

This way you can use ON DUPLICATE KEY UPDATE statement for merging Insert and Update statement.

Important points:
If you are using ON DUPLICATE KEY UPDATE statement, your table has must Primary key or at least one Unique Key.

If you are using Auto_Increment number, your LAST_INSERT_ID() function will not work properly because if data updates, also Auto_Increment ID will increase a number internally. This is a disadvantage of this statement.

Aug 18, 2015Anvesh Patel
MySQL Temporary Table vs Memory Table.Prepared or Parameterized Statements in Database System
Comments: 6
  1. maunamopest
    October 26, 2015 at 4:39 am

    If I use merge statement only for Insert then does it is not good as use Insert statement. Please give your expert comments which one give better performance.

    • Anvesh Patel
      Anvesh Patel
      October 26, 2015 at 4:30 pm

      I agree, Internally it is checking for duplicate key at every insert. You can use this for normal insertion. I can not suggest to use this during bulk insertion.

  2. Nubia
    February 23, 2016 at 10:15 pm

    I want forgathering useful information , this post has got me even more info! .

  3. taqveem khan
    March 31, 2018 at 2:40 pm

    suppose my both table does’t have primary key basically its log table like in oracle there is no restriction table must have primary key for merge statement .
    so would i use merge statement in this scenario ??? l

    • Anvesh Patel
      Anvesh Patel
      March 31, 2018 at 6:09 pm

      You should not merge data without a unique or primary key. And still, if you want it, you should check all columns combination so that you can merge the whole record.

  4. taqveem khan
    April 1, 2018 at 5:25 am

    Thanks for Your reply .

Anvesh Patel
Anvesh Patel

Database Engineer

August 18, 2015 MySQLAnvesh Patel, database, database research and development, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks, ON DUPLICATE KEY UPDATE
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....