REPLACE 
============================



描述 
-----------

该语句用于替换一个或多个记录到表。如果没有主键或唯一键冲突则插入记录，如果存在冲突则先删除冲突记录再插入新的记录。

格式 
-----------

```javascript
replace_stmt:
    REPLACE [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}
```





### 参数解释 



|       **参数**        |  **描述**   |
|---------------------|-----------|
| table_factor        | 替换的表名     |
| column_name_list    | 指定替换数据的列  |
| partition_name_list | 替换表指定的分区名 |



### 示例 

本示例基于如下表定义。

```javascript
OceanBase(admin@test)>create table test (c1 int primary key, c2 varchar(40));
Query OK, 0 rows affected (0.23 sec)
```



1. 替换 test 表中的行一和行二的值分别为 'hello alibaba' 和 'hello ob'。




```javascript
OceanBase(admin@test)>REPLACE INTO test VALUES (1, 'hello alibaba'),(2, 'hello ob');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
```



2. 查看 test 表中的行一和行二内容。




```javascript
OceanBase(admin@test)>SELECT * FROM test;
+----+---------------+
| c1 | c2            |
+----+---------------+
|  1 | hello alibaba |
|  2 | hello ob      |
+----+---------------+
2 rows in set (0.00 sec)
```



3. 替换表 test 中的行三和行二的内容分别为 'hello alibaba' 和 'hello oceanbase'。




```javascript
OceanBase(admin@test)>REPLACE INTO test VALUES (3, 'hello alibaba'),(2, 'hello oceanbase');
Query OK, 3 rows affected (0.00 sec)
Records: 2  Duplicates: 1  Warnings: 0
```



4. 查看 test 表中的行一、行二和行三的内容。




```javascript
OceanBase(admin@test)>SELECT * FROM test;
+----+-----------------+
| c1 | c2              |
+----+-----------------+
|  1 | hello alibaba   |
|  2 | hello oceanbase |
|  3 | hello alibaba   |
+----+-----------------+
3 rows in set (0.00 sec)
```



