DML: UPDATE
The UPDATE
statement in SQL is used to modify the existing records in a table.
Basic Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
- table_name: The table to update.
- column1 = value1, column2 = value2, …: The column(s) to update and the new value(s).
- condition: An optional clause to filter rows to update.
It’s very important to always use the WHERE
clause in an UPDATE
statement. Without it, all records will be updated.
Example 1 - Basic UPDATE
For the “customer” table:
customer_id | name | country |
---|---|---|
1 | John | USA |
2 | Michael | Germany |
3 | Sarah | France |
4 | Sally | UK |
If we want to update Sally’s country to Canada, we can use the following SQL:
UPDATE customer
SET country = 'Canada'
WHERE name = 'Sally';
The “customer” table would now look like this:
customer_id | name | country |
---|---|---|
1 | John | USA |
2 | Michael | Germany |
3 | Sarah | France |
4 | Sally | Canada |
Example 2 - Update Multiple Columns
If we want to update both Sally’s name and country to Sam and Australia, we can use the following SQL:
UPDATE customer
SET name = 'Sam', country = 'Australia'
WHERE customer_id = 4;
The “customer” table would now look like this:
customer_id | name | country |
---|---|---|
1 | John | USA |
2 | Michael | Germany |
3 | Sarah | France |
4 | Sam | Australia |
Example 3 - Update All Rows
If we want to update all rows in a column, we can do so without the WHERE
clause. However, be careful because this will change all rows in the specified column(s).
UPDATE product
SET price = price * 1.1;
This will increase the price of all products by 10%.
- SQL UPDATE Statement - W3Schools
- The SQL UPDATE Statement - SQL Tutorial
- Update Data In a MySQL Table Using MySQLi and PDO - W3Schools
- SQL Server UPDATE Statement - SQL Server Tutorial
- UPDATE (Transact-SQL) - Microsoft Docs
Last updated on