One-to-One, One-to-Many, Many-to-Many: The Three Relationship Shapes
Every link between two tables in a relational database has one of three shapes. Once you can recognise them, designing tables becomes much easier. This page explains each shape with a tiny example and the keys that implement it.
How to find the shape
Ask two questions about two things, A and B:
- Can one A have many Bs?
- Can one B have many As?
| Answers | Shape |
|---|---|
| No and no | One-to-one |
| Yes and no | One-to-many |
| Yes and yes | Many-to-many |
One-to-many: the most common
One customer can have many orders; each order belongs to one customer.
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id)
);
How it works: the "many" side, orders, holds a foreign key pointing to the "one" side. Other examples: an author and their books, a post and its comments.
Many-to-many: needs a link table
A student can take many courses, and a course has many students. Neither table can hold a single foreign key for the other, so we add a third table:
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE enrolments (
student_id INTEGER NOT NULL REFERENCES students(id),
course_id INTEGER NOT NULL REFERENCES courses(id),
PRIMARY KEY (student_id, course_id)
);
Each row of enrolments says "this student takes this course". The link table can also hold facts about the pairing, such as the date enrolled or a grade.
One-to-one: less common
Each user has at most one profile, and each profile belongs to one user.
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE);
CREATE TABLE profiles (
user_id INTEGER PRIMARY KEY REFERENCES users(id),
bio TEXT
);
How it works: the foreign key is also the primary key (or is UNIQUE), so no user can have two profiles. People use one-to-one when some columns are optional, large or rarely needed. Often the simpler choice is to keep everything in one table.
Querying each shape
| Shape | Typical query |
|---|---|
| One-to-many | JOIN orders to customers on customer_id |
| Many-to-many | JOIN students to enrolments to courses |
| One-to-one | JOIN users to profiles on user_id, often a LEFT JOIN |
Common beginner mistakes
- Storing a list in one column, like
courses = "Maths, Art", instead of a link table. - Putting the foreign key on the wrong side of a one-to-many, which forces repeated data.
- Forgetting the composite primary key on a link table, allowing duplicate pairs.
Practise
Think of a hobby, such as music or sport, and list four things in it. For each pair, ask the two questions and write down the shape. Then sketch the tables. Our picture guide to keys linking two tables shows the one-to-many case as a diagram.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.