OceanBase INSERT

2021-06-11 17:45 更新

描述

該語(yǔ)句用于添加一個(gè)或多個(gè)記錄到表中。

格式

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:
    tbl_name [PARTITION (partition_name,...)]

values_clause:
     {{VALUES | VALUE} ({expr | DEFAULT},...) [, ...]
      | select_stmt}

column_list
    column_name [, ...]

update_asgn_list:
     column_name = expr [, ...]

參數(shù)解釋

參數(shù)

描述

IGNORE

在 INSERT 語(yǔ)句執(zhí)行過(guò)程中發(fā)生的錯(cuò)誤將會(huì)被忽略。

column_list

用于指定插入數(shù)據(jù)的列,同時(shí)插入多列時(shí),用“,”隔開(kāi)。

tbl_name

指定要插入的表名。

partition_name

插入表指定的分區(qū)名。

update_asgn_list

賦值語(yǔ)句,例如:“c1 = 2”

ON DUPLICATE KEY UPDATE

指定對(duì)重復(fù)主鍵或唯一鍵的處理。如果指定了 ON DUPLICATE KEY UPDATE,當(dāng)要插入的主鍵或唯一鍵有重復(fù)時(shí),會(huì)用配置值替換待插入的值;如果不指定 ON DUPLICATE KEY UPDATE,當(dāng)要插入的主鍵或唯一鍵有重復(fù)時(shí),插入報(bào)錯(cuò)。

示例

示例表及數(shù)據(jù)基于以下定義:

obclient>create table t1(c1 int primary key, c2 int) partition by key(c1) partitions 4;
Query OK, 0 rows affected (0.16 sec)

obclient>create table t2(c1 int primary key, c2 int);
Query OK, 0 rows affected (0.16 sec)
obclient>select * from t2;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.06 sec)
  • 向表 t1 中插入一行數(shù)數(shù)據(jù)。
obclient>insert into t1 values(1,1);
Query OK, 1 row affected (0.01 sec)

obclient>select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
+----+------+
1 row in set (0.04 sec)
  • 向表 t1 中插入多行數(shù)據(jù)。
obclient>insert t1 values(1,1),(2,default),(2+2,3*4);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

obclient>select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
|  2 | NULL |
|  4 |   12 |
+----+------+
3 rows in set (0.02 sec)
  • 向表 t1 指定的 p0 分區(qū)插入單行數(shù)據(jù)。
obclient>insert into t1 partition(p0) (c1) values(5);
Query OK, 1 row affected (0.02 sec)
obclient>select * from t1 partition(p0);
+----+------+
| c1 | c2   |
+----+------+
|  5 | NULL |
+----+------+
1 row in set (0.01 sec)
  • 將表 t2 的查詢結(jié)果作為數(shù)據(jù)插入表 t1。
obclient>insert into t1 select * from t2;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

obclient>select * from t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.01 sec)
  • 向表 t1 中插入重復(fù)主鍵值時(shí)利用 ON DUPLICATE KEY UPDATE 功能進(jìn)行值更新
obclient>insert into t1 values(1,1),(1,2) ON DUPLICATE KEY UPDATE c1=100;
Query OK, 3 rows affected (0.01 sec)
Records: 2  Duplicates: 1  Warnings: 0

obclient>select * from t1;
+-----+------+
| c1  | c2   |
+-----+------+
| 100 |    1 |
+-----+------+
1 row in set (0.02 sec)
  • 對(duì)可更新視圖 v 的插入值
obclient>create view v as select * from t1;
Query OK, 0 rows affected (0.07 sec)
obclient>insert into v values(1,1);
Query OK, 1 row affected (0.01 sec)

obclient>select * from v;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
+----+------+
1 row in set (0.02 sec)

注意事項(xiàng)

INSERT 語(yǔ)句不支持直接對(duì)子查詢進(jìn)行插入操作,比如:?insert into (select * from t1) values(1, 1);?


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)