描述

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

格式

  1. UPDATE [IGNORE] table_references
  2. SET update_asgn_list
  3. [WHERE where_condition]
  4. [ORDER BY order_list]
  5. [LIMIT row_count];
  6. table_references:
  7. tbl_name [PARTITION (partition_name,...)] [, ...]
  8. update_asgn_list:
  9. column_name = expr [, ...]
  10. order_list:
  11. column_name [ASC|DESC] [, column_name [ASC|DESC]…]

参数解释

参数

描述

IGNORE

在 INSERT 语句执行过程中发生的错误将会被忽略。

table_references

指定修改表名,多表修改时,表名直接‘,’作为间隔。

where_condition

指定过滤条件。

row_count

限制的行数。

tbl_name

插入表名。

partition_name

插入表指定的分区名。

column_name

列名。

column_name ASC

按列名升序修改。

column_name DESC

按列名降序修改。

注意事项

不管是多表还是单表更新都不支持直接对子查询进行更新值操作,例如:update (select * from t1) set c1 = 100;

示例

  1. 创建示例表 t1 和 t2。
  1. OceanBase(admin@test)>create table t1(c1 int primary key, c2 int);
  2. Query OK, 0 rows affected (0.16 sec)
  3. OceanBase(admin@test)>select * from t1;
  4. +----+------+
  5. | c1 | c2 |
  6. +----+------+
  7. | 1 | 1 |
  8. | 2 | 2 |
  9. | 3 | 3 |
  10. | 4 | 4 |
  11. +----+------+
  12. 4 rows in set (0.06 sec)
  13. OceanBase(admin@test)>create table t2(c1 int primary key, c2 int) partition by key(c1) partitions 4;
  14. Query OK, 0 rows affected (0.19 sec)
  15. OceanBase(admin@test)>select * from t2;
  16. +----+------+
  17. | c1 | c2 |
  18. +----+------+
  19. | 5 | 5 |
  20. | 1 | 1 |
  21. | 2 | 2 |
  22. | 3 | 3 |
  23. +----+------+
  24. 4 rows in set (0.02 sec)
  1. 将表 t1 中 “t1.c1=1” 对应的那一行数据的 c2 列值修改为 100。
  1. OceanBase(admin@test)>update t1 set t1.c2 = 100 where t1.c1 = 1;
  2. Query OK, 1 row affected (0.02 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. OceanBase(admin@test)>select * from t1;
  5. +----+------+
  6. | c1 | c2 |
  7. +----+------+
  8. | 1 | 100 |
  9. | 2 | 2 |
  10. | 3 | 3 |
  11. | 4 | 4 |
  12. +----+------+
  13. 4 rows in set (0.01 sec)
  1. 将表 t1 中按照 c2 列排序的前两行数据的 c2 列值修改为 100。
  1. OceanBase(admin@test)>update t1 set t1.c2 = 100 order by c2 limit 2;
  2. Query OK, 2 rows affected (0.02 sec)
  3. Rows matched: 2 Changed: 2 Warnings: 0
  4. OceanBase(admin@test)>select * from t1;
  5. +----+------+
  6. | c1 | c2 |
  7. +----+------+
  8. | 1 | 100 |
  9. | 2 | 100 |
  10. | 3 | 3 |
  11. | 4 | 4 |
  12. +----+------+
  13. 4 rows in set (0.01 sec)
  1. 将表 t2 中 p2 分区的数据中 “t2.c1 > 2” 的对应行数据的 c2 列值修改为 100。
  1. OceanBase(admin@test)>update t2 partition(p2) set t2.c2 = 100 where t2.c1 > 2;
  2. Query OK, 1 row affected (0.02 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. OceanBase(admin@test)>select * from t2;
  5. +----+------+
  6. | c1 | c2 |
  7. +----+------+
  8. | 5 | 5 |
  9. | 1 | 1 |
  10. | 2 | 2 |
  11. | 3 | 100 |
  12. +----+------+
  13. 4 rows in set (0.06 sec)
  1. 修改多个表。将 t1 表和 t2 表中满足 “t1.c1 = t2.c1” 对应行的数据 t1 表中的 c2 列值修改为 100,t2 表中的 c2 列值修改为 200。
  1. OceanBase(admin@test)>update t1,t2 set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2;
  2. Query OK, 6 rows affected (0.03 sec)
  3. Rows matched: 6 Changed: 6 Warnings: 0
  4. OceanBase(admin@test)>select * from t1;
  5. +----+------+
  6. | c1 | c2 |
  7. +----+------+
  8. | 1 | 100 |
  9. | 2 | 100 |
  10. | 3 | 100 |
  11. | 4 | 4 |
  12. +----+------+
  13. 4 rows in set (0.00 sec)
  14. OceanBase(admin@test)>select * from t2;
  15. +----+------+
  16. | c1 | c2 |
  17. +----+------+
  18. | 5 | 5 |
  19. | 1 | 200 |
  20. | 2 | 200 |
  21. | 3 | 200 |
  22. +----+------+
  23. 4 rows in set (0.01 sec)
  1. 修改多个表。修改 t1 表和 t2 表的p2分区中满足 “t1.c1 = t2.c1” 对应行的数据 t1 表中的 c2 列值修改为 100,t2 表中的 c2 列值修改为 200。
  1. OceanBase(admin@test)>update t1,t2 partition(p2) set t1.c2 = 100, t2.c2 = 200 where t1.c2 = t2.c2;
  2. Query OK, 6 rows affected (0.02 sec)
  3. Rows matched: 6 Changed: 6 Warnings: 0
  4. OceanBase(admin@test)>select * from t1;
  5. +----+------+
  6. | c1 | c2 |
  7. +----+------+
  8. | 1 | 100 |
  9. | 2 | 100 |
  10. | 3 | 100 |
  11. | 4 | 4 |
  12. +----+------+
  13. 4 rows in set (0.01 sec)
  14. OceanBase(admin@test)>select * from t2;
  15. +----+------+
  16. | c1 | c2 |
  17. +----+------+
  18. | 5 | 5 |
  19. | 1 | 200 |
  20. | 2 | 200 |
  21. | 3 | 200 |
  22. +----+------+
  23. 4 rows in set (0.01 sec)
  1. 对可更新视图 v 进行更新值。
  1. OceanBase(admin@test)>create view v as select * from t1;
  2. Query OK, 0 rows affected (0.07 sec)
  3. OceanBase(admin@test)>update v set v.c2 = 100 where v.c1 = 1;
  4. Query OK, 1 row affected (0.02 sec)
  5. Rows matched: 1 Changed: 1 Warnings: 0
  6. OceanBase(admin@test)>select * from v;
  7. +----+------+
  8. | c1 | c2 |
  9. +----+------+
  10. | 1 | 100 |
  11. | 2 | 2 |
  12. | 3 | 3 |
  13. | 4 | 4 |
  14. +----+------+
  15. 4 rows in set (0.01 sec)