2022-08-05 20:00:04 +01:00
|
|
|
---
|
2024-06-15 11:00:03 +01:00
|
|
|
tags:
|
|
|
|
- SQL
|
|
|
|
- databases
|
2022-08-05 20:00:04 +01:00
|
|
|
---
|
|
|
|
|
2022-12-06 09:29:41 +00:00
|
|
|
# Update existing data with the SQL `UPDATE` command
|
2022-08-05 20:00:04 +01:00
|
|
|
|
2022-12-06 09:29:41 +00:00
|
|
|
We use `UPDATE` to modify existing records in our table.
|
2022-08-05 20:00:04 +01:00
|
|
|
|
|
|
|
## Schematic syntax
|
|
|
|
|
2022-08-16 11:58:34 +01:00
|
|
|
```sql
|
2022-08-05 20:00:04 +01:00
|
|
|
UPDATE [table_name]
|
|
|
|
SET [field]
|
|
|
|
WHERE [conditional expression/filter]
|
2022-08-16 11:58:34 +01:00
|
|
|
```
|
2022-08-05 20:00:04 +01:00
|
|
|
|
2022-12-06 09:29:41 +00:00
|
|
|
## Example
|
2022-08-05 20:00:04 +01:00
|
|
|
|
2022-08-16 11:58:34 +01:00
|
|
|
```sql
|
2022-08-05 20:00:04 +01:00
|
|
|
UPDATE manufacturer
|
|
|
|
SET url = '<http://www.hp.co.uk>'
|
|
|
|
WHERE manufacturer_id = 4; --typically this will be the primary key as you are updating and existing record and need to identify it uniquely
|
2022-08-16 11:58:34 +01:00
|
|
|
```
|
2022-08-05 20:00:04 +01:00
|
|
|
|
2022-12-06 09:29:41 +00:00
|
|
|
## Update multiple fields
|
2022-08-05 20:00:04 +01:00
|
|
|
|
2022-08-16 11:58:34 +01:00
|
|
|
```sql
|
2022-08-05 20:00:04 +01:00
|
|
|
UPDATE manufacturer
|
|
|
|
SET url = '<http://www.apple.co.uk>',
|
|
|
|
year_founded = 1977
|
|
|
|
WHERE manufacturer_id = 2;
|
2022-08-16 11:58:34 +01:00
|
|
|
```
|