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:有 ...
随机推荐
- 【数学建模】day02-整数规划
基本类似于中学讲的整数规划--线性规划中变量约束为整数的情形. 目前通用的解法适合整数线性规划.不管是完全整数规划(变量全部约束为整数),还是混合整数规划(变量既有整数又有实数),MATLAB都提供了 ...
- HTTP协议【详解】——经典面试题
http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- Python里的单下划线,双下划线,以及前后都带下划线的意义
Python里的单下划线,双下划线,以及前后都带下划线的意义: 单下划线如:_name 意思是:不能通过from modules import * 导入,如需导入需要:from modules imp ...
- 【XSY1545】直径 虚树 DP
题目大意 给你一棵\(n\)个点的树,另外还有\(m\)棵树,第\(i\)棵树与原树的以\(r_i\)为根的子树形态相同.这\(m\)棵树之间也有连边,组成一颗大树.求这棵大树的直径长度. \(n ...
- pycharm opencv4.0安装使用
pycharm+opencv4.0 还记得去年冬天装了两回opencv3,每次都搞得死去活来的.. 今天也是查了一上午,什么anaconda,vs,但是我是在pycharm的虚拟环境中安装,突然看到一 ...
- Codeforces | CF1010C 【Border】
这道题大致题意是给定\(n\)个十进制整数和一个进制数\(k\),可以用无数多个给定的十进制整数,问这些十进制整数的和在模k意义下有多少种不同的结果(\(k\)进制下整数的最后一位就是这个数模\(k\ ...
- 原生JS节点操作
获取子节点 1. children 不是标准的dom属性,但是几乎被所有浏览器支持.获取子元素的元素节点(只包括元素节点) 注意:在IE中,children包含注释节点. 2. childNodes ...
- 工作队列.py
#对列模式图Work Queue背后的主要思想是避免立即执行资源密集型任务的时,需要等待其他任务完成.所以我们把任务安排的晚一些,我们封装一个任务到消息中并把它发送到队列,一个进程运行在后端发送并最终 ...
- python中,print函数的sep和end参数
print函数是我们经常使用的,但是它的sep和end参数或许对很多python使用者相对陌生,他们可以让我们的打印更具有个性化. 先来看下官方解释, sep:分割值与值,默认是一个空格 end:附件 ...