W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
該語句用于添加一個或多個記錄到表中。
INSERT [hint_options] { single_table_insert | multi_table_insert }
single_table_insert:
{ INTO insert_table_clause opt_nologging '(' column_list ')' values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
| INTO insert_table_clause opt_nologging '(' ')' values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
| INTO insert_table_clause opt_nologging values_clause [{ RETURNING | RETURN } returning_exprs [into_clause]]
}
opt_nologging: { NOLOGGING | /*EMPTY*/ }
returning_exprs:
projection [, ...]
insert_into_clause:
{ INTO into_var_list | BULK COLLECT INTO into_var_list}
into_var_list:
{ USER_VARIABLE | ref_name } [, ...]
values_clause:
VALUES ({ expr | DEFAULT } [, { expr | DEFAULT } ]... )
multi_table_insert:
{ ALL { insert_into_clause [ values_clause ] [error_logging_clause] }
| conditional_insert_clause
} subquery
conditional_insert_clause:
[ ALL | FIRST ]
WHEN condition
THEN insert_into_clause
[ values_clause ]
[ error_logging_clause ]
[ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
[ WHEN condition
THEN insert_into_clause
[ values_clause ]
[ error_logging_clause ]
[ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
]...
[ ELSE insert_into_clause
[ values_clause ]
[ error_logging_clause ]
[ insert_into_clause [ values_clause ] [ error_logging_clause ] ]...
]
error_logging_clause:
LOG ERRORS [ INTO [schema.] table ] [ (simple_expression) ] [ REJECT LIMIT { integer | UNLIMITED } ]
參數(shù) |
描述 |
---|---|
hint_options |
指定 Hint 選項。 |
single_table_insert |
單表插入。 |
insert_table_clause |
指定的插入的表,可以是基表、可更新視圖、特殊子查詢。 |
opt_nologging |
盡量減少插入時的日志信息。 |
column_list |
指定要插入的列名。 |
returning_exprs |
返回插入數(shù)據(jù)之后的投影列。 |
insert_into_clause |
將插入數(shù)據(jù)之后的列值插入到指定列表中。 |
multi_table_insert |
多表插入。 |
conditional_insert_clause |
帶條件的多表插入。
|
注意
特殊子查詢指的類似于可更新視圖對應的子查詢,這類子查詢不應該包含復雜的算子(比如 group by/distinct/window function 等)
示例表及數(shù)據(jù)基于以下定義:
obclient>CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT,c3 INT);
Query OK, 0 rows affected (0.10 sec)
obclient> SELECT * FROM t;
Empty set (0.02 sec)
單表插入:向表 t 中插入一行數(shù)據(jù)。
obclient> INSERT INTO t VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
obclient> SELECT * FROM t;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 2 | 3 |
+----+------+------+
1 row in set (0.00 sec)
單表插入:直接向子查詢中插入數(shù)據(jù)
obclient>INSERT INTO (SELECT * FROM t) VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
obclient> SELECT * FROM t;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 2 | 3 |
+----+------+------+
1 row in set (0.01 sec)
單表插入:包含 RETURNING
子句。
obclient>INSERT INTO t VALUES(1,2,3) RETURNING c1;
+----+
| C1 |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
obclient>SELECT * FROM t;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 2 | 3 |
+----+------+------+
1 row in set (0.00 sec)
普通的多表插入:當表 t 中有至少一行數(shù)據(jù)時,向表 t1 插入一行數(shù)據(jù) (1,1,1),向表 t2 插入一行數(shù)據(jù) (2,2,2)。
obclient>INSERT ALL INTO t1 VALUES(1,1,1) INTO t2 VALUES(2,2,2) SELECT * FROM t
WHERE ROWNUM< 2;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
obclient>SELECT * FROM t1;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 1 | 1 |
+----+------+------+
1 row in set (0.02 sec)
obclient>SELECT * FROM t2;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 2 | 2 | 2 |
+----+------+------+
1 row in set (0.01 sec)
帶條件的多表插入:使用 INSERT ALL
,當表 t 中 c2 的值大于 1 時,向表 t1 中插入數(shù)據(jù) (1,1,1);當表 t 中 c3 的值大于 1 時,向表 t2 中插入數(shù)據(jù) (2,2,2);如果都不滿足,則向表 t1 中插入數(shù)據(jù) (3,3,3)。
obclient>DELETE FROM (SELECT * FROM t);
Query OK, 4 rows affected (0.04 sec)
obclient>INSERT INTO t VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
obclient>INSERT ALL
WHEN c2 > 1 THEN INTO t1 VALUES(1,1,1)
WHEN c3 > 1 THEN INTO t2 VALUES(2,2,2)
ELSE INTO t1 VALUES(3,3,3) SELECT c2,c3 FROM t;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
obclient>SELECT * FROM t1;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 1 | 1 |
+----+------+------+
1 row in set (0.00 sec)
obclient>SELECT * FROM t2;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 2 | 2 | 2 |
+----+------+------+
1 row in set (0.00 sec)
帶條件的多表插入:使用 INSERT FIRST
,當表 t 中 c2 的值大于 1 時,向表 t1 中插入數(shù)據(jù) (1,1,1),向表 t2 中插入數(shù)據(jù) (4,4,4);當表 t 中 c3 的值大于 1 時,向表 t2 中插入數(shù)據(jù) (2,2,2);如果都不滿足,則向表 t1 中插入數(shù)據(jù) (3,3,3)。
obclient>DELETE FROM (SELECT * FROM t);
Query OK, 4 rows affected (0.04 sec)
obclient>INSERT INTO t VALUES(1,2,3);
Query OK, 1 row affected (0.00 sec)
obclient>INSERT ALL
WHEN c2 > 1 THEN INTO t1 VALUES(1,1,1) INTO t2 VALUES(4,4,4)
when c3 > 1 THEN INTO t2 VALUES(2,2,2)
ELSE INTO t1 VALUES(3,3,3) SELECT c2,c3 FROM t;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
obclient>SELECT * FROM t1;
+----+------+------+
| C1 | C2 | C3 |
+----+------+------+
| 1 | 1 | 1 |
+----+------+------+
1 row in set (0.00 sec)
obclient>SELECT * FROM t2;
+------+------+------+
| C1 | C2 | C3 |
+------+------+------+
| 4 | 4 | 4 |
| 2 | 2 | 2 |
+------+------+------+
2 rows in set (0.00 sec)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: