After you create a table, you can use the INSERT statement or other statements to insert records into the table. This topic describes how to use related statements.
Prerequisites
Before you insert data into a table, make sure that:
You have connected to an Oracle tenant of OceanBase Database. For more information about how to connect to OceanBase Database, see Connection methods.
Note
You can query the
oceanbase.DBA_OB_TENANTSview in thesystenant to confirm the mode of the tenant to which you have logged on.You have the
INSERTprivilege on the target table. For more information about how to view your privileges, see View user privileges. If you do not have the required privileges, contact the administrator to obtain the privileges. For more information, see Grant direct privileges.
Use the INSERT INTO statement to insert data
You can use the INSERT INTO statement 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 table into which data is to be inserted. |
| (list_of_columns) | No | The table columns into which data is to be inserted. |
| (list_of_values) | Yes | The values of the columns specified by list_of_columns. The values and columns must be in one-to-one mapping. |
Suggestions on data insertion
Before you insert data, we recommend that you learn the information about all columns of the table, including the column types, valid values, and whether null values are allowed.
You can use the
DESCstatement to query information about columns.obclient [SYS]> DESC ordr; +-------+--------------+------+-----+---------+-------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +-------+--------------+------+-----+---------+-------+ | C1 | NUMBER | YES | NULL | NULL | NULL | | C2 | VARCHAR2(50) | YES | NULL | NULL | NULL | +-------+--------------+------+-----+---------+-------+ 2 rows in setFor a
NOT NULLcolumn, take note of the following points:If the column has a default value, you do not need to specify a value for this column when you insert data. The system inserts the default value into this column if you do not specify a value.
If no default value has been specified for the column, you must specify a value for the column when you insert data.
For a
NULLcolumn, you do not need to specify a value for this column when you insert data. The system inserts aNULLvalue into this column.
Before you insert data into a table, we recommend that you learn the information about constraints defined on columns of the table. This is to avoid errors when you insert data.
You can use the
DESCstatement to queryNOT NULL,PRIMARY KEY, andUNIQUEconstraints. You can query theALL_CONSTRAINTS,DBA_CONSTRAINTS, orUSER_CONSTRAINTSview forFOREIGN KEYandCHECKconstraints.
Insert a single row of data
You can use the INSERT statement to insert a single row of data. To insert multiple records, you can execute the single-row insertion statement multiple times. To perform batch insertion, see the Insert multiple rows of data in batches section.
Assume that the information about the table into which data is to be inserted is as follows:
obclient [SYS]> CREATE TABLE t_insert(
id number NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
value number,
gmt_create DATE NOT NULL DEFAULT sysdate
);
Query OK, 0 rows affected
The id and name columns of the table cannot be empty. The id column is the primary key column with a UNIQUE constraint, and therefore cannot have duplicate values. A default value is specified for the gmt_create column.
Example 1: Insert multiple rows of data into a table by executing the single-row insertion statement multiple times.
A default value is specified for the gmt_create column. Therefore, you do not need to specify a value for this column when you insert data.
obclient [SYS]> INSERT INTO t_insert(id, name, value)
VALUES (1,'CN',10001);
Query OK, 2 rows affected
obclient [SYS]> INSERT INTO t_insert(id, name, value)
VALUES(2,'US', 10002);
Query OK, 2 rows affected
If no default value is specified for the gmt_create column, you must specify a value for this column when you insert data by using the following statement:
obclient [SYS]> INSERT INTO t_insert(id, name, value, gmt_create)
VALUES (3,'EN', 10003, sysdate);
Query OK, 1 row affected
Insert multiple rows of data in batches
To insert multiple records, you can also use an INSERT statement that contains multiple VALUES clauses. A single multi-row insertion statement is executed faster than multiple single-row insertion statements.
The operation in Example 1 can be completed by using the statement in Example 2.
Example 2: Insert multiple rows of data in batches.
obclient [SYS]> INSERT INTO t_insert(id, name, value)
VALUES (1,'CN',10001),(2,'US', 10002);
Query OK, 2 rows affected
To back up a table or copy all records in a table to another table, you can use the INSERT INTO ... SELECT ... FROM statement as the VALUES clause in the INSERT statement for batch insertion.
Example 3: Back up all data of the t_insert table to the t_insert_bak table.
obclient [SYS]> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
| 1 | CN | 10001 | 31-OCT-22 |
| 2 | US | 10002 | 31-OCT-22 |
| 3 | EN | 10003 | 31-OCT-22 |
+----+------+-------+------------+
3 rows in set
obclient [SYS]> CREATE TABLE t_insert_bak(
id number NOT NULL PRIMARY KEY,
name varchar(10) NOT NULL,
value number,
gmt_create DATE NOT NULL DEFAULT sysdate
);
Query OK, 0 rows affected
obclient [SYS]> INSERT INTO t_insert_bak SELECT * FROM t_insert;
Query OK, 2 rows affected
Records: 3 Duplicates: 0 Warnings: 0
obclient [SYS]> SELECT * FROM t_insert_bak;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
| 1 | CN | 10001 | 31-OCT-22 |
| 2 | US | 10002 | 31-OCT-22 |
| 3 | EN | 10003 | 31-OCT-22 |
+----+------+-------+------------+
3 rows in set
Use the INSERT OVERWRITE SELECT statement to insert data
The INSERT OVERWRITE SELECT statement overwrites existing data in the target table with the query results. This operation is atomic. If any exception occurs during this operation, it is rolled back as a whole.
The syntax is as follows:
INSERT [/*+PARALLEL(N)*/] OVERWRITE table_name [(list_of_columns)] [PARTITION (partition_name, ...) ] select_stmt;
| Parameter | Description |
|---|---|
| PARALLEL(N) | Optional. The degree of parallelism (DOP) for overwriting. If this parameter is not specified, the default DOP 2 is used. |
| table_name | The name of the table into which data is to be inserted. |
| list_of_columns | The table columns into which data is to be inserted. |
| select_stmt | The SELECT clause. For more information, see SIMPLE SELECT. |
| partition_name | The name of the partition into which data is to be inserted. Separate multiple partitions with commas (,). |
Limitations on using INSERT OVERWRITE SELECT
Use INSERT OVERWRITE SELECT to insert data into tables
- The statement cannot be executed in a multi-row transaction. Therefore, you must first execute the
SET autocommit = on;statement to enable auto commit of transactions. - The target table is locked during the execution of this statement. During this process, you cannot initiate any DDL operations on this table. If you initiate a DML operation, it waits for the table lock to be released, or until it times out. However, you can still initiate queries on the table during this process.
- The number of columns in the source data to be inserted by the statement must exactly match that in the target table to be overwritten. Otherwise, an error is returned.
- The statement writes data in full direct load mode, and is therefore subject to the full direct load feature. For more information about the direct load feature, see Limitations in Import data through direct load by using the INSERT INTO SELECT statement.
- If you specify a hint for direct load in this statement, an error is returned.
- This statement is subject to the Parallel Data Manipulation Language (PDML) framework. You cannot use this statement to import data in scenarios that do not support PDML. If you do, an error is returned. For more information about PDML, see Parallel DML.
Use INSERT OVERWRITE SELECT to insert data into partitions
Description
- The source table can be a partitioned table of any partitioning type or a non-partitioned table.
- The target table can be either a partitioned table or subpartitioned table.
- The target table can contain a local index or a large object (LOB) column.
- You can specify all or partial partitions in the target table. If the target table has subpartitions, you can specify them separately or together with partitions.
- If a specified partition in the target table is not hit by data from the source table after calculation based on the target table partitioning rule, an error is returned to indicate that the specified partition does not exist.
- If a specified partition in the target table is not hit by data from the source table, data in this partition is cleared and overwritten by null values.
Limitations
- If the specified partition does not exist, an error is returned.
- In full direct load mode, you do not need to add a
/*+ append */hint. Otherwise, an error is returned. - The statement cannot be executed in a multi-row transaction. Therefore, you must first execute the
SET autocommit = on;statement to enable auto commit of transactions. - The target table cannot be a hash- or key-partitioned table. If the target table is a subpartitioned table, no limitations are exerted on the partitions, but the subpartitions cannot be of the hash or key types.
- The target table cannot contain identity columns.
- The target table cannot contain a global index or a foreign key.
- The target table cannot be an external table.
Examples
Example 1
Enable auto commit of transactions.
SET autocommit = on;Create two test tables:
source_tbl1that serves as the data source andtarget_tbl1that serves as the target table.CREATE TABLE source_tbl1 (col1 INT, col2 VARCHAR2(20), col3 INT);CREATE TABLE target_tbl1 (col1 INT, col2 VARCHAR2(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 return result is as follows:
+------+------+------+ | COL1 | COL2 | COL3 | +------+------+------+ | 4 | D4 | 35 | | 5 | E5 | 28 | +------+------+------+ 2 rows in setUse the
INSERT OVERWRITE SELECTstatement to select the data whosecol3values exceed25from thesource_tbl1table and insert the data into thetarget_tbl1table in overwriting mode.INSERT OVERWRITE target_tbl1 SELECT * FROM source_tbl1 WHERE col3 > 25;Query the data in the
target_tbl1table.SELECT * FROM target_tbl1;The return result is as follows:
+------+------+------+ | COL1 | COL2 | COL3 | +------+------+------+ | 1 | A1 | 30 | +------+------+------+ 1 row in set
Example 2
Use the INSTER OVERWRITE PARTITION statement to insert data.
Enable auto commit of transactions.
obclient [SYS]> SET autocommit = on;Create a partitioned table named
t1_ins_overwrt.obclient [SYS]> 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, 0 rows affected (0.223 sec)Create another partitioned table named
t2_ins_overwrtobclient [SYS]> 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, 0 rows affected (0.092 sec)Insert several data records into the two tables, respectively.
obclient [SYS]> insert into t1_ins_overwrt values (12, 20), (34, 40), (45, 50); Query OK, 3 rows affected (0.002 sec) Records: 3 Duplicates: 0 Warnings: 0 obclient [SYS]> insert into t2_ins_overwrt values (3, 60), (8, 80), (17, 170), (26, 260), (29, 290), (142, 1420); Query OK, 6 rows affected (0.003 sec) Records: 6 Duplicates: 0 Warnings: 0Query the data in the p1 partition in each table.
obclient [SYS]> select * from t1_ins_overwrt partition(p1); +------+------+ | C1 | C2 | +------+------+ | 12 | 20 | +------+------+ 1 row in set (0.006 sec) obclient [SYS]> select * from t2_ins_overwrt partition(p1); +------+------+ | C1 | C2 | +------+------+ | 17 | 170 | +------+------+ 1 row in set (0.007 sec)Run the following SQL command to insert data in the p1 partition in the
t1_ins_overwrttable into the same partition in thet2_ins_overwrttable.obclient [SYS]> insert overwrite t2_ins_overwrt partition(p1) select * from t1_ins_overwrt partition(p1); Query OK, 3 rows affected (1.360 sec) Records: 3 Duplicates: 0 Warnings: 0Query the data in the p1 partition in the
t2_ins_overwrttable to verify that the original data has been overwritten.obclient [SYS]> SELECT * FROM t2_ins_overwrt partition(p1); +------+------+ | C1 | C2 | +------+------+ | 12 | 20 | +------+------+ 1 row in set (0.020 sec)
Use the MERGE INTO statement to insert data
In a batch processing task, a large number of INSERT operations may need to be performed on the target table. If the to-be-inserted records in the source table do not exist in the target table, you can also use the MERGE INTO statement to insert the records of the source table into the target table. For more information, see Replace data.
Note
If you use the INSERT statement to insert data into a table that has a UNIQUE constraint, the system returns an error when duplicate records are inserted. In this case, you can use the MERGE INTO statement to update records in the table to avoid conflicts with the UNIQUE constraint.
Use the INSERT statement to insert data through a DBLink
The current OceanBase Database version allows you to write data into OceanBase Database in Oracle mode and into Oracle Database through a DBLink.
Here is an example of inserting a row (11,11) into the t1 table in the remote database through a DBLink:
obclient> SELECT * FROM t1@ob_dblink;
+------+------+
| C1 | C2 |
+------+------+
| 1 | 1 |
+------+------+
1 row in set
obclient> INSERT INTO t1@ob_dblink VALUES (11,11);
Query OK, 1 row affected
obclient> commit;
Query OK, 0 rows affected
obclient> SELECT * FROM t1@ob_dblink;
+------+------+
| C1 | C2 |
+------+------+
| 1 | 1 |
| 11 | 11 |
+------+------+
2 rows in set
In the Oracle mode of OceanBase Database, you can use the INSERT, DELETE, UPDATE, and MERGE INTO statements to write data from local tables to remote tables. For more information, see Use a DBLink to modify data in a remote database.