HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL UPDATE Statement

Written by RedoHub Team · Last updated: August 1, 2026

The UPDATE statement is used to modify existing data in a table. It is the U in CRUD — Update. You use it whenever a value in the database needs to change — correcting a typo, updating a price, changing a user's email address, or marking an order as shipped.


Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  • SET specifies which columns to change and what their new values should be.
  • WHERE filters which rows to update. Without it, every row in the table is updated.

Sample Table

All examples on this page use the following students table:

idnamecityage
1Rahim UddinDhaka22
2Sara BegumChittagong20
3Karim AliSylhet23
4Nila AkterDhaka21
5Rafi HossainRajshahi24

Updating a Single Row

The most common pattern is targeting a specific row by its primary key. This guarantees only one row is affected:

UPDATE students
SET city = 'Khulna'
WHERE id = 2;

Sara Begum's city is now changed from Chittagong to Khulna. MySQL confirms:

Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Updating Multiple Columns at Once

You can update as many columns as you need in one statement by separating them with commas in the SET clause:

UPDATE students
SET city = 'Barishal', age = 25
WHERE id = 3;

Karim Ali now has city = 'Barishal' and age = 25.


Updating Multiple Rows

If the WHERE condition matches more than one row, all matching rows are updated. The example below gives every student from Dhaka a new city:

UPDATE students
SET city = 'Narayanganj'
WHERE city = 'Dhaka';
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

Both Rahim Uddin (id=1) and Nila Akter (id=4) are updated in one operation.


Using Expressions in SET

The new value does not have to be a fixed number or string — you can use an expression based on the current value of the column:

-- Increase every student's age by 1
UPDATE students
SET age = age + 1;
-- Apply a 10% discount to all products priced above 500
UPDATE products
SET price = price * 0.90
WHERE price > 500;

Verify Before You Update

Before running an UPDATE, it is good practice to run a SELECT with the same WHERE condition first to confirm exactly which rows will be affected:

-- Step 1: Check which rows will be affected
SELECT * FROM students WHERE city = 'Dhaka';

-- Step 2: Once confirmed, run the update
UPDATE students
SET city = 'Narayanganj'
WHERE city = 'Dhaka';
Tip: This "select before update" habit can save you from accidentally modifying the wrong rows — especially when writing complex conditions.

The Danger of UPDATE Without WHERE

If you omit the WHERE clause, MySQL updates every single row in the table — there is no confirmation prompt:

-- DANGEROUS: This updates ALL students' city
UPDATE students
SET city = 'Dhaka';
Warning: An UPDATE without WHERE is one of the most common and costly mistakes in MySQL. Always double-check that your WHERE clause is present and correct before executing an update. Some database tools have a "safe mode" that prevents UPDATE and DELETE statements without a WHERE clause.

Key Points to Remember

  • UPDATE modifies existing rows — it never adds new rows or removes them.
  • Always use a WHERE clause to target specific rows — omitting it updates the entire table.
  • Target rows by primary key (WHERE id = n) when you want to update exactly one row.
  • You can update multiple columns in one statement using a comma-separated SET list.
  • Run a SELECT with the same WHERE condition first to preview what will be changed.
  • Expressions like price = price * 0.90 are valid in the SET clause.
What's next? The next lesson covers DELETE — how to permanently remove rows from a table, and how to do it safely.

Common Mistakes to Avoid

Running UPDATE without testing the WHERE clause first. An off-by-one condition (age >= 21 instead of age > 21) can silently update the wrong set of rows — always preview with a matching SELECT first.
Assuming UPDATE creates a row if none match. It doesn't — if the WHERE condition matches zero rows, MySQL reports "0 rows affected" and nothing happens. Use INSERT ... ON DUPLICATE KEY UPDATE for upsert behavior instead.
Updating a column based on its own current value without considering execution order. SET price = price * 0.9 is safe per-row, but combining multiple SET expressions that reference each other in the same statement can behave unexpectedly — test carefully.

Try It: A Safe Two-Step Update

A second example — previewing exactly which rows will change before committing to the update:

-- Step 1: preview
SELECT id, name, age FROM students WHERE age < 20;

-- Step 2: apply, once the preview looks correct
UPDATE students
SET age = age + 1
WHERE age < 20;

Frequently Asked Questions

Q: Can I undo an UPDATE after running it?

A: Only if it ran inside an explicit transaction that hasn't been committed yet (ROLLBACK). Without a transaction, there's no built-in undo — always back up important data first.

Q: Does UPDATE change AUTO_INCREMENT values?

A: No — AUTO_INCREMENT only affects inserts. Updating a row's other columns has no effect on the ID counter.

Q: How do I update a row using a value from another table?

A: Use an UPDATE ... JOIN. See the official UPDATE documentation for multi-table update syntax.