Back to subject
MySQL/DELETE

Remove a Row the Careful Way

Practical lesson

Let's read a DELETE statement with the same care an UPDATE deserves arguably even more, since DELETE has no undo.

DELETE FROM students WHERE id = 2; targets exactly one row: the student with id 2 (Sara, from our earlier examples). The table itself, and every other row in it, is completely unaffected.

Query OK, 1 row affected confirms exactly one row was removed. Just like with UPDATE, this number is worth checking every time if it doesn't match what you expected, stop and investigate before doing anything else.

SELECT * FROM students; afterward confirms the result: only Ali's row remains, and the table's structure (its columns and types) is exactly as it was before DELETE only touched data, never the table's definition.

The golden rule for both UPDATE and DELETE is the same: before running either one for real, run its exact WHERE condition as a plain SELECT first. If the SELECT shows precisely the rows you meant to change or remove, and nothing else, you can safely proceed.

Try it yourself
Code

Starter example

Code
DELETE FROM students WHERE id = 2;
SELECT * FROM students;
Live preview
Query OK, 1 row affected

+----+------+-----+
| id | name | age |
+----+------+-----+
| 1  | Ali  | 17  |
+----+------+-----+
1 row in set