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
UPDATE
keyword. - Use
SET
clause 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
WHERE
clause to filter the rows that will be updated. - The
WHERE
clause is optional. If you do not specify aWHERE
clause, 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
SELECT
statement 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
UPDATE
statement to update theemail
value.UPDATE customer SET email = '[email protected]' WHERE customer_id = 1;
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
In this
UPDATE
statement:WHERE
clause specifies the filtering condition:customer_id = 1
.- to Set
email
new value in theSET
clause.
In the
UPDATE
return output:1 row affected
indicates that 1 row is affected. That is, 1 row is updated.Rows matched: 1
indicates the number of rows which matchsWHERE
condition.Changed: 1
indicates that the number of updated rows.Warnings: 0
indicates that there is not Warnings.
-
Use the following
SELECT
statement 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_PRIORITY
modifier, MySQL server will delay the execution of theDELETE
operation until there are no clients there are no clients who read on the table.LOW_PRIORITY
modifier is supported by those storage engines which only has table-level locking, such as:MyISAM
,MEMORY
, andMERGE
. -
IGNORE
: If you specify aIGNORE
modifier, MySQL server will performUPDATE
ignore 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
UPDATE
keyword. - Use
SET
clause 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
WHERE
clause to filter the rows that will be updated. - The
WHERE
clause is optional. If you do not specify aWHERE
clause, all rows in the table is updated. - The
UPDATE
statements supportsLOW_PRIORITY
andIGNORE
modifiers. If you want to ignore some wrong line, you can use theIGNORE
modifier.