Skip to content

SQL UPDATE 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