OceanBase UPDATE

2021-06-11 17:05 更新

描述

該語句用于修改表中的字段值。

格式

UPDATE [IGNORE] table_references
    SET update_asgn_list
    [WHERE where_condition] 
    [ORDER BY order_list]
    [LIMIT row_count];

table_references:
    tbl_name [PARTITION (partition_name,...)] [, ...]

update_asgn_list:
    column_name = expr [, ...]

order_list: 
    column_name [ASC|DESC] [, column_name [ASC|DESC]…]

參數(shù)解釋

參數(shù)

描述

IGNORE

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

table_references

指定修改表名,多表修改時(shí),表名直接‘,’作為間隔。

where_condition

指定過濾條件。

row_count

限制的行數(shù)。

tbl_name

插入表名。

partition_name

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

column_name

列名。

column_name ASC

按列名升序修改。

column_name DESC

按列名降序修改。

注意事項(xiàng)

不管是多表還是單表更新都不支持直接對(duì)子查詢進(jìn)行更新值操作,例如:?update (select * from t1) set c1 = 100;?

示例

  1. 創(chuàng)建示例表 t1 和 t2。
  2. OceanBase(admin@test)>create table t1(c1 int primary key, c2 int);
    Query OK, 0 rows affected (0.16 sec)
    OceanBase(admin@test)>select * from t1;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |    1 |
    |  2 |    2 |
    |  3 |    3 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.06 sec)
    
    OceanBase(admin@test)>create table t2(c1 int primary key, c2 int) partition by key(c1) partitions 4;
    Query OK, 0 rows affected (0.19 sec)
    OceanBase(admin@test)>select * from t2;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  5 |    5 |
    |  1 |    1 |
    |  2 |    2 |
    |  3 |    3 |
    +----+------+
    4 rows in set (0.02 sec)
  3. 將表 t1 中 "t1.c1=1" 對(duì)應(yīng)的那一行數(shù)據(jù)的 c2 列值修改為 100。
  4. OceanBase(admin@test)>update t1 set t1.c2 = 100 where t1.c1 = 1;
    Query OK, 1 row affected (0.02 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    OceanBase(admin@test)>select * from t1;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |  100 |
    |  2 |    2 |
    |  3 |    3 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.01 sec)
  5. 將表 t1 中按照 c2 列排序的前兩行數(shù)據(jù)的 c2 列值修改為 100。
  6. OceanBase(admin@test)>update t1 set t1.c2 = 100 order by c2 limit 2;
    Query OK, 2 rows affected (0.02 sec)
    Rows matched: 2  Changed: 2  Warnings: 0
    
    OceanBase(admin@test)>select * from t1;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |  100 |
    |  2 |  100 |
    |  3 |    3 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.01 sec)
    
  7. 將表 t2 中 p2 分區(qū)的數(shù)據(jù)中 "t2.c1 > 2" 的對(duì)應(yīng)行數(shù)據(jù)的 c2 列值修改為 100。
  8. OceanBase(admin@test)>update t2 partition(p2) set t2.c2 = 100 where t2.c1 > 2;
    Query OK, 1 row affected (0.02 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    OceanBase(admin@test)>select * from t2;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  5 |    5 |
    |  1 |    1 |
    |  2 |    2 |
    |  3 |  100 |
    +----+------+
    4 rows in set (0.06 sec)
  9. 修改多個(gè)表。將 t1 表和 t2 表中滿足 "t1.c1 = t2.c1" 對(duì)應(yīng)行的數(shù)據(jù) t1 表中的 c2 列值修改為 100,t2 表中的 c2 列值修改為 200。
  10. OceanBase(admin@test)>update t1,t2 set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2;
    Query OK, 6 rows affected (0.03 sec)
    Rows matched: 6  Changed: 6  Warnings: 0
    
    OceanBase(admin@test)>select * from t1;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |  100 |
    |  2 |  100 |
    |  3 |  100 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.00 sec)
    
    OceanBase(admin@test)>select * from t2;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  5 |    5 |
    |  1 |  200 |
    |  2 |  200 |
    |  3 |  200 |
    +----+------+
    4 rows in set (0.01 sec)
    
  11. 修改多個(gè)表。修改 t1 表和 t2 表的p2分區(qū)中滿足 "t1.c1 = t2.c1" 對(duì)應(yīng)行的數(shù)據(jù) t1 表中的 c2 列值修改為 100,t2 表中的 c2 列值修改為 200。
  12. OceanBase(admin@test)>update t1,t2 partition(p2) set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2;
    Query OK, 6 rows affected (0.02 sec)
    Rows matched: 6  Changed: 6  Warnings: 0
    
    OceanBase(admin@test)>select * from t1;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |  100 |
    |  2 |  100 |
    |  3 |  100 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.01 sec)
    
    OceanBase(admin@test)>select * from t2;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  5 |    5 |
    |  1 |  200 |
    |  2 |  200 |
    |  3 |  200 |
    +----+------+
    4 rows in set (0.01 sec)
    
  13. 對(duì)可更新視圖 v 進(jìn)行更新值。
  14. OceanBase(admin@test)>create view v as select * from t1;
    Query OK, 0 rows affected (0.07 sec)
    
    OceanBase(admin@test)>update v set v.c2 = 100 where v.c1 = 1;
    Query OK, 1 row affected (0.02 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    OceanBase(admin@test)>select * from v;
    +----+------+
    | c1 | c2   |
    +----+------+
    |  1 |  100 |
    |  2 |    2 |
    |  3 |    3 |
    |  4 |    4 |
    +----+------+
    4 rows in set (0.01 sec)
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)