SQL语法基础之UPDATE语句
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> UPDATE student SET stu_name='齐天大圣美猴王' WHERE stu_id = 1;
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>
mysql> UPDATE student,student_backup SET student.stu_name = student_backup.stu_name WHERE student.stu_id = student_backup.stu_id;
二.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>
mysql> UPDATE student SET stu_name = '火影忍者' LIMIT 2;
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 '' 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>
mysql> UPDATE IGNORE student SET stu_id =4 WHERE stu_id=1;
三.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>
mysql> UPDATE student SET stu_id = stu_id + 5;
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>
mysql> UPDATE teacher SET t_id = t_id + 5,teacher_id = t_id;
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 '' 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>
mysql> UPDATE teacher SET t_id = t_id + 1 ORDER BY t_id DESC;
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>
mysql> UPDATE student,student_backup SET student.stu_name = student_backup.stu_name WHERE student.stu_id = student_backup.stu_id;
SQL语法基础之UPDATE语句的更多相关文章
- SQL语法基础之INSEART语句
SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...
- SQL语法基础之CREATE语句
SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...
- SQL语法基础之DROP语句
SQL语法基础之DROP语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看DROP帮助信息 mysql> ? DROP Many help items for yo ...
- SQL语法基础之ALTER语句
SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...
- SQL语法基础之DELETE语句
SQL语法基础之DELETE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看DELETE的帮助信息 mysql> ? DELETE Na ...
- SQL语法基础之高级应用
SQL语法基础之高级应用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程与函数 1>.CREATE PROCEDURE 用来创建存储过程 mysql> ? ...
- SQL语法基础之SELECT
SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...
- SQL——语法基础篇(上)
用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...
- oracle PL/SQL语法基础
目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...
随机推荐
- IntelliJ IDEA 导航的 20 大特性
本文由 ImportNew - elviskang 翻译自 dzone.欢迎加入翻译小组.转载请见文末要求. 在前面的文章里,我介绍了IntelliJ IDEA(以下称IntelliJ)中与代码补全及 ...
- ceph 重启,停止,开始
systemctl restart ceph-mon@mon-node3 systemctl stop ceph-mon@mon-node3 systemctl start ceph-mon@mon- ...
- IDEA 根据 Mysql 自动生成
1 找到 没有的,file--project structure--modules--+--JPA 2 找到如下 添加Mysql连接,记得 Text Connecting一下看看 然后刷新,就可以出 ...
- 树形DP和状压DP和背包DP
树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...
- Matplotlib学习---用matplotlib画雷达图(radar chart)
雷达图常用于对多项指标的全面分析.例如:HR想要比较两个应聘者的综合素质,用雷达图分别画出来,就可以进行直观的比较. 用Matplotlib画雷达图需要使用极坐标体系,可点击此链接,查看对极坐标体系的 ...
- swagger2 如何匹配多个controller
方法一:使用多个controller的共同拥有的父类,即精确到两个controller的上一级 @Bean public Docket createRestApi() { return new Doc ...
- 【agc013d】Piling Up(动态规划)
[agc013d]Piling Up(动态规划) 题面 atcoder 洛谷 有\(n\)个球,颜色为黑白中的一种,初始时颜色任意. 进行\(m\)次操作,每次操作都是先拿出一个求,再放进黑白各一个, ...
- 【BZOJ3451】Normal (点分治)
[BZOJ3451]Normal (点分治) 题面 BZOJ 题解 显然考虑每个点的贡献.但是发现似乎怎么算都不好计算其在点分树上的深度. 那么考虑一下这个点在点分树中每一次被计算的情况,显然就是其在 ...
- 【BZOJ2823】[AHOI2012]信号塔(最小圆覆盖)
[BZOJ2823][AHOI2012]信号塔(最小圆覆盖) 题面 BZOJ 洛谷 相同的题: BZOJ1 BZOJ2 洛谷 题解 模板题... #include<iostream> #i ...
- Nowcoder | [题解-N165]牛客网NOIP赛前集训营-普及组(第二场)
啊...表示一大早还没睡醒就开始打比赛(开始前一分钟的我还在桌子上趴着休眠)...表示题目思路清奇(尤其C题)...但是我还是太蒻了...\(D\)题暴力都没打...题解正式开始之前先\(\%\)一下 ...