The difference between MySQL's replace and on DUPLICATE KEY update

Source: Internet
Author: User

1) When there is no key, replace is the same as insert: on Deplicate udpate.
2) When there is a key, the primary key value is preserved, and auto_increment automatically +1
differences: When there is a key, replace is the delete old record, and enter a new record, so all the original records will be cleared, this time, if the Replace statement field is not complete, some of the original values such as the C field will be automatically populated as the default value.
The insert: Deplicate update only executes the SQL after the update tag, which is equivalent to a simple UPDATE statement on the façade.

but actually, according to my guess, if it's a simple UPDATE statement, auto_ Increment will not be +1, it should also be the delete, then insert operation, except in the process of insert to keep all fields other than update values.
   
  So the difference between the two is only one, insert. On Deplicate Udpate retains the old values of all the fields, overwrites and inserts them together, and replace does not retain the old values, directly deletes and inserts the new values.
  from the underlying execution efficiency, replace is more efficient than the insert: on Deplicate update, but when you write replace, the fields are written in full, preventing old field data from being deleted.

Personal inclination with replace.


The difference between MySQL's replace and on DUPLICATE KEY update

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.