Skip to content
Commitwise
Menu

Foreign key

Also called: FK, Referential integrity

A column that points at a row in another table, and that the database keeps valid.

Definition

A foreign key constraint makes the database reject a value that doesn't exist in the referenced table, and reject deleting a referenced row unless ON DELETE says otherwise (CASCADE, SET NULL). It models one-to-many relationships directly and many-to-many ones through a junction table. PostgreSQL doesn't index the referencing column automatically, so joins and parent deletes need an index you add yourself.

In practice

orders.customer_id REFERENCES customers(id): an order for customer 999 that doesn't exist fails to insert.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.