MySQL Magic: Bulk INSERT records, repeat records are automatically updated

Source: Internet
Author: User
Tags bulk insert

You may encounter two problems when you update large amounts of data:

    1. If each update executes a SQL performance is low, it is also prone to blocking;

    2. There may be problems with primary key duplication when you batch update

Use on DUPLICATE key to update a SQL to resolve bulk updates and primary key duplication issues (ID primary key)

INSERT into MyTable (Id,pid,ele,anim) VALUES (?,?,?,?), (?,?,?,?), (?,?,?,?) On DUPLICATE KEY UPDATE pid=values (PID), Ele=values (ele)//pid=values (PID), Ele=values (ele) Indicates that when the ID field value of a column in values duplicates the value of an existing ID field in the table, the two fields of the corresponding record are updated///can also specify other values or perform operations: pid=pid+1,ele=ele-1//because the column anim is not specified here, the column with duplicate ID is encountered. The Anim field of the column in the table is not updated//If a column is inserted as a new record, the value of the affected row is 1, and if the records in the table are updated, the value of the affected row is 2

It is not only valid for the unique primary key, but also for the composite primary key, and the composite primary key is set:

ALTER TABLE mytable ADD (CONSTRAINT PRIMARY KEY (id,pid));

But on DUPLICATE KEY update is only MySQL's unique syntax, not SQL standard syntax, don't mess with OH

MySQL Magic: Bulk INSERT records, repeat records are automatically updated

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.