Skip to content
Commitpedia
Log in Join & edit

What is a foreign key?

A foreign key is a column that points to the primary key of another table, linking related rows together.

Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?

Edit history of What is a foreign key?: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
1 min

A foreign key is a column whose values must match a primary key in another table. If an orders table has a customer_id column, each value must be a real customer. It is a reference like a footnote number: the footnote only makes sense if the numbered note actually exists at the bottom of the page.

Foreign keys are what make a relational database relational. They let you JOIN orders with customers, and they protect data: the database refuses to insert an order for customer 999 if no such customer exists, and can delete or block related rows when the parent goes away (ON DELETE CASCADE). Diagrams of tables and their foreign keys are called ER diagrams.

CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  customer_id INT REFERENCES customers (id)
);

See also: What is a primary key? and What is SQL?

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Dominik +811 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?