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:有 ...
随机推荐
- 【数学建模】day07-数理统计II
方差分析和回归分析. 用数理统计分析试验结果.鉴别各因素对结果影响程度的方法称为方差分析(Analysis Of Variance),记作 ANOVA. 比如:从用不同工艺制作成的灯泡中,各自抽取了若 ...
- P1387 最大正方形 dp
思路: i j的最大正方形等于min(他的斜上方的的最大正方形,他的上方有的连续1,他的左方有的连续1)+1 #include<bits/stdc++.h> using namespac ...
- Ionic3在ts中获取html中值的方法
我觉得有两种方法,都是Angular中的语法,一种是把值当做参数传递,另一种是使用ngModel实现双向绑定 还有一种很少用到的,Js的原生方法:document.getElementById('ch ...
- IDEA 简单的正则匹配
IDEA在进行查看或替换的时候,勾选Regex 选项就可以进行正则匹配查找了 几个简单实用的正则: 以什么开头,以什么结尾的字符串 以aa开头,以bb结尾的字符串aa.*bb 从开头到某个字符串为止的 ...
- HttpWebRequest发http参数
使用js发请求时,一般使用表单.json对象或者字符串 $.post(url,jsonStr) 服务端获取参数 Request.QueryString.Get();// GET参数 Request.F ...
- 20165223 Linux安装及命令入门
预备作业3:Linux安装及命令入门 一.VirtualBox和Ubuntu的安装 通过学习实践基于VirtualBox虚拟机安装Ubuntu图文教程,我开始学习虚拟机的安装,根据教程一步步试着安装. ...
- Oracle数据库中遇到的坑
最近在帮别人忙写程序,用的是Oracle数据库,写一篇文章来说说在Oracle中遇到的一些坑: 1. PL/SQL develop的坑: 由于在这里工作环境是内网完全,无奈只能使用PL/SQL 工具, ...
- UVALive - 4222
题目链接:https://vjudge.net/contest/244167#problem/D 题目: For a dance to be proper in the Altered Culture ...
- Apache Beam实战指南 | 手把手教你玩转KafkaIO与Flink
https://mp.weixin.qq.com/s?__biz=MzU1NDA4NjU2MA==&mid=2247492538&idx=2&sn=9a2bd9fe2d7fd6 ...
- Go 语言 HTTP Server 源码分析
http://www.codeceo.com/go-http-server-code.html