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.
UPDATE studentspicks the table to change.SET grade = 90means "fill thegradecolumn with 90". To change several columns at once, separate them with commas:SET grade = 90, city = 'Oxford'.WHERE name = 'Rizky'picks the rows to change, just likeWHEREin aSELECT.- Text goes inside single quotes:
'Rizky'. Numbers go without quotes:90.
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.
DELETE FROM studentsmeans "delete from thestudentstable". There is no column list and no*, because a delete always removes whole rows.WHERE city = 'York'picks the rows to delete. If two students were from York, both would be deleted.- To empty one column only, without deleting the row, use
UPDATE ... SET column = NULL.
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:
- Write a
SELECTwith theWHEREyou plan to use, and run it. - Check the rows in its result.
- If they are the right rows, replace
SELECT *withDELETE(or write theUPDATEwith thatWHERE).
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:
SELECTfirst (the way above): enough for a small change where you can check each row.- A transaction: write
BEGIN;before theDELETE, check the result withSELECT, thenCOMMIT;to save orROLLBACK;to cancel. This is the real undo button, covered in the lesson Transactions. RETURNING:DELETE FROM students WHERE city = 'York' RETURNING id, name;shows the rows it just deleted, here Dimas withid7 (SQLite 3.35 and later, PostgreSQL, MariaDB).- A backup: copy the database file, or export it with
pg_dumpormysqldump, before you change many rows at once.
💡 SQLite, MySQL and PostgreSQL differ here
- Double quotes.
WHERE name = "Rizky"works in the SQLite in this browser (and in MySQL with its default settings). When there is no column named Rizky, SQLite reads "Rizky" as text. PostgreSQL reads what is inside double quotes as a column name, so it refuses withcolumn "Rizky" does not exist. The officialsqlite3program in the terminal (version 3.41 and later) refuses too, withno such column: Rizky. Make single quotes your habit for text. - Upper and lower case. In SQLite,
WHERE name = 'rizky'does not matchRizky: 0 rows changed, no error. MySQL, with its default settings, treats upper and lower case as the same, so in MySQL the row matches. - Safety catch. MySQL has a
sql_safe_updatesmode, which is on in MySQL Workbench. This mode refuses anUPDATEorDELETEwithout aWHEREon a key column (an identifying column such as the primary key). SQLite and PostgreSQL run such a command right away.
⚠️ Check these before the query runs
- Forgetting WHERE. Every row changes or is deleted. Check the "rows changed" number every time. If it is bigger than you expected, something is wrong.
- Text without quotes.
WHERE name = Rizkyis refused withno such column: Rizky, because without quotes, Rizky is read as a column name. DELETE * FROM. This comes from the habit of writingSELECT *. SQLite refuses it withnear "*": syntax error.- A WHERE that is too broad.
WHERE city = 'London'can match more than one student. To change exactly one row, use the primary key:WHERE id = 2.
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.