Back to subject
MySQL/UPDATE

Change One Row Safely

Practical lesson

Let's read an UPDATE statement carefully, the way you always should before running one for real.

UPDATE students SET age = 17 WHERE id = 1; has three parts worth checking in order: which table (students), what's changing (SET age = 17), and which rows (WHERE id = 1) in that order, before you ever run it.

Query OK, 1 row affected confirms exactly one row was changed a valuable sanity check. If this had said 2 rows affected, that would be an immediate signal that the WHERE clause matched more rows than intended.

SELECT * FROM students; afterward shows the result: Ali's age is now 17, while Sara's row (id 2) is completely untouched, exactly as the WHERE id = 1 condition intended.

A genuinely good habit, especially while learning: before running an UPDATE, run the same WHERE condition as a plain SELECT first (SELECT * FROM students WHERE id = 1;) to see exactly which rows it would affect then, and only then, change SELECT * to your intended UPDATE ... SET.

Try it yourself
Code

Starter example

Code
UPDATE students SET age = 17 WHERE id = 1;
SELECT * FROM students;
Live preview
Query OK, 1 row affected

+----+------+-----+
| id | name | age |
+----+------+-----+
| 1  | Ali  | 17  |
| 2  | Sara | 17  |
+----+------+-----+
2 rows in set