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:有 ...
随机推荐
- Go For It ,一个灵活的待办事项列表程序
导读 Go For It,是我们开源工具系列中的第十个工具,它将使你在 2019 年更高效,它在 Todo.txt 系统的基础上构建,以帮助你完成更多工作. 每年年初似乎都有疯狂的冲动想提高工作效率. ...
- docker-安装技巧
使用官方脚本安装 curl -fsSL "https://get.docker.com/" | sh 使用yum 安装是可以查看版本 yum list docker-ce.x86_ ...
- HDU1075 字典树板子题
题意 :给出两组字符串 一一映射,给出一种组成的文字,要求映射成另外一种思路:使用字典树,把映射的另外一个字符存在字典树的单词节点处 例如 abc 123 则把123存在abc节点中的c处即可 ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation 思维构造
题意 给出一个1-n的集合 gcd 集合里面的所有数 得到的 一个 数 然后自己选择删去一个数 要使得到的数 构成的数列 的字典序最大 思路: gcd所有数 那gcd得到的数肯定要小于数 ...
- HDU - 3917(朴素LIS + 最大流)
题意: 求出所有的最长上升子序列的个数且每个元素只能用一次 解析: 呵...呵...呵..呵..emm... 再见 我死了...wa了15发之后...原来不能用~ 要用 != EOF 这题算水题吧. ...
- 网络流24(san)题题解汇总
开坑(烂尾预定 1.餐巾计划问题 题解 2.最小路径覆盖问题 题解 3.试题库问题 题解 4.[CTSC1999]家园 题解 5.骑士共存问题 题解 6.最长不下降子序列问题 题解 7.深海机器人问题 ...
- 【CF1082F】Speed Dial(动态规划)
[CF1082F]Speed Dial(动态规划) 题面 CF 洛谷 题解 把\(Trie\)树建出来之后发现就是一个树型\(dp\),每个点会对于其父亲中第一个被标记的点产生贡献. 那么把第一个点压 ...
- 转----------数据库常见笔试面试题 - Hectorhua的专栏 - CSDN博客
数据库基础(面试常见题) 一.数据库基础 1. 数据抽象:物理抽象.概念抽象.视图级抽象,内模式.模式.外模式 2. SQL语言包括数据定义.数据操纵(Data Manipulation),数据控制( ...
- SP8791 DYNALCA - Dynamic LCA 解题报告
SP8791 DYNALCA - Dynamic LCA 有一个森林最初由 \(n (1 \le n \le 100000)\) 个互不相连的点构成 你需要处理以下操作: link A B:添加从顶点 ...
- 「SCOI2015」小凸想跑步 解题报告
「SCOI2015」小凸想跑步 最开始以为和多边形的重心有关,后来发现多边形的重心没啥好玩的性质 实际上你把面积小于的不等式列出来,发现是一次的,那么就可以半平面交了 Code: #include & ...