Explanation of myslq_replace statements

Source: Internet
Author: User

REPLACE runs like INSERT. There is only one exception. Assume that an old record in the table and a record used for PRIMARY

If the new record of a KEY or UNIQUE index has the same value, the old record is deleted before the new record is inserted.
Note: unless the table has a primary key or UNIQUE index, using a REPLACE statement is meaningless. The

The statement is the same as the INSERT statement, because no index is used to determine whether other rows are copied in the new row.


The values of all columns are equal to the value specified in the REPLACE statement. All missing columns are set as their default values.

Same as INSERT. You cannot reference a value from the current row or use a value in a new row. If you use

SET col_name = col_name + 1 ", the reference to the column name on the right will be used as DEFAULT

(Col_name) processing. Therefore, the value is equivalent to SET col_name = DEFAULT (col_name) + 1.

To use REPLACE, you must have both the INSERT and DELETE permissions for the table.


The REPLACE statement returns a number to indicate the number of affected rows. This is the sum of the number of deleted and inserted rows.

. If this number is 1 for a single row, one row is inserted and no row is deleted. If the number is greater than 1

Before a new row is inserted, one or more old rows are deleted. If the table contains multiple unique indexes and a new row is copied

The value of different old rows in different unique indexes, it is possible that a single row replaces multiple old rows.


The number of affected rows can be easily determined whether only one row is added to REPLACE, or whether REPLACE also replaces other rows.

: Check whether the number is 1 (ADD) or greater (replace ).

1. Try to Insert a new row into the table

2. When insertion fails due to a duplicate keyword error for the primary key or unique Keyword:

A. Delete conflicting rows with duplicate keyword values from the table

B. Try to Insert a new row into the table again

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT },...), (...),...
Or:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name = {expr | DEFAULT },...
Or:

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT...

Replace into 'table' ('unique _ column', 'num') VALUES ('$ unique_value', $ num); and insert into 'table' ('unique _ column ', 'num') VALUES ('$ unique_value', $ num) on duplicate update num = $ num; there are some differences.
The difference is that old records are deleted when replace into is used. If the table has an auto-increment primary key.
There is a problem.

First, because the new record and the old record have different primary key values, all the associations established with the old data primary key id in other tables will be damaged.

Second, frequent replace into causes the primary key value of the new record to increase rapidly.
One day. When the maximum value is reached, the data overflows because of the large amount of data. There is no way to insert new records. The data table is full, not because the space is insufficient, but because the value of the primary key cannot be increased.

From: http://www.cnblogs.com/martin1009/archive/2012/10/08/2714858.html

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.