DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Using Arrow Flight SQL to Improve Data Transfer Performance in Apache Doris
  • Automating a Web Form With Playwright MCP and MySQL MCP
  • Master SQL Performance Optimization: Step-by-Step Techniques With Case Studies

Trending

  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • Production Database Migration or Modernization: A Comprehensive Planning Guide [Part 2]
  • How Rule Engines Transform Business Agility and Code Simplicity
  • Why Good Models Fail After Deployment
  1. DZone
  2. Data Engineering
  3. Databases
  4. Difference Between Mysql Replace and Insert on Duplicate Key Update

Difference Between Mysql Replace and Insert on Duplicate Key Update

By 
Prathap Givantha Kalansuriya user avatar
Prathap Givantha Kalansuriya
·
Oct. 03, 12 · News
Likes (0)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

While me and my friend roshan recently working as a support developers at Australia famous e-commerce website. recently roshan as assign a new bug in this site it’s related to the product synchronize process in the ware house product table and the e-commerce site, his main task was check the quickly the site product table and check with ware house product table product if the either insert new data into a site database, or update an existing record on the site database, Of course, doing a lookup to see if the record exists already and then either updating or inserting would be an expensive process (existing items are defined either by a unique key or a primary key). Luckily, MySQL offers two functions to combat this (each with two very different approaches).

1. REPLACE = DELETE+INSERT
2. INSERT ON DUPLICATE KEY UPDATE = UPDATE + INSERT

1 . REPLACE

This syntax is the same as the INSERT function. When dealing with a record with a unique or primary key, REPLACE will either do a DELETE and then an INSERT, or just an INSERT if use this this function will cause a record to be removed, and inserted at the end. It will cause the indexing to get broken apart, decreasing the efficiency of the table. If, however

 

REPLACE INTO
ds_product
SET
pID =  3112,
catID =  231,
uniCost = 232.50,
salePrice = 250.23;

 

2. ON DUPLICATE KEY UPDATE

ON DUPLICATE KEY UPDATE clause to the INSERT function. This one actively hunts down an existing record in the table which has the same UNIQUE or PRIMARY KEY as the one we’re trying to update. If it finds an existing one, you specify a clause for which column(s) you would like to UPDATE. Otherwise, it will do a normal INSERT.

 

INSERT INTO
ds_product
SET
pID =  3112,
catID =  231,
uniCost = 232.50,
salePrice = 250.23,
ON DUPLICATE KEY UPDATE
uniCost = 232.50,
salePrice = 250.23;

This should be helpful when trying to create database queries that add and update information, without having to go through the extra step.

Thanks Have a Nice Day :)

 

 

Database MySQL

Opinions expressed by DZone contributors are their own.

Related

  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Using Arrow Flight SQL to Improve Data Transfer Performance in Apache Doris
  • Automating a Web Form With Playwright MCP and MySQL MCP
  • Master SQL Performance Optimization: Step-by-Step Techniques With Case Studies

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook