UPDATE
UPDATE
语句用于修改指定表中的数据。
概述
- UpdateStmt
- UpdateOption
- TableRef
- TableRefs
UpdateStmt ::=
"UPDATE" UpdateOption
( TableRef "SET" Assignment ("," Assignment)* WhereClause? OrderBy? Limit?
| TableRefs "SET" Assignment ("," Assignment)* WhereClause?
)
UpdateOption ::=
OptimizerHints? ("LOW_PRIORITY" | "HIGH_PRIORITY" | "DELAYED")? "IGNORE"?
TableRef ::=
( TableFactor | JoinTable )
TableRefs ::=
EscapedTableRef ("," EscapedTableRef)*
示例
mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO t1 (c1) VALUES (1), (2), (3);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1;
+----+----+
| id | c1 |
+----+----+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+----+
3 rows in set (0.00 sec)
mysql> UPDATE t1 SET c1=5 WHERE c1=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM t1;
+----+----+
| id | c1 |
+----+----+
| 1 | 1 |
| 2 | 2 |
| 3 | 5 |
+----+----+
3 rows in set (0.00 sec)
MySQL 兼容性
TiDB 在评估表达式时总是使用列的原始值。例如:
CREATE TABLE t (a int, b int);
INSERT INTO t VALUES (1,2);
UPDATE t SET a = a+1,b=a;
在 MySQL 中,列 b
会被更新为 2,因为它被设置为 a
的值,而在同一条语句中,a
的值(为 1)会被更新为 a+1
(为 2)。
TiDB 遵循更标准的 SQL 行为,会将 b
更新为 1。