Primary & Foreign
Keys
Learn how to uniquely identify rows with primary keys and connect tables with foreign keys: the foundation of relational database design.
What Is a Key?
In a relational database, a key is a column (or set of columns) used to identify and relate rows in tables. Keys enforce data integrity and make it possible to connect information across multiple tables.
The Primary Key
A primary key (PK) is a column (or combination of columns) whose values uniquely identify every row in a table. No two rows may share the same primary key value, and it can never be NULL.
The Foreign Key
A foreign key (FK) is a column in one table that references the primary key of another table. It creates a link between two tables and enforces referential integrity: you cannot insert a value that does not exist in the referenced table.
Two Fundamental Rules
Visualizing a One-to-Many Relationship
Primary Key vs Foreign Key
| Property | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Uniquely identify each row | Reference a row in another table |
| Uniqueness | Must be unique | Can repeat (many rows may reference the same PK) |
| NULL allowed? | No, never | Sometimes (if the relationship is optional) |
| Per table | Exactly one PK | Zero or many FKs |
| Enforces | Entity integrity | Referential integrity |
How to Choose a Primary Key
Defining a Foreign Key Relationship
Example 1: School Database
A school tracks students and the clubs they join. A student can join many clubs, and a club can have many students. This is a many-to-many relationship: it requires a junction table.
Example 2: E-Commerce Orders
An online store has customers, orders, and products. Each order belongs to one customer, and each order contains many products (via order_items).
Example 3: Full SQL Syntax
Here is the complete SQL to create the customers and orders tables with a foreign key constraint:
Composite Primary Keys
Sometimes no single column is unique on its own. A composite primary key uses two or more columns together. The combination must be unique, even if individual values repeat.
Natural vs Surrogate Keys
Common Mistakes
Summary at a Glance
| Concept | Key Fact | SQL Keyword |
|---|---|---|
| Primary Key | Unique + NOT NULL per table | PRIMARY KEY |
| Foreign Key | References another table's PK | FOREIGN KEY ... REFERENCES |
| Composite Key | Two+ columns forming PK together | PRIMARY KEY (a, b) |
| Surrogate Key | Auto-generated ID, no business meaning | AUTO_INCREMENT / SERIAL |
| Referential Integrity | FK value must exist as a PK | REFERENCES table(col) |
Question 1 of 5
Which property is required of a primary key column?
Question 2 of 5
In a one-to-many relationship between departments (one) and employees (many), where should the foreign key be placed?
Question 3 of 5
A student_courses table has columns student_id and course_id. The same student should not enroll in the same course twice. What is the best primary key?
Question 4 of 5
What happens if you try to insert a row into orders with a customer_id that does not exist in the customers table?
Question 5 of 5
Why is email generally a poor choice for a primary key, even though emails are unique?