Chapter 7 of 10All chapters
Chapter 7 of 10
Keys and relationships
How tables point at each other.
Primary and foreign
A primary key identifies each row uniquely and never repeats. A foreign key is a column holding another table's primary key, which is what makes a join meaningful.
- Foreign key constraints stop a row pointing at something that does not exist.
- One to many is the common shape; many to many needs a join table between them.
Choosing a key
A generated id is usually safer than a natural key such as an email address, because natural values change and people expect to be able to change them.