OceanBase UPDATE

2021-06-29 10:02 更新

描述

该语句用于修改表中的字段值。

格式

UPDATE [hint_options] dml_table_clause 
       SET update_asgn_list 
       [WHERE where_condition]
       [{ RETURNING | RETURN } returning_exprs [into_clause]]

dml_table_clause:
    dml_table_name opt_table_alias

update_asgn_list:
    column_name = expr [, ...]

where_condition:
    expression
    
returning_exprs:
    projection [, ...]
 
into_clause: 
{ INTO into_var_list | BULK COLLECT INTO into_var_list}

into_var_list:
{ USER_VARIABLE | ref_name } [, ...]

参数解释

参数

描述

hint_options

指定 hint 选项。

dml_table_clause

指定修改的表名(基表、可更新视图、特殊子查询)

where_condition

指定过滤条件。

update_asgn_list

指定更新列表。

returning_exprs

返回修改数据后的投影列。

into_clause

将修改数据后的投影列插入到指定列表

注意 
特殊子查询指的类似于可更新视图对应的子查询,这类子查询不应该包含复杂的算子(比如group by/distinct/window function等等)

示例

创建示例表 t1 和 t2。

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)
  • 单表更新:将表 t1 中 "t1.c1=1" 对应的那一行数据的 c2 列值修改为 100。

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)
  • 单表更新:直接操作子查询,将子查询 中 "v.c1=1" 对应的那一行数据的 c2 列值修改为 100。

OceanBase(admin@test)>update (select * from t1)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 t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |  100 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.01 sec)
  • 单表更新:包含 returning 子句。

OceanBase(admin@test)>update t1 set t1.c2 = 100 where t1.c1 = 1 returning c2;
+------+
| C2   |
+------+
|  100 |
+------+
1 row in set (0.02 sec)

OceanBase(admin@test)>select * from t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |  100 |
|  2 |    2 |
|  3 |    3 |
|  4 |    4 |
+----+------+
4 rows in set (0.01 sec)
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号