Skip to content

SQL UPDATE Statement ✏️¢

Prerequisites: SELECT Statement

Mentor's Note: People change. Prices change. Phone numbers change. The UPDATE statement is how your database keeps up with the real world. But be careful: forgetting a WHERE clause is the fastest way to get fired! ⚠️


🌟 The Scenario: The Birthday Party πŸŽ‚ΒΆ

Imagine you have a list of friends. - The Update: Your friend "Rahul" just turned 21. - The Precise Action: You find Rahul's name and change his age to 21. βœ… - The Disaster: You forget to look for Rahul and just shout "EVERYONE IS 21!" Now your list is ruined. ❌


🎨 Visual Logic: Targeted Modification¢

graph LR
    A[(Table)] -- "WHERE id = 5" --> B[Target Row]
    B -- "SET price = 100" --> C[Updated Row]

πŸ’» 1. The Basic SyntaxΒΆ

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example: Update a specific recordΒΆ

-- Scenario: Update phone number for Student 101
UPDATE students
SET phone = '9876543210'
WHERE roll_no = 101;

πŸ’» 2. Updating Multiple Columns πŸ“‘ΒΆ

You can change several fields at once by separating them with commas.

-- Scenario: Student moved house and changed email
UPDATE students
SET 
    city = 'Surat',
    email = '[email protected]'
WHERE roll_no = 105;

πŸ’» 3. Updating with Logic (Expressions) βš™οΈΒΆ

You can use the existing value to calculate the new one.

-- Scenario: Give everyone in 'Sales' a 5% raise
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';

⚠️ The "No-Where" Danger Zone¢

If you omit the WHERE clause, EVERY SINGLE ROW in the table will be updated.

-- πŸ’£ DO NOT DO THIS (unless you mean it)
UPDATE students SET status = 'Expelled';
-- Result: Congratulations, you have no students left!

πŸ’‘ Pro Tip: The "Safety First" WorkflowΒΆ

"Before running an UPDATE, run a SELECT with the exact same WHERE clause. If the SELECT shows the correct 5 people, then copy that WHERE clause into your UPDATE statement!"


πŸ“ˆ Learning PathΒΆ