TExcelTAD
Home › MySQL › MySQL UPDATE

MySQL UPDATE

UPDATE changes values in existing rows. You say which table, which columns get which new values, and which rows:

sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
mysql · try it yourself
UPDATE customers
SET city = 'Sofia'
WHERE customer_id = 9;

SELECT customer_id, first_name, city, country FROM customers WHERE customer_id = 9;
+-------------+------------+-------+----------+
| customer_id | first_name | city  | country  |
+-------------+------------+-------+----------+
|           9 | Ivan       | Sofia | Bulgaria |
+-------------+------------+-------+----------+

Several columns, computed values

The new value can be any expression, including one based on the current value of the column:

mysql · try it yourself
UPDATE products
SET unit_price = unit_price * 1.10,
    units_in_stock = units_in_stock + 24
WHERE category_id = 2 AND discontinued = 0;

SELECT name, unit_price, units_in_stock FROM products WHERE category_id = 2;
+------------------+------------+----------------+
| name             | unit_price | units_in_stock |
+------------------+------------+----------------+
| Sourdough Loaf   |       5.28 |             49 |
| Butter Croissant |       1.76 |            144 |
| Chocolate Cake   |      19.80 |             32 |
| Rye Crackers     |       2.90 |              0 |
+------------------+------------+----------------+

The prices became 5.28, 1.76 and 19.80: the result is rounded to the column's two decimal places. Rye Crackers is discontinued, so it was left alone.

Don't forget WHERE

Without a WHERE clause, UPDATE changes every row in the table:

mysql · try it yourself
UPDATE employees SET salary = salary + 100;
SELECT first_name, salary FROM employees WHERE employee_id <= 3;
SELECT ROW_COUNT() AS rows_changed;
+------------+---------+
| first_name | salary  |
+------------+---------+
| Grace      | 9600.00 |
| Alan       | 6300.00 |
| Ada        | 5900.00 |
+------------+---------+
+--------------+
| rows_changed |
+--------------+
|           -1 |
+--------------+

ROW_COUNT() is -1 there, because the statement just before it was a SELECT. It reports the rows changed by the previous statement, so call it right after the UPDATE:

mysql · try it yourself
UPDATE orders SET status = 'delivered' WHERE status = 'shipped';
SELECT ROW_COUNT() AS rows_changed;
+--------------+
| rows_changed |
+--------------+
|            2 |
+--------------+

Tip: Before running a risky UPDATE, run a SELECT with the same WHERE clause and check that it returns exactly the rows you mean to change. Many teams also enable SET sql_safe_updates = 1;, which makes MySQL refuse UPDATE and DELETE statements that have no key-based WHERE or LIMIT.

Updating from another table

MySQL lets an UPDATE join other tables, so new values can come from related rows. Here the tea gets more expensive, and then every line of the still-open order 22 is re-priced from the product's current price (a typical "re-price the cart" step):

mysql · try it yourself
UPDATE products SET unit_price = 6.95 WHERE product_id = 2;

UPDATE order_details AS d
JOIN products AS p ON p.product_id = d.product_id
SET d.unit_price = p.unit_price
WHERE d.order_id = 22;

SELECT product_id, unit_price FROM order_details WHERE order_id = 22;
+------------+------------+
| product_id | unit_price |
+------------+------------+
|          2 |       6.95 |
|         20 |      11.90 |
+------------+------------+

Joins are explained properly later in the course; for now, just note that this form exists.

Test yourself

What does UPDATE products SET unit_price = 0; do?

  1. Nothing without WHERE
  2. Sets the price of the first product to 0
  3. Sets the price of every product to 0
  4. Causes a syntax error

How do you set two columns in one UPDATE?

  1. SET a = 1 AND b = 2
  2. SET a = 1, b = 2
  3. SET (a, b) = 1, 2
  4. Two SET clauses

What does ROW_COUNT() return right after an UPDATE?

  1. The number of rows in the table
  2. The number of rows actually changed
  3. The new AUTO_INCREMENT value
  4. Always 1