# REPLACE Statement

__*REPLACE*__

+ Use the REPLACE statement to replace an existing row with a new one in a table with a primary key or unique constraint
+ For tables without primary keys or unique constraints, it works the same way as an INSERT statement
+ The number of rows return by the statement is the sum of the number of rows deleted and inserted
  + rows affected equals one: One row was inserted, no rows were deleted
  + rows affected greater than one: One or more old rows were deleted before inserting a new row.
```7
tidb> SELECT * FROM test.r1;
+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | B    |
+----+------+

tidb> replace into test.r1 (id,name) values (2,'C');
Query OK, ~!@2 rows affected@!~ (0.00 sec)

tidb> SELECT * FROM test.r1;
+----+------+
| id | name |
+----+------+
|  1 | A    |
|  2 | C    |
+----+------+
```
------------------------------------------------------------

Semantically, the REPLACE statement is a combination of a DELETE statement and an INSERT statement that can be used to simplify application code.