MySQL UPDATE
This article describes how to use MySQL UPDATE statement to update one or more rows.
If you want to update existing rows in a table, you can use the UPDATE statement.
UPDATE syntax
The UPDATE statement can update one or more columns in one or more rows. Here is the basic syntax of the UPDATE statement:
UPDATE [IGNORE] table_name
SET
column_name1 = value1,
column_name2 = value2,
...
[WHERE clause];
Here:
- You should specify the name of the table whose rows will be updated after the
UPDATEkeyword. - Use
SETclause to set new values of one or more columns. Use commas to separate multiple columns. The column value can be an literal value, an expression, or a subquery. - Use
WHEREclause to filter the rows that will be updated. - The
WHEREclause is optional. If you do not specify aWHEREclause, all rows in the table is updated.
In a UPDATE Statement, the WHERE clause is very important. Please do not omit WHERE clauses.
UPDATE example
Let us show you a couple of examples of UPDATE.
In the following examples, we use the customer table from Sakila sample database as demonstrations.
Update one column value
In this example, we will change the customer’s email to [email protected] whose customer_id value is 1.
-
Use the following
SELECTstatement to show the current values.SELECT first_name, last_name, email FROM customer WHERE customer_id = 1;+------------+-----------+-------------------------------+ | first_name | last_name | email | +------------+-----------+-------------------------------+ | MARY | SMITH | [email protected] | +------------+-----------+-------------------------------+ 1 row in set (0.00 sec) -
Use the following
UPDATEstatement to update theemailvalue.UPDATE customer SET email = '[email protected]' WHERE customer_id = 1;Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0In this
UPDATEstatement:WHEREclause specifies the filtering condition:customer_id = 1.- to Set
emailnew value in theSETclause.
In the
UPDATEreturn output:1 row affectedindicates that 1 row is affected. That is, 1 row is updated.Rows matched: 1indicates the number of rows which matchsWHEREcondition.Changed: 1indicates that the number of updated rows.Warnings: 0indicates that there is not Warnings.
-
Use the following
SELECTstatement to show row after updated.SELECT first_name, last_name, email FROM customer WHERE customer_id = 1;+------------+-----------+-----------------------------------+ | first_name | last_name | email | +------------+-----------+-----------------------------------+ | MARY | SMITH | [email protected] | +------------+-----------+-----------------------------------+ 1 row in set (0.01 sec)
Update multiple columns values
In this example, we will update the customer first_name, last_name, email values at the same time.
UPDATE customer
SET first_name = 'Tim',
last_name = 'Duncan',
email = '[email protected]'
WHERE customer_id = 1;
Then, we check the result:
SELECT first_name, last_name, email
FROM customer
WHERE customer_id = 1;
+------------+-----------+-------------------------------+
| first_name | last_name | email |
+------------+-----------+-------------------------------+
| Tim | Duncan | [email protected] |
+------------+-----------+-------------------------------+
1 row in set (0.00 sec)
Update using expression
In the UPDATE statment, the column value can be set to an expression, function and so on.
The following statment updates e-mail domain part of all customers:
UPDATE customer
SET email = REPLACE(email, 'sakilacustomer.org', 'sqliz.com');
Query OK, 599 rows affected (0.03 sec)
Rows matched: 599 Changed: 599 Warnings: 0
Note that there is not a WHERE clause in the statement, so all rows in the table have been updated.
Update using subquery
The following example shows how to set a random store for customers who have not been binded to a store.
UPDATE customer
SET store_id = (
SELECT store_id
FROM store
ORDER BY RAND()
LIMIT 1
)
WHERE store_id IS NULL;
In this example, the following SELECT statement returns a random store id:
SELECT store_id
FROM store
ORDER BY RAND()
LIMIT 1
In the SET clause, the store_id value is set to the store id retured of the above sub-queries.
UPDATE modifier
In MySQL, UPDATE statements support 2 modifiers:
-
LOW_PRIORITY: If you specifyLOW_PRIORITYmodifier, MySQL server will delay the execution of theDELETEoperation until there are no clients there are no clients who read on the table.LOW_PRIORITYmodifier is supported by those storage engines which only has table-level locking, such as:MyISAM,MEMORY, andMERGE. -
IGNORE: If you specify aIGNOREmodifier, MySQL server will performUPDATEignore those errors can be ignored during the operation. These errors return asWARNING.
Here is the usage of modifiers:
UPDATE [LOW_PRIORITY] [IGNORE] table_name SET column_name = value
Conclusion
In this article, you learned how to update rows using MySQL UPDATE statement. The following are the main points of the UPDATE statement:
- You should specify the name of the table whose rows will be updated after the
UPDATEkeyword. - Use
SETclause to set new values of one or more columns. Use commas to separate multiple columns. The column value can be an literal value, an expression, or a subquery. - Use
WHEREclause to filter the rows that will be updated. - The
WHEREclause is optional. If you do not specify aWHEREclause, all rows in the table is updated. - The
UPDATEstatements supportsLOW_PRIORITYandIGNOREmodifiers. If you want to ignore some wrong line, you can use theIGNOREmodifier.