|
| 1 | +--- |
| 2 | +keywords: [SQL REPLACE, SQL syntax, SQL examples, inserting records, SQL data manipulation] |
| 3 | +description: Describes the SQL REPLACE statement for adding records to a table in GreptimeDB, including syntax, examples for inserting single and multiple records. |
| 4 | +--- |
| 5 | + |
| 6 | +# REPLACE |
| 7 | + |
| 8 | +The `REPLACE` statement is used to insert new records into a table. In GreptimeDB, this statement is exactly the same as the `INSERT` statement. Please refer to [`INSERT`](/reference/sql/insert.md) for more details. |
| 9 | + |
| 10 | +## `REPLACE INTO` Statement |
| 11 | + |
| 12 | +### Syntax |
| 13 | + |
| 14 | +The syntax for the REPLACE INTO statement is as follows: |
| 15 | + |
| 16 | +```sql |
| 17 | +REPLACE INTO table_name (column1, column2, column3, ...) |
| 18 | +VALUES (value1, value2, value3, ...); |
| 19 | +``` |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +Here is an example of an `REPLACE INTO` statement that inserts a record into a table named `system_metrics`: |
| 24 | + |
| 25 | +```sql |
| 26 | +REPLACE INTO system_metrics (host, idc, cpu_util, memory_util, disk_util, ts) |
| 27 | +VALUES |
| 28 | + ("host1", "idc_b", 50.0, 66.7, 40.6, 1667446797462); |
| 29 | +``` |
| 30 | + |
| 31 | +Here is an example of an `REPLACE INTO` statement that inserts multiple records into the `system_metrics` table: |
| 32 | + |
| 33 | +```sql |
| 34 | +REPLACE INTO system_metrics (host, idc, cpu_util, memory_util, disk_util, ts) |
| 35 | +VALUES |
| 36 | + ("host1", "idc_a", 11.8, 10.3, 10.3, 1667446797460), |
| 37 | + ("host2", "idc_a", 80.1, 70.3, 90.0, 1667446797461), |
| 38 | + ("host1", "idc_c", 50.1, 66.8, 40.8, 1667446797463); |
| 39 | +``` |
0 commit comments