Insert data
After a table is created, you can use the INSERT statement or other statements to insert row records into the table. This topic describes the usage and provides examples.
Prepare to insert data
Before you insert data, make sure that the following conditions are met:
-
You have connected to seekdb. For more information, see Connection methods.
-
You have the
INSERTprivilege on the table to be operated on. For information about how to view the privileges of the current user, see View user privileges. If you do not have the privilege, contact the administrator to grant the privilege. For information about how to directly grant privileges, see Directly grant privileges.
Insert data by using the INSERT INTO statement
We recommend that you use the INSERT statement and follow the suggestions below to insert data into a table.
The syntax of the INSERT INTO statement is as follows:
INSERT INTO table_name [(list_of_columns)] VALUES (list_of_values);
| Parameter | Required | Description |
|---|---|---|
| table_name | Yes | The name of the table into which data is to be inserted. |
| (list_of_columns) | No | The names of the columns into which data is to be inserted. |
| (list_of_values) | Yes | The values corresponding to the columns in list_of_columns. The values must be in the same order as the columns in list_of_columns. |
Suggestions
-
Before you insert data, we recommend that you learn about the columns in the table, including their types, valid values, and whether they allow NULL values.
You can run the
DESCstatement to query the column information.DESC test;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | int(11) | NO | | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set-
If the column attribute is
NOT NULL:-
If the column has a default value, you can omit the value of the column when you insert data. The system automatically inserts the default value into the column.
-
If the column does not have a default value, you must specify the value of the column when you insert data.
-
-
If the column attribute is
NULL, you can omit the value of the column when you insert data. The system automatically inserts aNULLvalue into the column.
-
-
Before you insert data, we recommend that you learn about the constraints defined on the columns in the table to avoid errors.
You can run the
DESCstatement to query theNOT NULLandPRIMARY KEYconstraints and theUNIQUEconstraints. You can query theinformation_schema.TABLE_CONSTRAINTSview to query theFOREIGN KEYandCHECKconstraints.
Insert a single row
You can run the INSERT statement to insert a single row. To insert multiple rows, you can run multiple single-row insert statements. To batch insert multiple rows, see [Batch insert multiple rows](#Batch insert multiple rows).
Assume that the information of the table into which data is to be inserted is as follows:
CREATE TABLE t_insert(
id int NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
value int,
gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Query OK, 0 rows affected
In this table, the id and name columns cannot be empty. The id column is the primary key and must be unique. The gmt_create column has a default value.
Example 1: Insert multiple rows by using multiple single-row insert statements.
Since the gmt_create column has a default value, you can omit the value of this column when you insert data.
INSERT INTO t_insert(id, name, value)
VALUES (1,'CN',10001);
Query OK, 2 rows affected
INSERT INTO t_insert(id, name, value)
VALUES(2,'US', 10002);
Query OK, 2 rows affected
Note that if the gmt_create column does not have a default value, you must specify the value of this column when you insert data. The statement is as follows:
INSERT INTO t_insert(id, name, value, gmt_create)
VALUES (3,'EN', 10003, current_timestamp ());
Query OK, 1 row affected
Batch insert multiple rows
You can also run a single INSERT statement that contains multiple VALUES clauses to batch insert multiple rows. A single multi-row insert statement is faster than multiple single-row insert statements.
The operation in Example 1 can also be performed by using the following statement:
Example 2: Batch insert multiple rows.
INSERT INTO t_insert(id, name, value)
VALUES (1,'CN',10001),(2,'US', 10002);
Query OK, 2 rows affected
In addition, when you need to back up table data or copy all records from one table to another, you can use the INSERT INTO ... SELECT ... FROM statement to batch insert data. In this statement, the SELECT clause serves as the values clause of the INSERT statement.
Example 3: Back up all data in the t_insert table to the t_insert_bak table.
SELECT * FROM t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 10001 | 2025-12-22 21:41:28 |
| 2 | US | 10002 | 2025-12-22 21:41:32 |
| 3 | EN | 10003 | 2025-12-22 21:41:43 |
+----+------+-------+---------------------+
3 rows in set
CREATE TABLE t_insert_bak(
id number NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
value number,
gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Query OK, 0 rows affected
INSERT INTO t_insert_bak SELECT * FROM t_insert;
Query OK, 3 rows affected
SELECT * FROM t_insert_bak;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 10001 | 2025-12-22 21:41:28 |
| 2 | US | 10002 | 2025-12-22 21:41:32 |
| 3 | EN | 10003 | 2025-12-22 21:41:43 |
+----+------+-------+---------------------+
3 rows in set
Avoid uniqueness constraint conflicts
When a uniqueness constraint is defined on a table, the database returns an error if you insert duplicate records. The error message is as follows:
INSERT INTO t_insert(id, name, value) VALUES (3,'UK', 10003),(4, 'JP', 10004);
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
You can use the INSERT IGNORE INTO statement or the INSERT INTO ON DUPLICATE KEY UPDATE statement to avoid this error.
Example:
-
When you use the
INSERT IGNORE INTOstatement to avoid constraint conflicts, theIGNOREkeyword ignores the impact of theINSERTfailure caused by constraint conflicts.INSERT IGNORE INTO t_insert(id, name, value)
VALUES (3,'UK', 10003),(4, 'JP', 10004);
Query OK, 1 row affected
SELECT * FROM t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 10001 | 2025-12-22 21:41:28 |
| 2 | US | 10002 | 2025-12-22 21:41:32 |
| 3 | EN | 10003 | 2025-12-22 21:41:43 |
| 4 | JP | 10004 | 2025-12-22 21:43:28 |
+----+------+-------+---------------------+
4 rows in setIn this example, the
INSERT IGNORE INTOstatement is used. The row of data(3,'UK', 10003)fails to be inserted, but no error is returned. -
When you use the
INSERT INTO ON DUPLICATE KEY UPDATEstatement to avoid constraint conflicts, you can specify the subsequent processing for duplicate primary keys or unique keys.info-
Specify
ON DUPLICATE KEY UPDATE column_name = expr: When the primary key or unique key to be inserted is duplicate, you can use thecolumn_name = exprassignment statement to update the data of the conflicting row in the table. Thecolumn_name = exprassignment statement can assign values to one or more columns in the conflicting row. When you assign values to multiple columns, separate the columns with commas. -
Do not specify
ON DUPLICATE KEY UPDATE column_name = expr: When the primary key or unique key to be inserted is duplicate, an error is returned when you insert data.
INSERT INTO t_insert(id, name, value) VALUES (3,'UK', 10003),(5, 'CN', 10005) ON DUPLICATE KEY UPDATE name = VALUES(name);
Query OK, 1 row affected
SELECT * FROM t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 10001 | 2025-12-22 21:41:28 |
| 2 | US | 10002 | 2025-12-22 21:41:32 |
| 3 | UK | 10003 | 2025-12-22 21:41:43 |
| 4 | JP | 10004 | 2025-12-22 21:43:28 |
| 5 | CN | 10005 | 2025-12-22 21:46:27 |
+----+------+-------+---------------------+
5 rows in setIn this example,
ON DUPLICATE KEY UPDATE name = VALUES(name)indicates that when the data to be inserted has a duplicate primary key value, thenamevalue of the conflicting row(3,'EN', 10003)in the table is updated to thenamevalue of the data to be inserted. Other rows that do not conflict are inserted normally. -
Use the INSERT OVERWRITE SELECT statement to insert data
The INSERT OVERWRITE SELECT statement is used to replace the existing data in a table or partition with the query result. This means that the data retrieved by the query will overwrite the data in the target table or partition.
The syntax of this statement is as follows:
INSERT [/*+PARALLEL(N)*/] OVERWRITE table_name [(list_of_columns)] [PARTITION (partition_name, ...)] select_stmt;
| Parameter | Description |
|---|---|
| PARALLEL(N) | Optional. Specifies the degree of parallelism for the overwrite operation. If not specified, the default degree of parallelism is 2. |
| table_name | Specifies the name of the table to insert data into. |
| list_of_columns | Specifies the columns in the table where data needs to be inserted. |
| select_stmt | Specifies the SELECT clause. For more information about query statements, see SELECT statement. |
| partition_name | The name of the partition to insert data into. When inserting into multiple partitions, separate the partition names with commas (,). |
Limitations of INSERT OVERWRITE SELECT
Limitations of table-level INSERT OVERWRITE SELECT
- This statement cannot be used in a multi-row transaction. To ensure smooth operation, execute the
SET autocommit = on;command to enable automatic transaction commit. - A table lock is placed on the write table, preventing any concurrent DDL operations on the same table. Concurrent DML operations will wait for the table lock to be released, and queries on the table are allowed during this time.
- The number of columns in the source data and the target table must strictly match for the overwrite operation. Otherwise, an error will occur.
- The data write operation for this statement uses a full bypass import method. Therefore, it is subject to the limitations of the full bypass import feature. For more information about bypass import, see the Limitations section in Bypass import data by using the INSERT INTO SELECT statement.
- Specifying a bypass import hint for this statement will result in an error.
- Due to the limitations of the PDML (Parallel Data Manipulation Language) framework, data cannot be imported in scenarios not supported by PDML. In such cases, the
INSERT OVERWRITE SELECTstatement will return a "not supported" error. For more information about parallel DML, see Parallel DML.
Partition-level INSERT OVERWRITE SELECT
Overview
- The source table can be a partitioned or non-partitioned table, with no specific partition type required.
- The target table can be a primary or secondary partitioned table.
- The target table can have local indexes and LOB (Large Object) data.
- You can specify all or part of the partitions in the target table. If the target table has secondary partitions, you can specify primary and secondary partitions separately or in combination.
- If no data from the source table matches the specified partitions in the target table, the data in those partitions will be cleared (overwritten with empty data).
- At the partition level, the
insert overwriteoperation supports the last-level partition of the target table being a hash or key partition.
Limitations
- The specified partitions must already exist; otherwise, an error will occur.
- Since this operation uses full bypass import, you do not need to add the
/*+ append */hint. Otherwise, an error will occur. - This statement cannot be used in a multi-row transaction. To ensure smooth operation, execute the
SET autocommit = on;command to enable automatic transaction commit. - If the data from the source table does not match the specified partitions in the target table after applying the partitioning rules, an error indicating that the partition does not exist will occur.
- Importing data to a table with auto-increment columns is not supported.
- Importing data to a table with global indexes or foreign keys is not supported.
- Importing data to an external table is not supported.
INSERT OVERWRITE SELECT example
Example 1
-
Create two test tables:
source_tbl1as the data source andtarget_tbl1as the target table.CREATE TABLE source_tbl1 (col1 INT, col2 VARCHAR(20), col3 INT);CREATE TABLE target_tbl1 (col1 INT, col2 VARCHAR(20), col3 INT); -
Insert sample data into the
source_tbl1table.INSERT INTO source_tbl1 VALUES (1, 'A1', 30),(2, 'B2', 25),(3, 'C3', 22); -
Insert sample data into the
target_tbl1table.INSERT INTO target_tbl1 VALUES (4, 'D4', 35),(5, 'E5', 28); -
Query the data in the
target_tbl1table.SELECT * FROM target_tbl1;The returned result is as follows:
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 4 | D4 | 35 |
| 5 | E5 | 28 |
+------+------+------+
2 rows in set -
Use the
INSERT OVERWRITE SELECTstatement to filter data fromsource_tbl1wherecol3is greater than 25 and insert this data intotarget_tbl1, replacing its original content.INSERT OVERWRITE target_tbl1 SELECT * FROM source_tbl1 WHERE col3 > 25; -
View the data in the
target_tbl1table after the replacement.SELECT * FROM target_tbl1;The returned result is as follows:
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 1 | A1 | 30 |
+------+------+------+
1 row in set
Example 2
Here is an example of using INSERT OVERWRITE PARTITION SELECT:
-
Create the partitioned table
t1_ins_overwrt.CREATE TABLE t1_ins_overwrt (c1 int primary key, c2 int) partition by range(c1)(
partition p0 values less than(10),
partition p1 values less than(20),
partition p2 values less than(30),
partition p3 values less than (MAXVALUE)
);
Query OK, 1 row affected -
Create the partitioned table
t2_ins_overwrt.CREATE TABLE t2_ins_overwrt (c1 int primary key, c2 int) partition by range(c1)(
partition p0 values less than(10),
partition p1 values less than(20),
partition p2 values less than(30),
partition p3 values less than (MAXVALUE)
);
Query OK, 1 row affected -
Insert some data into each table.
INSERT INTO t1_ins_overwrt values (1, 10), (12, 20), (23, 30), (34, 40), (45, 50);
Query OK, 5 rows affected (0.018 sec)
Records: 5 Duplicates: 0 Warnings: 0
INSERT INTO t2_ins_overwrt values (3, 60), (8, 80), (17, 170), (26, 260), (29, 290), (142, 1420);
Query OK, 6 rows affected (0.015 sec)
Records: 6 Duplicates: 0 Warnings: 0 -
Query the data in the
p3partition of each table.SELECT * FROM t1_ins_overwrt partition(p3);
+------+------+
| c1 | c2 |
+------+------+
| 34 | 40 |
| 45 | 50 |
+------+------+
2 rows in set (0.006 sec)
SELECT * FROM t2_ins_overwrt partition(p3);
+------+------+
| c1 | c2 |
+------+------+
| 142 | 1420 |
+------+------+
1 row in set (0.006 sec) -
Execute the following SQL statement to insert the data from the
p3partition of thet1_ins_overwrttable into thep3partition of thet2_ins_overwrttable.INSERT OVERWRITE t2_ins_overwrt partition(p3) select * from t1_ins_overwrt partition(p3);
Query OK, 2 rows affected (1.354 sec)
Records: 2 Duplicates: 0 Warnings: 0 -
Query the
p3partition of thet2_ins_overwrttable and find that the data has been overwritten.SELECT * FROM t2_ins_overwrt partition(p3);
+------+------+
| c1 | c2 |
+------+------+
| 34 | 40 |
| 45 | 50 |
+------+------+
2 rows in set (0.006 sec)
Example 3
-
Create the partitioned table
tbl1.CREATE TABLE tbl1(col1 INT,col2 INT)
PARTITION BY RANGE COLUMNS(col1)
SUBPARTITION BY HASH(col2) SUBPARTITIONS 3 (
PARTITION p0 VALUES LESS THAN(10),
PARTITION p1 VALUES LESS THAN(20));
Query OK, 0 rows affected -
Insert a row of data into the
tbl1table.INSERT INTO tbl1 (col1, col2) VALUES (2, 3);
INSERT INTO tbl1 (col1, col2) VALUES (15, 2); -
Query the
p0partition of thetbl1table to check if the data was inserted.SELECT * FROM tbl1 PARTITION (p0);The query result is as follows:
+------+------+
| col1 | col2 |
+------+------+
| 2 | 3 |
+------+------+SELECT * FROM tbl1 PARTITION (p1);The query result is as follows:
+------+------+
| col1 | col2 |
+------+------+
| 15 | 2 |
+------+------+ -
Create the partitioned table
tbl2.CREATE TABLE tbl2(col1 INT,col2 INT)
PARTITION BY RANGE COLUMNS(col1)
SUBPARTITION BY HASH(col2) SUBPARTITIONS 3 (
PARTITION p0 VALUES LESS THAN(10),
PARTITION p1 VALUES LESS THAN(20));
Query OK, 0 rows affected -
Insert data into the
tbl2table.INSERT INTO tbl2 (col1, col2) VALUES (0, 22);
INSERT INTO tbl2 (col1, col2) VALUES (18, 6); -
Query the
p0andp1partitions of thetbl2table to check if data was inserted.SELECT * FROM tbl2 PARTITION (p0);The query result is as follows:
+------+------+
| c1 | c2 |
+------+------+
| 0 | 22 |
+------+------+SELECT * FROM tbl2 PARTITION (p1);The query result is as follows:
+------+------+
| c1 | c2 |
+------+------+
| 18 | 6 |
+------+------+ -
Use the
insert overwritestatement to overwrite the data in thetbl1table into the specified partitionp1of thetbl2table.insert overwrite tbl2 partition (p1) select * from tbl1 partition (p1); -
Verify whether the data in the
p1partition of thetbl2table has been overwritten.SELECT * FROM tbl2 PARTITION (p1);The query result is as follows:
+------+------+
| col1 | col2 |
+------+------+
| 15 | 2 |
+------+------+The result indicates that the data in the
p1partition of thetbl2table has been overwritten.
Insert data by using the REPLACE INTO statement
In addition to the INSERT statement, you can also use the REPLACE INTO statement to insert data when there are no data records in the table or when there are data records in the table but no primary key or unique key conflicts. For more information about the REPLACE INTO statement, see REPLACE.
Examples:
-
After the
t_replacetable is created, use theREPLACE INTOstatement to insert data.CREATE TABLE t_replace(
id int NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
value int,
gmt_create timestamp NOT NULL DEFAULT current_timestamp
);
Query OK, 0 rows affected
REPLACE INTO t_replace VALUES(1,'CN',2001, current_timestamp ());
Query OK, 1 row affected
SELECT * FROM t_replace;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 2001 | 2025-12-23 10:55:53 |
+----+------+-------+---------------------+
1 row in set -
In the
t_replacetable with data records, use theREPLACE INTOstatement to insert data.SELECT * FROM t_replace;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 2001 | 2025-12-23 10:55:53 |
+----+------+-------+---------------------+
1 row in set
REPLACE INTO t_replace values(2,'US',2002, current_timestamp ());
Query OK, 1 row affected
SELECT * FROM t_replace;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | CN | 2001 | 2025-12-23 10:55:53 |
| 2 | US | 2002 | 2025-12-23 10:56:23 |
+----+------+-------+---------------------+
2 rows in set