Purpose
You can use this statement to replace one or more records in a table. If no primary key or unique key conflict exists, records are directly inserted. If a conflict exists, the conflicting records are deleted. Then, the new records are inserted.
Syntax
replace_stmt:
REPLACE [LOW_PRIORITY] [INTO] table_factor [PARTITION (partition_name_list)] [(column_name_list)]
{VALUES | VALUE} column_value_lists;
partition_name_list:
partition_name [, partition_name ...]
column_name_list:
column_name [, column_name ...]
column_value_lists:
(column_value_list) [, (column_value_list) ...]
column_value_list:
column_value [, column_value ...]
column_value:
{expression | DEFAULT}
Parameters
| Parameter | Description |
|---|---|
| LOW_PRIORITY | Optional. Specifies to defer the execution of the REPLACE statement until no other clients read data from the table. In the current version, only the keyword syntax is supported. The functionality does not actually take effect. |
| table_factor | The name of the table in which data is to be replaced. |
| column_name_list | The name of the column in which data is to be replaced. |
| partition_name_list | The name of the partition in which data is to be replaced. |
Examples
The table used in the examples is defined as follows:
obclient> CREATE TABLE test (c1 INT PRIMARY KEY, c2 VARCHAR(40));
Query OK, 0 rows affected
Replace the values in Row 1 and Row 2 of the
testtable with'hello alibaba'and'hello ob', respectively.obclient> REPLACE INTO test VALUES (1, 'hello alibaba'),(2, 'hello ob'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0Query the data in Row 1 and Row 2 of the
testtable.obclient> SELECT * FROM test; +----+---------------+ | c1 | c2 | +----+---------------+ | 1 | hello alibaba | | 2 | hello ob | +----+---------------+ 2 rows in setReplace the values in Row 3 and Row 2 of the
testtable with'hello alibaba'and'hello oceanbase', respectively.obclient> REPLACE INTO test VALUES (3, 'hello alibaba'),(2, 'hello oceanbase'); Query OK, 3 rows affected Records: 2 Duplicates: 1 Warnings: 0Query the data in Row 1, Row 2, and Row 3 of the
testtable.obclient> SELECT * FROM test; +----+-----------------+ | c1 | c2 | +----+-----------------+ | 1 | hello alibaba | | 2 | hello oceanbase | | 3 | hello alibaba | +----+-----------------+ 3 rows in set