MySQL replace into (enhanced version of INSERT INTO)

Source: Internet
Author: User

Inserting a record with the same primary key if a primary key is defined in the table when using the SQL statement to insert a data table inserts an error:

Error code:1062. Duplicate entry ' XXXXX ' for key ' PRIMARY ' (primary key conflict)

So we have to check that the primary key is not present, and if it exists, it will be updated if it does not exist. or write SQL Process Control statements (If...else ...)

MySQL provides the same functionality as replace into and insert into. The difference is:

If you find that this row of data is already in the table ( judging by a primary key or a unique index ), the row data is first deleted and then the new data is inserted. Otherwise, insert the new data directly.

  Note : Because you want to determine whether there is duplicate data based on a primary key or a unique index, the table of operations must have a primary key or a unique index. Otherwise,replace into will insert the data directly.

There are three ways to use Mysql replace into, and one of the simplest uses is to simply replace the "insert" keyword with "replace".

  INSERT into T (...) the values (...) in the

Revision changed to

Replace into t (...) VALUES (...) for the ...

MySQL replace into (enhanced version of INSERT INTO)

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.