SQL语法基础之UPDATE语句
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.查看UPDATE语句的帮助信息
1>.查看UPDATE的帮助信息
mysql> ? UPDATE Name: 'UPDATE' Description: Syntax: UPDATE is a DML statement that modifies rows in a table. An UPDATE statement can start with a WITH clause to define common table expressions accessible within the UPDATE. See http://dev.mysql.com/doc/refman/8.0/en/with.html. Single-table syntax: #单表修改语句结构 UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET assignment_list [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] value: {expr | DEFAULT} assignment: col_name = value assignment_list: assignment [, assignment] ... Multiple-table syntax: #多表修改语句结构 UPDATE [LOW_PRIORITY] [IGNORE] table_references SET assignment_list [WHERE where_condition] For the single-table syntax, the UPDATE statement updates columns of existing rows in the named table with new values. The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value. The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated. If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated. For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. Each matching row is updated once, even if it matches the conditions multiple times. For multiple-table syntax, ORDER BY and LIMIT cannot be used. For partitioned tables, both the single-single and multiple-table forms of this statement support the use of a PARTITION option as part of a table reference. This option takes a list of one or more partitions or subpartitions (or both). Only the partitions (or subpartitions) listed are checked for matches, and a row that is not in any of these partitions or subpartitions is not updated, whether it satisfies the where_condition or not. *Note*: Unlike the case when using PARTITION with an INSERT or REPLACE statement, an otherwise valid UPDATE ... PARTITION statement is considered successful even if no rows in the listed partitions (or subpartitions) match the where_condition. For more information and examples, see http://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html. where_condition is an expression that evaluates to true for each row to be updated. For expression syntax, see http://dev.mysql.com/doc/refman/8.0/en/expressions.html. table_references and where_condition are specified as described in http://dev.mysql.com/doc/refman/8.0/en/select.html. You need the UPDATE privilege only for columns referenced in an UPDATE that are actually updated. You need only the SELECT privilege for any columns that are read but not modified. The UPDATE statement supports the following modifiers: o With the LOW_PRIORITY modifier, execution of the UPDATE is delayed until no other clients are reading from the table. This affects only storage engines that use only table-level locking (such as MyISAM, MEMORY, and MERGE). o With the IGNORE modifier, the update statement does not abort even if errors occur during the update. Rows for which duplicate-key conflicts occur on a unique key value are not updated. Rows updated to values that would cause data conversion errors are updated to the closest valid values instead. For more information, see http://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#ignore-strict-co mparison. URL: http://dev.mysql.com/doc/refman/8.0/en/update.html mysql> mysql>
2>.UPDATE语句的常规用法
mysql> SELECT * FROM student; +--------+-----------+ | stu_id | stu_name | +--------+-----------+ | 1 | 孙悟空 | | 2 | 猪八戒 | | 4 | 沙和尚 | +--------+-----------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE student SET stu_name='齐天大圣美猴王' WHERE stu_id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> mysql> SELECT * FROM student; +--------+-----------------------+ | stu_id | stu_name | +--------+-----------------------+ | 1 | 齐天大圣美猴王 | | 2 | 猪八戒 | | 4 | 沙和尚 | +--------+-----------------------+ 3 rows in set (0.00 sec) mysql> mysql>
mysql> CREATE TABLE student_backup LIKE student; Query OK, 0 rows affected (0.01 sec) mysql> mysql> INSERT INTO student_backup VALUES(1,'佩恩六道'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO student_backup VALUES(2,'六道仙人'); Query OK, 1 row affected (0.00 sec) mysql> mysql> INSERT INTO student_backup VALUES(3,'漩涡鸣人'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM student_backup; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 3 | 漩涡鸣人 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql> SELECT * FROM student; +--------+-----------------------+ | stu_id | stu_name | +--------+-----------------------+ | 1 | 齐天大圣美猴王 | | 2 | 猪八戒 | | 4 | 沙和尚 | +--------+-----------------------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE student,student_backup SET student.stu_name = student_backup.stu_name WHERE student.stu_id = student_backup.stu_id; Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql> SELECT * FROM student_backup; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 3 | 漩涡鸣人 | +--------+--------------+ 3 rows in set (0.00 sec) mysql>
二.UPDATE关键点剖析
1>.单表修改是指单个表中单已经存在数据单一个或多个字段的值;SET短语后面要更修改单列和值。
2>.WHERE子句表示限定要修改表中的那行数据,如果没有WHERE子句表示所有行都要修改;ORDER BY子句表示UPDATE数据按照指定的顺序进行;limit子句表示限定修改数据的行数;
3>.多表修改是指修改table_references指定的多个表中满足条件的行数据,多表修改不允许使用ORDER BY和LIMIT子句;
mysql> DESC student; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | stu_id | int(11) | NO | PRI | NULL | auto_increment | | stu_name | varchar(30) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE student SET stu_name = '火影忍者' LIMIT 2; Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 火影忍者 | | 2 | 火影忍者 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql>
4>.执行UPDATE语句需要修改表的权限;
5>.LOW_PRIORITY关键字表示修改语句需要等待其他链接的读此表操作结束后在执行,只作用在MyISAM,MEMORY和MERGE存储引擎;
6>.IGNORE关键字表示修改语句碰到违反唯一性约束条件等情况时,语句不会报错回退而是报警告信息。
mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 火影忍者 | | 2 | 火影忍者 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE student SET stu_id =4 WHERE stu_id=1; ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY' mysql> mysql> mysql> mysql> UPDATE IGNORE student SET stu_id =4 WHERE stu_id=1; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 火影忍者 | | 2 | 火影忍者 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql>
三.UPDATE语句执行
1>.对表中某列值加5
mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 火影忍者 | | 2 | 火影忍者 | | 4 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql> mysql> UPDATE student SET stu_id = stu_id + 5; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 6 | 火影忍者 | | 7 | 火影忍者 | | 9 | 沙和尚 | +--------+--------------+ 3 rows in set (0.00 sec) mysql> mysql>
2>.修改某列的值并将修改后的值和另一列数值同步
mysql> SELECT * FROM teacher; +------+-------------+------------+ | t_id | course_name | teacher_id | +------+-------------+------------+ | 1 | Chinese | 1 | | 2 | English | 4 | | 3 | Physic | 4 | +------+-------------+------------+ 3 rows in set (0.00 sec) mysql> mysql> mysql> UPDATE teacher SET t_id = t_id + 5,teacher_id = t_id; Query OK, 3 rows affected (0.02 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> mysql> SELECT * FROM teacher; +------+-------------+------------+ | t_id | course_name | teacher_id | +------+-------------+------------+ | 6 | Chinese | 6 | | 7 | English | 7 | | 8 | Physic | 8 | +------+-------------+------------+ 3 rows in set (0.00 sec) mysql>
3>.ORDER BY指定UPDATE数据的顺序,在某些情况喜爱可以避免错误的发生
mysql> SELECT * FROM teacher; +------+-------------+------------+ | t_id | course_name | teacher_id | +------+-------------+------------+ | 6 | Chinese | 6 | | 7 | English | 7 | | 8 | Physic | 8 | +------+-------------+------------+ 3 rows in set (0.00 sec) mysql> mysql> DESC teacher; +-------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+----------------+ | t_id | int(11) | NO | PRI | NULL | auto_increment | | course_name | varchar(20) | YES | | NULL | | | teacher_id | int(11) | YES | | NULL | | +-------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE teacher SET t_id = t_id + 1; ERROR 1062 (23000): Duplicate entry '7' for key 'PRIMARY' mysql> mysql> UPDATE teacher SET t_id = t_id + 1 ORDER BY t_id DESC; Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> mysql> SELECT * FROM teacher; +------+-------------+------------+ | t_id | course_name | teacher_id | +------+-------------+------------+ | 7 | Chinese | 6 | | 8 | English | 7 | | 9 | Physic | 8 | +------+-------------+------------+ 3 rows in set (0.00 sec) mysql>
4>.多表修改(表之间通过WHERE条件进行JOIN操作)
mysql> CREATE TABLE student_backup LIKE student; Query OK, 0 rows affected (0.01 sec) mysql> mysql> INSERT INTO student_backup VALUES(1,'佩恩六道'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO student_backup VALUES(2,'六道仙人'); Query OK, 1 row affected (0.00 sec) mysql> mysql> INSERT INTO student_backup VALUES(3,'漩涡鸣人'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM student_backup; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 3 | 漩涡鸣人 | +--------+--------------+ rows in set (0.00 sec) mysql> mysql> SELECT * FROM student; +--------+-----------------------+ | stu_id | stu_name | +--------+-----------------------+ | 1 | 齐天大圣美猴王 | | 2 | 猪八戒 | | 4 | 沙和尚 | +--------+-----------------------+ rows in set (0.00 sec) mysql> mysql> UPDATE student,student_backup SET student.stu_name = student_backup.stu_name WHERE student.stu_id = student_backup.stu_id; Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> mysql> mysql> SELECT * FROM student; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 4 | 沙和尚 | +--------+--------------+ rows in set (0.00 sec) mysql> mysql> SELECT * FROM student_backup; +--------+--------------+ | stu_id | stu_name | +--------+--------------+ | 1 | 佩恩六道 | | 2 | 六道仙人 | | 3 | 漩涡鸣人 | +--------+--------------+ rows in set (0.00 sec) mysql>