SQL - MySQL

Module 01 - Database Theory
1. Introduction to Databases2. DBMS Theory Concepts3. Types of Keys4. Database Relationships5. DBMS Interview Questions
Module 02 - CRUD Operations
1. Create - INSERT2. Read - SELECT3. Update - UPDATE4. Delete - DELETE5. Alter - ALTER TABLE
MySQL Playground
Profile
Akkal DhamiFull Stack Developer

Building modern web experiences with a focus on performance, scalability, and clean architecture.

© 2026 | Akkal Dhami | All rights reserved

Built with
byAkkal Dhami

Navigation

  • Projects
  • Dev Setup
  • Playbook
  • Templates
  • Networking
  • SQL - MySQL
  • SQL Playground
  • DSA
AKKAL DHAMIAKKAL DHAMIAKKAL DHAMI

Update - UPDATE

UPDATE modifies existing rows in a table. Always pair it with WHERE — leave that off and it changes every row in the table.


Basic syntax

example.sql
UPDATE table_name
SET column = value
WHERE condition;
  • UPDATE — which table to modify
  • SET — the new value(s)
  • WHERE — which rows to apply it to

Updating one row

example.sql
UPDATE books
SET price = 11.99
WHERE title = 'Dune';

Output:

# Before update:
+----+-----------------+---------------+-------+----------+---------------------+
| id | title           | author        | price | in_stock | created_at          |
+----+-----------------+---------------+-------+----------+---------------------+
|  1 | Dune            | Frank Herbert | 15.50 |        1 | 2026-07-15 16:34:45 |
|  2 | 1984            | George Orwell |  9.99 |        1 | 2026-07-15 16:34:45 |
|  3 | Brave New World | Aldous Huxley | 10.25 |        1 | 2026-07-15 16:34:45 |
+----+-----------------+---------------+-------+----------+---------------------+
 
# After update:
+----+-----------------+---------------+-------+----------+---------------------+
| id | title           | author        | price | in_stock | created_at          |
+----+-----------------+---------------+-------+----------+---------------------+
|  1 | Dune            | Frank Herbert | 11.99 |        1 | 2026-07-15 16:34:45 |
|  2 | 1984            | George Orwell |  9.99 |        1 | 2026-07-15 16:34:45 |
|  3 | Brave New World | Aldous Huxley | 10.25 |        1 | 2026-07-15 16:34:45 |
+----+-----------------+---------------+-------+----------+---------------------+

Updating multiple columns at once

Separate each column with a comma:

example.sql
UPDATE books
SET price = 8.99, in_stock = FALSE
WHERE title = '1984';

Updating multiple rows

WHERE can match more than one row — every matching row gets updated:

example.sql
UPDATE books
SET in_stock = FALSE
WHERE price > 15;

The most important rule

Forgetting WHERE updates the entire table:

example.sql
-- Danger: this sets EVERY book's price to 0
UPDATE books
SET price = 0;

There's no built-in undo once this runs (unless you're inside a transaction — covered in Module 03).


Preview before you update

Run the same condition as a SELECT first, to see exactly which rows will be affected:

example.sql
SELECT * FROM books WHERE title = 'Dune';

If that returns the rows you expect, swap it for the UPDATE with confidence.


MySQL's safety net

MySQL's "safe update mode" can block an UPDATE that doesn't use a key column in WHERE, throwing Error 1175. If you hit this, it's protecting you from an accidental mass update — not a bug.


Quick Reference

GoalExample
Update one rowUPDATE books SET price = 11.99 WHERE title = 'Dune';
Update multiple columnsSET price = 8.99, in_stock = FALSE
Update multiple rowsWHERE price > 15
Preview before updatingRun the same WHERE as a SELECT first
update-reference.sql
-- Updating one row
UPDATE books
SET price = 11.99
WHERE title = 'Dune';
 
-- Updating multiple columns at once
UPDATE books
SET price = 8.99, in_stock = FALSE
WHERE title = '1984';
 
-- Updating multiple rows
UPDATE books
SET in_stock = FALSE
WHERE price > 15;