Purpose
You can use this statement to add one or more records to a table.
Direct INSERT operations on subqueries are not supported. For example, the following statement cannot be executed: INSERT INTO (SELECT * FROM t1) VALUES(1, 1).
Syntax
INSERT [IGNORE] [INTO]
single_table_insert
[ON DUPLICATE KEY UPDATE update_asgn_list]
single_table_insert:
{dml_table_name values_clause
| dml_table_name '(' ')' values_clause
| dml_table_name '(' column_list ')' values_clause
| dml_table_name SET update_asgn_list}
dml_table_name:
table_name [PARTITION (partition_name,...)]
values_clause:
{{VALUES | VALUE} ({expr | DEFAULT},...) [, ...]
| select_stmt}
column_list
column_name [, ...]
update_asgn_list:
column_name = expr [, ...]
Parameters
| Parameter | Description |
|---|---|
| IGNORE | Ignores errors that occur during the execution of an INSERT statement. |
| column_list | The name of the column to which the data is inserted. Multiple columns must be separated with commas (,). |
| table_name | The name of the table to which the data is inserted. |
| partition_name | The name of the partition to which the data is inserted. |
| update_asgn_list | The statement that assigns values such as c1 = 2. |
| ON DUPLICATE KEY UPDATE | The action performed on duplicate primary keys or unique keys. If this parameter is specified, the configured value takes the place of the inserted value when the inserted primary key or unique key is duplicated. If this parameter is not specified, an error is returned when the inserted primary key or unique key is duplicated. |
Examples
Sample tables and their data are defined as follows:
obclient> CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT) PARTITION BY KEY(c1) PARTITIONS 4;
Query OK, 0 rows affected
obclient> CREATE TABLE t2(c1 INT PRIMARY KEY, c2 INT);
Query OK, 0 rows affected
obclient> SELECT * FROM t2;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+----+------+
4 rows in set
Insert a row of data into table
t1.obclient> INSERT INTO t1 VALUES(1,1); Query OK, 1 row affected obclient> SELECT * FROM t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | +----+------+ 1 row in setInsert multiple rows of data into the
t1table.obclient> INSERT t1 VALUES(1,1),(2,DEFAULT),(2+2,3*4); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 obclient> SELECT * FROM t1; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | | 2 | NULL | | 4 | 12 | +----+------+ 3 rows in setInsert a single row of data into partition
p0of tablet1.obclient> INSERT INTO t1 PARTITION(p0) (c1) VALUES(5); Query OK, 1 row affected obclient> SELECT * FROM t1 PARTITION(p0); +----+------+ | c1 | c2 | +----+------+ | 5 | NULL | +----+------+ 1 row in setWhen you insert a duplicate primary key value into table
t1, use theON DUPLICATE KEY UPDATEfunction to update the value.obclient> INSERT INTO t1 VALUES(1,1),(1,2) ON DUPLICATE KEY UPDATE c1=100; Query OK, 3 rows affected Records: 2 Duplicates: 1 Warnings: 0 obclient> SELECT * FROM t1; +-----+------+ | c1 | c2 | +-----+------+ | 100 | 1 | +-----+------+ 1 row in setInsert values into an updatable view
v.obclient> CREATE VIEW v AS SELECT * FROM t1; Query OK, 0 rows affected obclient> INSERT INTO v VALUES (1,1); Query OK, 1 row affected obclient> SELECT * FROM v; +----+------+ | c1 | c2 | +----+------+ | 1 | 1 | +----+------+ 1 row in setInsert the query results of table
t1into tablet2.obclient> INSERT INTO t1 SELECT * FROM t2 WHERE c1>2; Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0 obclient> SELECT * FROM t1; +----+------+ | c1 | c2 | +----+------+ | 3 | 3 | | 4 | 4 | +----+------+ 2 rows in set