UPDATE — Modify Existing Data
The UPDATE statement allows you to change existing data in a table.
You can update one column, multiple columns, or entire rows.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Important: Always use a WHERE clause unless you want to update all rows.
Example 1: Update One Column
UPDATE employees
SET age = 30
WHERE id = 1;
This changes the age of the employee with id = 1 to 30.
Example 2: Update Multiple Columns
UPDATE employees
SET department = 'Sales', age = 34
WHERE name = 'Thomas';
This updates both department and age for the employee named 'Thomas'.
Common Mistakes
Forgetting the WHERE clause
UPDATE employees
SET age = 30;
Mistake: This will update age = 30 for all employees, which is probably not what you want.
Tip: Always include a WHERE condition to target specific rows:
UPDATE employees
SET age = 30
WHERE id = 1;