Updating & Deleting Data — SQL

UPDATE table SET column = value WHERE condition; replaces a column's value in the rows that meet the condition. DELETE FROM table WHERE condition; deletes the…

In the shop's debt book, Mrs. Sri wrote a number wrong: Rizky's debt should be 90 thousand, not 85 thousand. She crossed the number out and wrote the right one. The next day Dimas paid his whole debt, so Dimas's line was crossed out. Before she crosses anything out, Mrs. Sri always checks which line she is pointing at.

In a database, each of those two jobs has its own command. UPDATE replaces column values in rows that already exist, like Rizky's debt written again. DELETE removes rows from a table, like Dimas's line crossed out. WHERE decides which rows are changed or deleted. Be careful: a database never asks "are you sure?", and it has no undo button.

UPDATE: replace values in the chosen rows

The students table in the Lab and the exercise holds 4 students:

id name grade city
1 Dewi 90 London
2 Rizky 85 Manchester
3 Maya 92 Leeds
4 Dimas 82 York

The query below changes Rizky's grade to 90:

UPDATE students
SET grade = 90
WHERE name = 'Rizky';

SQLite reports 1 row changed: Rizky's grade is now 90, and the other three rows stay as they were.

The new value may also be calculated from the old one. SET grade = grade + 5 adds 5 to the current value of grade.

DELETE: remove the chosen rows

The query below deletes the students whose city is York:

DELETE FROM students
WHERE city = 'York';

SQLite reports 1 row changed: Dimas's row is gone, and the table now holds 3 rows.

Without WHERE: every row is hit

Guess first: in the students table as it was at the start, with 4 students, how many rows does the query below change?

UPDATE students SET grade = 90;

SQLite reports 4 rows changed: every student now has 90. There is no error and no warning. DELETE FROM students; does the same: every row is deleted, and the table is empty (0 rows).

So do what Mrs. Sri does: look at the rows first. Before you run an UPDATE or a DELETE:

  1. Write a SELECT with the WHERE you plan to use, and run it.
  2. Check the rows in its result.
  3. If they are the right rows, replace SELECT * with DELETE (or write the UPDATE with that WHERE).

For example, to delete the students from York:

SELECT * FROM students WHERE city = 'York';  -- 1 row: Dimas
DELETE FROM students WHERE city = 'York';

Other options to stay safe:

💡 SQLite, MySQL and PostgreSQL differ here

⚠️ Check these before the query runs

TL;DR: UPDATE table SET column = value WHERE condition; replaces a column's value in the rows that meet the condition. DELETE FROM table WHERE condition; deletes the rows that meet the condition. Without WHERE, every row is changed or deleted, with no error and no undo. Text goes inside single quotes. Before you change or delete, first run a SELECT ... WHERE with the same condition to see the rows. Afterwards, check how many rows changed.

⚡ UPDATE: replace a value

Write:

UPDATE students
SET grade = 90
WHERE name = 'Rizky';

Meaning: UPDATE students changes rows that already exist in the students table. SET writes the new value: SET grade = 90 fills the grade column with 90. WHERE picks which rows change. SQLite reports 1 row changed: only Rizky's row.

⚡ DELETE: remove rows

Write:

DELETE FROM students
WHERE city = 'York';

Meaning: DELETE FROM students deletes rows from the students table, and WHERE picks which rows are deleted. A delete always removes whole rows, so there is no column list and no *. If two students are from York, both are deleted.

⚡ Without WHERE

Guess first: DELETE FROM students;, with no WHERE, runs without an error. So what is the danger?

Every row is deleted, with no warning and no undo. UPDATE without WHERE is the same: every row changes.

Meaning: After you run an UPDATE or a DELETE, check the "rows changed" number that SQLite reports. If it is bigger than you expected, something is wrong.

⚡ SELECT first, then delete

Write:

SELECT * FROM students WHERE city = 'York';
DELETE FROM students WHERE city = 'York';

Meaning: Before you delete, run a SELECT with the same WHERE, and check the rows in its result. If they are the right rows, only then replace SELECT * with DELETE.

What you will learn