eolas/Databases/SQL/9_Utilising_foreign_keys.md
2022-08-21 11:00:04 +01:00

1.3 KiB

categories tags
Databases
Programming Languages
SQL

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:

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

Creating the returns table and establishing relationship with sales using the FOREIGN KEY keyword:

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)
	);

Here's an example with more than one foreign key:

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)
    );