首批通过分布式安全可靠测评,为关键业务系统打造
UPDATE
更新时间:2026-06-04 15:12:35
描述
该语句用于更新表中的数据。
使用限制及注意事项
不管是多表更新还是单表更新都不支持直接对子查询进行更新值操作,否则会报错。示例如下:
UPDATE (SELECT * FROM T1) SET C1 = 100;
返回结果如下:
ERROR 1288 (HY000): The target table of the UPDATE is not updatable
权限要求
执行 UPDATE 语句,需要当前用户拥有 UPDATE 权限。有关 OceanBase 数据库权限的详细介绍,请参见 MySQL 模式下的权限分类。
语法
UPDATE [hint_options] [IGNORE] table_references
SET update_asgn_list
[WHERE where_condition_list]
[ORDER BY order_expression_list]
[LIMIT row_count];
table_references:
table_name [PARTITION (partition_name_list)] [, table_name [PARTITION (partition_name_list)] ...]
partition_name_list:
partition_name [, partition_name ...]
update_asgn_list:
column_name = expr [, column_name = expr ...]
where_condition_list:
where_condition [, where_condition ...]
where_condition:
expression
order_expression_list:
order_expression [, order_expression ...]
order_expression:
expression [ASC | DESC]
参数解释
| 参数 | 描述 |
|---|---|
| hint_options | 可选项,用于指定 hint 选项。有关 Hint 的介绍信息,参见 Optimizer Hint。 |
| IGNORE | 可选项,如果更新操作中出现了重复键的情况,会忽略该行的更新操作。 |
| table_references | 指定要更新的表名列表,可以是一个或多个表。更新多个表时,表名间以英文逗号(,)分隔。详细介绍可参见下文 table_references。 |
| update_asgn_list | 指定要更新的列以及相应的新值。 |
| column_name | 列名。 |
| WHERE where_condition_list | 可选项,指定过滤条件列表,即指定哪些行会被更新。更新特定条件数据的详细介绍可参见下文 where_condition。 |
| ORDER BY order_expression_list | 可选项,指定要更新行的排序键列表。通常与 LIMIT 子句一起使用。更新顺序的详细介绍可参见下文 order_expression。 |
| LIMIT row_count | 可选项,指定要更新的行数。 |
table_references
table_name:指定需要更新数据的表名。并且也可以通过PARTITION关键字指定分区更新数据。partition_name_list:指定需要更新数据的分区名称的列表,可以是一个或多个分区名称。更新多个分区时,分区名间以英文逗号(,)分隔。partition_name:表示需要更新数据的分区名。
示例如下:
创建表
tbl1。CREATE TABLE tbl1(col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT) PARTITION BY HASH(col1) PARTITIONS 5;向表
tbl1中插入测试数据。INSERT INTO tbl1 VALUES (1, 'A1', 1),(2, 'A2', 2),(3, 'A3', 3), (4, 'A4', 4),(5, 'A5', 5),(6, 'A6', 6), (7, 'A7', 7),(8, 'A8', 8),(9, 'A9', 9);查看表
tbl1中p0和p1分区的数据。SELECT * FROM tbl1 PARTITION(p0, p1);返回结果如下:
+------+------+------+ | col1 | col2 | col3 | +------+------+------+ | 5 | A5 | 5 | | 1 | A1 | 1 | | 6 | A6 | 6 | +------+------+------+ 3 rows in set更新表
tbl1中p0和p1分区的数据,将col2列的值与下划线和字符串update拼接在一起,然后更新col2列的值为拼接后的结果。UPDATE tbl1 PARTITION(p0, p1) SET col2 = CONCAT(col2, '_', 'update');返回结果如下:
Query OK, 3 rows affected Rows matched: 3 Changed: 3 Warnings: 0再次查看表
tbl1中p0和p1分区的数据。SELECT * FROM tbl1 PARTITION(p0, p1);返回结果如下:
+------+-----------+------+ | col1 | col2 | col3 | +------+-----------+------+ | 5 | A5_update | 5 | | 1 | A1_update | 1 | | 6 | A6_update | 6 | +------+-----------+------+ 3 rows in set
where_condition
expression:指定可用于过滤要更新的行的条件表达式。
示例如下:
查看表
tbl1中col1 = 2的数据。SELECT * FROM tbl1 WHERE col1 = 2;返回结果如下:
+------+------+------+ | col1 | col2 | col3 | +------+------+------+ | 2 | A2 | 2 | +------+------+------+ 1 row in set更新表
tbl1中col1 = 2的行,将col2的值设为update A2,将col3的值设为 22。UPDATE tbl1 SET col2 = 'update A2', col3 = 22 WHERE col1 = 2;返回结果如下:
Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0再次查看表
tbl1中col1 = 2的数据。SELECT * FROM tbl1 WHERE col1 = 2;返回结果如下:
+------+-----------+------+ | col1 | col2 | col3 | +------+-----------+------+ | 2 | update A2 | 22 | +------+-----------+------+ 1 row in set
order_expression
expression [ASC | DESC]:指定按升序(ASC,默认值)或降序(DESC)排序的表达式。
示例如下:
查看表
tbl1中列col1的值大于 5,并按照col3列的值按降序(从大到小)对结果进行排序。SELECT * FROM tbl1 WHERE col1 > 5 ORDER BY col3 DESC;返回结果如下:
+------+-----------+------+ | col1 | col2 | col3 | +------+-----------+------+ | 9 | A9 | 9 | | 8 | A8 | 8 | | 7 | A7 | 7 | | 6 | A6_update | 6 | +------+-----------+------+ 4 rows in set对
tbl1表进行更新操作,将满足条件col1大于 5 的行中的col3列的值乘以 10,并按照col3降序排序,取出前 2 行进行更新。UPDATE tbl1 SET col3 = col3*10 WHERE col1 > 5 ORDER BY col3 DESC LIMIT 2;返回结果如下:
Query OK, 2 rows affected Rows matched: 2 Changed: 2 Warnings: 0再次查看表
tbl1中的数据。SELECT * FROM tbl1 WHERE col1 > 5 ORDER BY col3 DESC;返回结果如下:
+------+-----------+------+ | col1 | col2 | col3 | +------+-----------+------+ | 9 | A9 | 90 | | 8 | A8 | 80 | | 7 | A7 | 7 | | 6 | A6_update | 6 | +------+-----------+------+ 4 rows in set
示例
创建示例表
test_tbl1。CREATE TABLE test_tbl1(col1 INT PRIMARY KEY, col2 INT);向表
test_tbl1中插入测试数据。INSERT INTO test_tbl1 VALUES (1, 1),(2, 2),(3, 3), (4, 4),(5, 5);
修改多个表。
将
tbl1表和test_tbl1表中满足tbl1.col1 = test_tbl1.col1对应行的数据tbl1表中的col3列值修改为100,test_tbl1表中的col2列值修改为200。UPDATE test_tbl1, tbl1 SET tbl1.col3 = 100, test_tbl1.col2 = 200 WHERE tbl1.col1 = test_tbl1.col1;返回结果如下:
Query OK, 10 rows affected Rows matched: 10 Changed: 10 Warnings: 0查看表
test_tbl1的数据。SELECT * FROM test_tbl1;返回结果如下:
+------+------+ | col1 | col2 | +------+------+ | 1 | 200 | | 2 | 200 | | 3 | 200 | | 4 | 200 | | 5 | 200 | +------+------+ 5 rows in set
使用
IGNORE关键字更新测试表test_tbl1中的数据,更新中出现了重复键的情况,就会忽略该行的更新操作。对
test_tbl1表进行更新操作,将满足条件col1大于 3 的行中col1值将会加 1。UPDATE IGNORE test_tbl1 SET col1 = col1 + 1 WHERE col1 > 3;返回结果如下:
Query OK, 1 row affected Rows matched: 2 Changed: 1 Warnings: 0查看表
test_tbl1的数据。SELECT * FROM test_tbl1;返回结果如下:
+------+------+ | col1 | col2 | +------+------+ | 1 | 200 | | 2 | 200 | | 3 | 200 | | 4 | 200 | | 6 | 200 | +------+------+ 5 rows in set
对可更新视图进行更新值。
创建视图
v1。CREATE VIEW v1 AS SELECT * FROM test_tbl1;更新
v1中符合条件v1.col1 = 1的行,将v1.col2的值设置为 100。UPDATE v1 SET v1.col2 = 100 WHERE v1.col1 = 1;返回结果如下:
Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0查看表
v1的数据。SELECT * FROM v1;返回结果如下:
+------+------+ | col1 | col2 | +------+------+ | 1 | 100 | | 2 | 200 | | 3 | 200 | | 4 | 200 | | 6 | 200 | +------+------+ 5 rows in set
更新分区键的值。
创建表
test_tbl2。obclient> CREATE TABLE test_tbl2(col1 INT, col2 INT, PRIMARY KEY(col1, col2)) PARTITION BY HASH(col2) PARTITIONS 4;向表
test_tbl2中插入测试数据。obclient> INSERT INTO test_tbl2 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);更新表
test_tbl2中col1 = 4的行中col2的值为 55。obclient> UPDATE test_tbl2 SET col2 = 55 WHERE col1 = 4;