Careeroza
UPDATE
The SQL UPDATE Statement
UPDATE changes existing records.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;Note: Be careful! The WHERE clause decides which rows are updated. If you leave it out, every row in the table is updated.
Update One Row
Example
Give customer 7 a city:
UPDATE Customers
SET City = 'Singapore', ContactName = 'Mei Lin Chen'
WHERE CustomerID = 7;Running SELECT * FROM Customers WHERE CustomerID = 7; afterwards shows:
Result: 1 row
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 7 | Golden Spoon | Mei Lin Chen | Singapore | Singapore |
Update Multiple Rows
Example
Give every Support employee a 10% raise:
UPDATE Employees
SET Salary = Salary * 1.10
WHERE Department = 'Support';Running SELECT FirstName, Department, Salary FROM Employees; afterwards shows:
Result: 6 rows
| FirstName | Department | Salary |
|---|---|---|
| Rahul | Sales | 72000 |
| Sara | Sales | 58000 |
| David | Sales | 61000 |
| Ananya | Support | 49500 |
| James | Support | 73700 |
| Fatima | Finance | 80000 |
Update Warning!
-- Without WHERE, every product gets price 0!
UPDATE Products
SET Price = 0;Tip: Before running an UPDATE or DELETE, run a SELECT with the same WHERE clause to see exactly which rows will change. In production, do it inside a transaction so you can ROLLBACK.