The UPDATE statement in MySQL is used to modify existing records in a table. Use SET to assign new values and WHERE to limit which rows change. Omitting WHERE updates every row.
// Syntax for UPDATE statement
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
// Update the 'age' of a student named John Doe
UPDATE students
SET age = 23
WHERE name = 'John Doe';
// Update multiple columns for Jane Smith
UPDATE students
SET age = 24, department = 'Science'
WHERE name = 'Jane Smith';
The first query finds rows where name = 'John Doe' and sets their age to 23. The second query updates age and department for rows where name = 'Jane Smith'. If no rows match, zero rows are changed.
SELECT with the same WHERE first to preview affected rows.START TRANSACTION / COMMIT) for multi-step updates.WHERE for large tables.LIMIT cautiously where supported to restrict changes.department of a student where student_id = 101.age by 1 for all students in the 'Science' department.