2022-08-05 20:30:04 +01:00
---
2022-08-16 11:58:34 +01:00
categories:
2022-08-05 20:30: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:30:04 +01:00
---
# SQL: Creating relationships between tables with `PRIMARY` and `FOREIGN` keys
We will demonstrate with an example. We already have the `sales` table. We want to create new table called `returns` that will sustain a one-to-one relationship with `sales` . We are going to use the `sale_id` as our foreign key in `returns` . This is the primary key in `sales` .
The `sales` table:
2022-08-16 11:58:34 +01:00
```
2022-08-05 20:30:04 +01:00
sale_id model_id sale_date employee_id price
---------- ---------- ---------- ----------- ----------
1 44 2020-07-27 tbishop 399.99
2 22 2021-02-07 tbishop 200.99
2022-08-16 11:58:34 +01:00
```
2022-08-05 20:30:04 +01:00
2022-08-16 11:58:34 +01:00
Creating the `returns` table and establishing relationship with `sales` using the `FOREIGN KEY` keyword:
2022-08-05 20:30:04 +01:00
2022-08-16 11:58:34 +01:00
```sql
2022-08-05 20:30:04 +01:00
CREATE TABLE returns (
return_id integer PRIMARY KEY,
sale_id integer NOT NULL,
date_returned text,
reason text,
FOREIGN KEY (sale_id) REFERENCES sales(sale_id)
);
2022-08-16 11:58:34 +01:00
```
2022-08-05 20:30:04 +01:00
Here's an example with more than one foreign key:
2022-08-16 11:58:34 +01:00
```sql
2022-08-05 20:30:04 +01:00
CREATE TABLE returns (
return_id integer PRIMARY KEY,
sale_id integer NOT NULL,
employee_id text NOT NULL,
date_returned text,
reason text,
FOREIGN KEY(sale_id) REFERENCES sales(sale_id),
FOREIGN KEY(employee_id) REFERENCES employee(employee_id)
);
2022-08-16 11:58:34 +01:00
```