Skip to content
StackDecode
System Design
4. Database FundamentalsAnimated

Foreign Keys

A column that refers to a row in another table.

The idea

Foreign keys enforce that a referenced row exists, keeping relationships consistent.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

BrowserApp serverDatabase
  • client
  • gateway / proxy
  • service
  • cache
  • data store
  • queue / stream

Presentation tier: the browser shows the interface.

step:
1/4
at:
Browser
1/9

The green dot is a request moving through the system.

Step by step

  1. 1Add a column holding the other table's key
  2. 2The database checks it exists
  3. 3Deleting or updating follows a rule
  4. 4Joins use it

Where it is used

Orders referencing customers.

Watch out for

Large sharded systems often drop them for speed and enforce in code.

Related