foreign key

A column holding another table's primary key value, which the database guarantees points at a row that exists.

In more detail

Declared with REFERENCES, it stops you inserting an order for a customer who does not exist and, depending on the delete action, stops you deleting a customer who still has orders. PostgreSQL always enforces foreign keys; SQLite enforces them only after PRAGMA foreign_keys = ON on each connection.

Example

customer_id INTEGER NOT NULL REFERENCES customers(id)

Related terms

Taught in

References