SQL Basics | DML: Data Manipulation Language | UPDATE
Use the UPDATE command when modifying data in a table.
UPDATE syntax
The basic SQL for an UPDATE statement is as follows.
UPDATE "table_name"
SET "field1" = [new_value]
WHERE "condition";
You can also modify multiple fields at the same time.
UPDATE "table_name"
SET "field1" = [value1], "field2" = [value2]
WHERE "condition";
UPDATE example
Suppose you have the following table.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| Los Angeles | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
Later, you find that the actual sales for the Los Angeles store on 2018/01/08 were $500, not the $300 stored in the table. Use the following SQL to modify the data.
UPDATE store_information
SET Sales = 500
WHERE Store_Name = 'Los Angeles'
AND Txn_Date = 'Jan-08-2018';
If you query the table again, you can confirm that it has been updated as follows.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| Los Angeles | 500 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
In this example, only one row matches the condition in the WHERE clause. If multiple rows match the condition, all matching rows are modified.