DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: MySQL

INSERT … ON DUPLICATE KEY UPDATE

Share

If you mention ON DUPLICATE KEY UPDATE when a row is inserted that is causing a duplicate value in a UNIQUE index or PRIMARY KEY then MySQL performs an UPDATE of the old row.

Suppose, in a table there are three columns i.e. a, b, c.
If column ‘a’ is declared as UNIQUE and contains the value 1 then the following two statements are similar effect:

INSERT INTO tbl_name (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c = c+1;
UPDATE tbl_name SET c = c+1 WHERE a = 1;

A table containing an AUTO_INCREMENT column and INSERT … ON DUPLICATE KEY UPDATE inserts or updates a row then, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value.

Read Also: Update from one table to another with condition in MySQL

The ON DUPLICATE KEY UPDATE clause may contain multiple column assignments i.e. separated by commas.

The DELAYED option is ignored while you are using ON DUPLICATE KEY UPDATE.

Namaste UI

For any types of queries, you can contact us on info[at]namasteui.com.

Recent Posts

How Digital Technology Is Transforming the Way Construction Companies Source Heavy Machinery

The construction industry has always depended on having the right equipment available at the right…

10 hours ago

How Small Brands Can Make Offline Touchpoints Feel More Memorable

Most small brands spend a lot of energy improving digital touchpoints. They refine their websites,…

4 days ago

What Should You Do When a Dog Bites You?

Not every dog is friendly, and even friendly dogs can attack when threatened or provoked.…

5 days ago

Holding Manufacturers Accountable: Steps to File a Lawsuit for Pressure Cooker Explosions

Pressure cookers have become a favored kitchen appliance because they can quickly and efficiently cook…

5 days ago

Reasons to Hire a Shopify Web Designer for Your Business

Shopify has quickly become the preferred platform for businesses seeking to build an online presence.…

1 week ago

How To Gain Free Instagram Followers and Likes Easily-GetInsta

We all know that Instagram is a very famous social platform that attracts more and…

2 weeks ago