meaning of replace into in mysql
REPLACE
works exactly like INSERT
, except that if an old row in the table has the same value as a new row for a PRIMARY KEY
or a UNIQUE
index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT
Syntax”.
Note that unless the table has a PRIMARY KEY
or UNIQUE
index, using a REPLACE
statement makes no sense. It becomes equivalent to INSERT
, because there is no index to be used to determine whether a new row duplicates another.
Values for all columns are taken from the values specified in the REPLACE
statement. Any missing columns are set to their default values, just as happens for INSERT
. You cannot refer to values from the current row and use them in the new row. If you use an assignment such as SET col_name
= col_name
+ 1
, the reference to the column name on the right hand side is treated as DEFAULT(col_name
)
, so the assignment is equivalent to SETcol_name
= DEFAULT(col_name
) + 1
.
The REPLACE
statement returns a count to indicate the number of rows affected. This is the sum of the rows deleted and inserted. If the count is 1 for a single-row REPLACE
, a row was inserted and no rows were deleted. If the count is greater than 1, one or more old rows were deleted before the new row was inserted. It is possible for a single row to replace more than one old row if the table contains multiple unique indexes and the new row duplicates values for different old rows in different unique indexes.
The affected-rows count makes it easy to determine whether REPLACE
only added a row or whether it also replaced any rows: Check whether the count is 1 (added) or greater (replaced).
If you are using the C API, the affected-rows count can be obtained using the mysql_affected_rows()
function.
Currently, you cannot replace into a table and select from the same table in a subquery.
MySQL uses the following algorithm for REPLACE
(and LOAD DATA ... REPLACE
):
-
Try to insert the new row into the table
-
While the insertion fails because a duplicate-key error occurs for a primary key or unique index:
-
Delete from the table the conflicting row that has the duplicate key value
-
Try again to insert the new row into the table
When i use REPLACE i can not make it work as expected. Included is an
example.
I have the following 'test' table;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(255) | | PRI | | |
| number | int(5) | | | 0 | |
+--------+--------------+------+-----+---------+-------+
mysql> REPLACE INTO test VALUES ('agent', number+1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+-------+--------+
| name | number |
+-------+--------+
| agent | 1 |
+-------+--------+
1 row in set (0.00 sec)
This works fine, so i do it again:
mysql> REPLACE INTO test VALUES ('agent', number+1);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+-------+--------+
| name | number |
+-------+--------+
| agent | 2 |
+-------+--------+
1 row in set (0.00 sec)
Still works as expected
mysql> REPLACE INTO test VALUES ('agent', number+1);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from test;
+-------+--------+
| name | number |
+-------+--------+
| agent | 3 |
+-------+--------+
1 row in set (0.00 sec)
Still, but....
mysql> REPLACE INTO test VALUES ('agent2', number+1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+--------+--------+
| name | number |
+--------+--------+
| agent | 3 |
| agent2 | 4 |
+--------+--------+
2 rows in set (0.00 sec)
Now i had expected 'agent2' to have the value '1', but it gets the last
value from the table - and it touches 2 rows (not 1 row).