01 / 21

Table
Relationships

Understand how database tables connect through one-to-one, one-to-many, and many-to-many relationships: the foundation of relational database design.

SQL

What Are Table Relationships?

In a relational database, data is organized into tables (also called relations). Each table stores information about one type of entity: students, courses, orders, or products. A table relationship describes how rows in one table connect to rows in another table.

Primary Keys and Foreign Keys

Primary Key (PK)
A column (or set of columns) that uniquely identifies each row in a table. Every table should have one. Example: student_id in a Students table.
Foreign Key (FK)
A column in one table that references the primary key of another table. It creates the link between tables. Example: student_id in an Enrollments table.

Why Relationships Matter

1
Eliminate redundancy: Store each fact once. Change a customer's name in one place, and every order reflects it.
2
Maintain data integrity: Foreign keys enforce that references point to real rows, preventing "orphan" records.
3
Enable powerful queries: JOIN operations combine related tables, letting you answer complex questions like "Which students enrolled in Biology?"
4
Support scalability: Well-structured relationships keep databases efficient as data grows to millions of rows.

The Three Relationship Types

One-to-One
Each row in Table A relates to exactly one row in Table B, and vice versa.
One-to-Many
One row in Table A can relate to many rows in Table B, but each row in B relates to only one in A.
Many-to-Many
Rows in Table A can relate to many rows in Table B, and rows in B can relate to many in A.

One-to-One Relationship

Users user_id (PK) name email 1 : Alice 2 : Bob 3 : Carol Profiles profile_id (PK) user_id (FK) bio 1 : user 1 2 : user 2 3 : user 3 1 : 1

One-to-Many Relationship

Customers customer_id (PK) name 1 : Alice 2 : Bob Orders order_id (PK) customer_id (FK) total 101 : cust 1 102 : cust 1 103 : cust 2 104 : cust 1 1 : Many

Many-to-Many Relationship

Students student_id (PK) name 1 : Alice 2 : Bob 3 : Carol Enrollments student_id (FK) course_id (FK) 1, 101 1, 102 2, 101 3, 102 Courses course_id (PK) title 101 : Biology 102 : History 1 : Many Many : 1

Key Distinction

One-to-One One-to-Many Many-to-Many
FK Location Either table "Many" side Junction table
FK Unique? Yes No No (composite PK)
Tables Needed 2 2 3 (includes junction)
Common Example User ↔ Profile Customer ↔ Orders Students ↔ Courses
Frequency Rare Most common Common

The Junction Table

Relational databases have no built-in "many-to-many" link. Instead, we create a third table (the junction table, also called a bridge table or linking table) that sits between the two related tables.

1
Two foreign key columns: one pointing to each parent table's primary key.
2
Composite primary key: both FK columns together form the PK, ensuring no duplicate pairings.
3
Optional extra columns: you can add attributes specific to the relationship, such as enrollment_date or grade.

How to Identify the Type

1
Name the two entities. Write them down: "Customer" and "Order."
2
Ask "How many?" in both directions. Can one customer have many orders? Can one order belong to many customers?
3
Match the pattern. If both answers are "many," it is many-to-many. If only one side is "many," it is one-to-many. If both are "one," it is one-to-one.
4
Place the foreign key. Put it on the "many" side. For M:M, create a junction table.

Example: Library System

Scenario: A library has Authors and Books. One author can write many books, but each book is written by exactly one author.

Authors author_id (PK) name Books book_id (PK) author_id (FK) title 1 : Many

Example: School Schedule

Scenario: Teachers and Classes. A teacher can teach many classes, and a class can be taught by many teachers (team-teaching, substitutes).

Teachers teacher_id (PK) name TeacherClasses teacher_id (FK) class_id (FK) Classes class_id (PK) subject Many-to-Many (via junction table)

SQL in Action

Here is how you create a one-to-many relationship between Customers and Orders in SQL:

CREATE TABLE Customers (
  customer_id INT PRIMARY KEY,
  name VARCHAR(100)
);

CREATE TABLE Orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  total DECIMAL(10,2),
  FOREIGN KEY (customer_id)
    REFERENCES Customers(customer_id)
);

Common Mistakes

✕ FK on the Wrong Side
Placing the foreign key on the "one" side instead of the "many" side. The FK always goes in the table where rows repeat the reference.
✕ Skipping the Junction Table
Trying to model many-to-many with just two tables by stuffing comma-separated IDs into one column. This breaks queries and constraints.
✕ Missing UNIQUE for 1:1
Creating a one-to-one relationship but forgetting the UNIQUE constraint on the FK. Without it, the database silently allows one-to-many.

Summary Table

Concept Key Point
Primary Key Uniquely identifies each row
Foreign Key References another table's PK
One-to-One FK with UNIQUE constraint
One-to-Many FK on the "many" side, no UNIQUE
Many-to-Many Junction table with two FKs
Junction Table Composite PK from both FKs

Question 1 of 5

A Customers table and an Orders table are related so that each customer can place many orders, but each order belongs to one customer. What type of relationship is this?

Question 2 of 5

In a one-to-one relationship, what constraint must be placed on the foreign key column?

Question 3 of 5

Students can enroll in many Courses, and each course can have many students. How should this be modeled in a relational database?

Question 4 of 5

In a one-to-many relationship between Departments and Employees, where should the foreign key be placed?

Question 5 of 5

A Person has exactly one Passport, and each passport belongs to exactly one person. A developer creates the relationship but forgets the UNIQUE constraint on the FK. What is the consequence?