MySQL
MySQL

UPDATE

UPDATE changes existing data in a table. Unlike INSERT, which adds new rows, UPDATE modifies rows that are already there. UPDATE students SET age = 17 WHERE id = 1; changes the age column to 17, but only for the row where id equals 1. The SET clause defines what changes; the WHERE clause defines which rows are affected. You can update multiple columns in one statement: UPDATE students SET age = 17, name = "Ali Khan" WHERE id = 1;, separating each column = value pair with a comma. This is the single most important habit in this entire topic: never run UPDATE without a WHERE clause, unless you genuinely intend to change every single row in the table. UPDATE students SET age = 17; with no WHERE sets every student's age to 17 a mistake that's easy to make and hard to undo.

Why this matters

  • ·UPDATE lets you correct or refresh existing data without deleting and re-inserting it.
  • ·SET can change several columns in a single, efficient statement.
  • ·A precise WHERE clause lets you target exactly the row (or rows) that actually need changing.

Common mistakes

  • ·Running UPDATE without a WHERE clause changes every row in the table one of the most damaging and common SQL mistakes.
  • ·A WHERE condition that's broader than intended can update more rows than you meant to.
  • ·There is no automatic undo always double-check an UPDATE's WHERE clause, ideally by running the same condition as a SELECT first.
Your codePHP
Expected output
Query OK, 1 row affected

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

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

INSERT adds a brand-new row. UPDATE modifies values in a row (or rows) that already exist.

Practice questions

Question 1

Write an UPDATE that changes Sara's age to 18, using her id.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Write an UPDATE that changes both the name and age for the student with id 1.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

What does UPDATE do?