eolas/Databases/SQL/5_UPDATE.md

36 lines
606 B
Markdown
Raw Normal View History

2022-08-05 20:00:04 +01:00
---
2022-08-16 11:58:34 +01:00
categories:
2022-08-05 20:00:04 +01:00
- Databases
2022-08-21 11:00:04 +01:00
- Programming Languages
2022-08-16 11:58:34 +01:00
tags: [SQL]
2022-08-05 20:00:04 +01:00
---
2022-08-06 09:00:04 +01:00
# SQL: The UPDATE query
2022-08-05 20:00:04 +01:00
With UPDATE we modify existing records.
## 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
## Real example
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
## Multiple fields
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
```