30 lines
1.2 KiB
PHP
30 lines
1.2 KiB
PHP
A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integrity between two related tables.
|
|
|
|
Say you have the product table that we have used several times already:
|
|
|
|
CREATE TABLE products (
|
|
product_no integer PRIMARY KEY,
|
|
name text,
|
|
price numeric
|
|
);
|
|
|
|
Let's also assume you have a table storing orders of those products. We want to ensure that the orders table only contains orders of products that actually exist. So we define a foreign key constraint in the orders table that references the products table:
|
|
|
|
CREATE TABLE orders (
|
|
order_id integer PRIMARY KEY,
|
|
product_no integer REFERENCES products (product_no),
|
|
quantity integer
|
|
);
|
|
|
|
Now it is impossible to create orders with product_no entries that do not appear in the products table.
|
|
|
|
We say that in this situation the orders table is the referencing table and the products table is the referenced table. Similarly, there are referencing and referenced columns.
|
|
|
|
You can also shorten the above command to
|
|
|
|
CREATE TABLE orders (
|
|
order_id integer PRIMARY KEY,
|
|
product_no integer REFERENCES products,
|
|
quantity integer
|
|
);
|