为了账号安全,请及时绑定邮箱和手机立即绑定

使用mysql的update语句更新数据

标签:
MySQL

Summary: updating data is one of the most important tasks when you work with databases. In this tutorial, you will learn how to use MySQL UPDATE statement to update data in database tables.

Introduction to MySQL UPDATE statement

The UPDATE statement is used to update existing data in tables. It can be used to change column values of a single row, a group of rows or all rows in a table.

The following illustrates the MySQL UPDATE statement syntax:

UPDATE [LOW_ PRIORITY] [IGNORE] table_name [, table_name...]   SET column_name1 = expr1       [, column_name2=expr2 ...] [WHERE condition]

Let’s examine the UPDATE statement in greater detail:

  • Followed by the UPDATE keyword is the name of the table that you want to update data. In MySQL, you can change the data of multiple tables using a single UPDATE statement. If the UPDATE statement violates any integrity constraint, MySQL does not perform the update and issues an error message.

  • The SET clause determines the column names of the table and the new values. The new values could be literal values, result of expressions or subqueries.

  • The WHERE clause determines which rows will be updated. It is an optional element of the UPDATE statement. If the WHEREclause is omitted, all rows in the table will be updated. The WHERE clause is so important that you should not forget. Sometimes, you may want to change just one row of the table; if you forget the WHERE clause, the UPDATE statement will update all the rows, which is not what you expected.

  • The LOW_PRIORITY keyword is used to delay the execution until no other connections read data from the table. It is used for controlling the update process in MySQL database server.

  • The IGNORE keyword is used to execute the update even there is an error occurred during the execution of the UPDATE statement. The error in the update process could be duplicate value on a unique column, the new value does not match with the column’s data type, etc.

MySQL UPDATE examples

Let’s practice with a couple of examples in the MySQL sample database.

MySQL UPDATE a single column in a table

In this example, we are going to update the email of Mary Patterson to the new email mary.patterso@classicmodelcars.com.

First, to make sure that we update the email successfully, we query Mary’s email using the SELECT statement as follows:

SELECT firstname,                 lastname,                 email FROM employees    WHERE employeeNumber = 1056

mysql update example before update

Second, we can update her current email to the new email mary.patterso@classicmodelcars.com using the UPDATE statement as the following query:

UPDATE employees  SET email = 'mary.patterso@classicmodelcars.com'   WHERE employeeNumber = 1056

Because we just want to update Mary’s record so we use the WHERE clause to specify the Mary’s record ID 1056. The SET clause sets the email column value to the new email.

Third, we execute the SELECT statement again to verify the change.

SELECT firstname,                 lastname,                 email FROM employees    WHERE employeeNumber = 1056

mysql update single column

MySQL UPDATE multiple columns

To update multiple columns, you need to specify them in the SET clause. The following query updates both mary’s last name and email:

UPDATE employees SET lastname = 'Hill',     email = 'mary.hill@classicmodelcars.com' WHERE employeeNumber = 1056;

Le’ts check the changes:

SELECT firstname,                 lastname,                 email FROM employees    WHERE employeeNumber = 1056;

mysql update multiple columns

MySQL UPDATE from SELECT statement

You can provide the values for the SET clause from a SELECT statement that selects data from other tables. For example, first we check if is there any customer who does not have sales representative:

SELECT customername,            salesRepEmployeeNumber FROM customers WHERE salesRepEmployeeNumber IS NULL;

customers with no sales representative

We can take any sale representative and update for those customers:

UPDATE customers SET salesRepEmployeeNumber = (   SELECT employeeNumber    FROM employees   WHERE jobtitle = 'Sales Rep'   LIMIT 1 ) WHERE salesRepEmployeeNumber IS NULL;

The SELECT statement returns an employee number of the employee whose job title is Sales Rep. The UPDATEstatement uses this employee number and update it for the customers whose sales representative is not available.

In this tutorial, you’ve learned how to use MySQL UPDATE statement to update data in database tables.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/mysql-update-data

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消