eolas/Databases/SQL/3_INSERT.md

30 lines
708 B
Markdown
Raw Normal View History

2022-08-06 09:00:04 +01:00
---
2022-08-16 11:58:34 +01:00
categories:
2022-08-06 09:00:04 +01:00
- Databases
2022-08-16 11:58:34 +01:00
- Programming_Languages
tags: [SQL]
2022-08-06 09:00:04 +01:00
---
# SQL: INSERT
## Adding a record
2022-08-16 11:58:34 +01:00
```sql
2022-08-06 09:00:04 +01:00
INSERT INTO sales
VALUES (1, 11, '2020-01-01','mhogan');
2022-08-16 11:58:34 +01:00
```
2022-08-06 09:00:04 +01:00
If you intend to miss out a value, you shouldn't leave it blank, you should instead use `NULL` :
2022-08-16 11:58:34 +01:00
```sql
2022-08-06 09:00:04 +01:00
INSERT INTO sales
VALUES (1, 11, '2020-01-01', NULL);
2022-08-16 11:58:34 +01:00
```
2022-08-06 09:00:04 +01:00
2022-08-16 11:58:34 +01:00
> There is a problem with this format: it only works so long as the order to the values in the `VALUES` clause corresponds to the order of the fields in the tables. To rule out error we should instead specify these fields along with the table name:
2022-08-06 09:00:04 +01:00
2022-08-16 11:58:34 +01:00
```sql
2022-08-06 09:00:04 +01:00
INSERT INTO sales**(employee_id, sale_id, model_id, sale_date)**
VALUES ('mhogan', 1, 11, '2020-01-01',);
2022-08-16 11:58:34 +01:00
```