首先,单表的UPDATE语句:
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
[ORDER BY ...]
[LIMIT row_count]
其次,多表的UPDATE语句:
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
UPDATE语法可以用新值更新原有表行中的各列。
SET子句指示要修改哪些列和要给予哪些值。WHERE子句指定应更新哪些行。
如果没有WHERE子句,则更新所有的行。如果指定了ORDER BY子句,则按照被指定的顺序对行进行更新。
LIMIT子句用于给定一个限值,限制可以被更新的行的数目。
UPDATE语句支持以下修饰符:
1,如果您使用LOW_PRIORITY关键词,则UPDATE的执行被延迟了,直到没有其它的客户端从表中读取为止。
2,如果您使用IGNORE关键词,则即使在更新过程中出现错误,更新语句也不会中断。
如果出现了重复关键字冲突,则这些行不会被更新。如果列被更新后,新值会导致数据转化错误,则这些行被更新为最接近的合法的值。
如果您在一个表达式中通过tbl_name访问一列,则UPDATE使用列中的当前值。
例如,把年龄列设置为比当前值多一:
代码如下:
mysql> UPDATE persondata SET age=age+1;
UPDATE赋值被从左到右评估。
例如,对年龄列加倍,然后再进行增加:
代码如下:
mysql> UPDATE persondata SET age=age*2, age=age+1;
如果您把一列设置为其当前含有的值,则MySQL会注意到这一点,但不会更新。
如果您把被已定义为NOT NULL的列更新为NULL,则该列被设置到与列类型对应的默认值,并且累加警告数。
对于数字类型,默认值为0;对于字符串类型,默认值为空字符串('');对于日期和时间类型,默认值为“zero”值。
UPDATE会返回实际被改变的行的数目。Mysql_info() C API函数可以返回被匹配和被更新的行的数目,以及在UPDATE过程中产生的警告的数量。
您可以使用LIMIT row_count来限定UPDATE的范围。LIMIT子句是一个与行匹配的限定。
只要发现可以满足WHERE子句的row_count行,则该语句中止,不论这些行是否被改变。
如果一个UPDATE语句包括一个ORDER BY子句,则按照由子句指定的顺序更新行。
您也可以执行包括多个表的UPDATE操作。table_references子句列出了在联合中包含的表。
例子:
代码如下:
SQL>UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;
说明:以上代码显示出了使用逗号操作符的内部联合,但是multiple-table UPDATE语句可以使用在SELECT语句中允许的任何类型的联合,比如LEFT JOIN。
注释:不能把ORDER BY或LIMIT与multiple-table UPDATE同时使用。
在一个被更改的multiple-table UPDATE中,有些列被引用。您只需要这些列的UPDATE权限。有些列被读取了,但是没被修改。您只需要这些列的SELECT权限。
如果您使用的multiple-table UPDATE语句中包含带有外键限制的InnoDB表,则MySQL优化符处理表的顺序可能与上下层级关系的顺序不同。
在此情况下,语句无效并被 回滚。同时,更新一个单一表,并且依靠ON UPDATE功能。
该功能由InnoDB提供,用于对其它表进行相应的修改。
目前,不能在一个子查询中更新一个表,同时从同一个表中选择。
update语句的几种基本用法
A. 使用简单的 UPDATE
下列示例说明如果从 UPDATE 语句中去除 WHERE 子句,所有的行会受到什么影响。
下面这个例子说明,如果表 publishers 中的所有出版社将总部搬迁到佐治亚州的亚特兰大市,表 publishers 如何更新。
代码如下:
UPDATE publishers
SET city = 'Atlanta', state = 'GA'
本示例将所有出版商的名字变为 NULL。
代码如下:
UPDATE publishers
SET pub_name = NULL
也可以在更新中使用计算值。本示例将表 titles 中的所有价格加倍。
代码如下:
UPDATE titles
SET price = price * 2
B.把 WHERE 子句和 UPDATE 语句一起使用
WHERE 子句指定要更新的行例如,在下面这个虚构的事件中,北加利福尼亚更名为 Pacifica(缩写为 PC),而奥克兰的市民投票决定将其城市的名字改为 Bay City。这个例子说明如何为奥克兰市以前的所有居民(他们的地址已经过时)更新表 authors。
代码如下:
UPDATE authors
SET state = 'PC', city = 'Bay City'
WHERE state = 'CA' AND city = 'Oakland'
必须编写另一个语句来更改北加利福尼亚其它城市的居民所在的州名。
C.通过 UPDATE 语句使用来自另一个表的信息
本示例修改表 titles 中的 ytd_sales 列,以反映表 sales 中的最新销售记录。
代码如下:
UPDATE titles
SET ytd_sales = titles.ytd_sales + sales.qty
FROM titles, sales
WHERE titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
这个例子假定,一种特定的商品在特定的日期只记录一批销售量,而且更新是最新的。如果不是这样(即如果一种特定的商品在同一天可以记录不止一批销售量),这里所示的例子将出错。例子可正确执行,但是每种商品只用一批销售量进行更新,而不管那一天实际销售了多少批。这是因为一个 UPDATE 语句从不会对同一行更新两次。
对于特定的商品在同一天可销售不止一批的情况,每种商品的所有销售量必须在 UPDATE 语句中合计在一起,如下例所示:
代码如下:
UPDATE titles
SET ytd_sales =
(SELECT SUM(qty)
FROM sales
WHERE sales.title_id = titles.title_id
AND sales.ord_date IN (SELECT MAX(ord_date) FROM sales))
FROM titles, sales
D. 将 UPDATE 语句与 SELECT 语句中的 TOP 子句一起使用
这个例子对来自表 authors 的前十个作者的 state 列进行更新。
代码如下:
UPDATE authors
SET state = 'ZZ'
FROM (SELECT TOP 10 * FROM authors ORDER BY au_lname) AS t1
WHERE authors.au_id = t1.au_id
以上就是mysql update语句用法的全部内容,希望对大家有所帮助。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment