Skip to main content
Database Design
CHAPTER 08 Beginner

Database Relationships | 1:1, 1:N, M:N Schema Design

Updated: May 16, 2026
20 min read

# CHAPTER 8

One-to-One, One-to-Many, and Many-to-Many Relationships

1. Introduction

In Chapter 6, we drew ER diagrams with Crow's Foot notation (Cardinality). In Chapter 7, we learned that Foreign Keys are the physical bridges connecting tables. But here is the ultimate architectural question: Which table gets the Foreign Key column? If you place the Foreign Key in the wrong table, you mathematically break the application. In this chapter, we will learn the strict rules for mapping Cardinalities to physical database schemas.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Architect a One-to-One (1:1) physical schema.
  • Architect a One-to-Many (1:N) physical schema.
  • Architect a Many-to-Many (M:N) physical schema.
  • Understand the structural necessity of a Junction (Pivot) Table.
  • Execute real-world relationship mapping.

3. Architecting One-to-Many (1:N)

This is the most common relationship in software engineering. *Example: One User has Many Orders. One Department has Many Employees.*

The Rule: The Foreign Key ALWAYS goes on the "Many" side. If 1 Department has Many Employees, the department_id Foreign Key goes inside the Employees table.

Why? Because a single row in the Department table cannot physically hold a list of 50 employee IDs (that violates relational rules). But 50 distinct Employee rows can easily all hold the number 1 in their department_id column!

4. Architecting One-to-One (1:1)

This relationship is rare, but useful for security (splitting public data from highly sensitive private data). *Example: One User has exactly One Tax Record.*

The Rule: The Foreign Key goes on either table, but it MUST have a UNIQUE constraint applied to it. We place user_id inside the tax_records table. By making it UNIQUE, we mathematically guarantee that no two tax records can ever point to the same user, enforcing a strict 1:1 limit.

sql
123456
CREATE TABLE tax_records (
    id INT PRIMARY KEY,
    ssn VARCHAR(11),
    user_id INT UNIQUE, -- The UNIQUE constraint makes it 1:1!
    FOREIGN KEY (user_id) REFERENCES users(id)
);

5. Architecting Many-to-Many (M:N)

This is the most complex relationship, and it causes beginners the most trouble. *Example: Students enroll in Many Classes. Classes contain Many Students.*

The Problem:

  • If you put the class_id in the Student table, the student can only take 1 class.
  • If you put the student_id in the Class table, the class can only have 1 student.

The Solution: You cannot link them directly. You MUST create a third, completely new table in the middle. This is called a Junction Table (or Pivot Table, or Bridge Table).

6. The Junction Table (Pivot Table)

The Junction table's entire purpose is to hold two Foreign Keys, effectively turning one M:N relationship into two simple 1:N relationships.
sql
12345678910111213
-- Table A (Students)
-- Table B (Classes)

-- Table C: The Junction Table (enrollments)
CREATE TABLE enrollments (
    student_id INT,
    class_id INT,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (class_id) REFERENCES classes(id),
    
    -- Best Practice: A Composite Primary Key!
    PRIMARY KEY (student_id, class_id)
);

*(Now, if Student #5 wants to take Class #10, you just insert a row into the Junction Table: [5, 10]. If they also want to take Class #12, you insert another row: [5, 12]. Flawless architecture!)*

7. Mini Project: School Management System

Let's blueprint a complete school system schema.
  1. 1. Teachers to Departments (1:N):
  • *Rule:* 1 Department has Many Teachers.
  • *Design:* department_id goes on the teachers table.
  1. 2. Teachers to Principals (1:1):
  • *Rule:* 1 School has 1 Principal.
  • *Design:* school_id goes on the principals table, marked as UNIQUE.
  1. 3. Teachers to Students (M:N):
  • *Rule:* Teachers teach Many Students, Students have Many Teachers.
  • *Design:* Create a Junction Table called teacher_student_assignments containing teacher_id and student_id.

8. Common Mistakes

  • Placing Arrays in Columns: A beginner dealing with a Many-to-Many relationship (like a Blog Post having many Tags) might try to solve it by putting a tags column in the posts table and inserting "[tech, coding, sql]". This destroys the relational database. You can never JOIN or search that data efficiently. You MUST use a post_tag_pivot Junction Table.

9. Best Practices

  • Naming Junction Tables: If the Junction table has no specific real-world name (like enrollments), industry standard is to name it by combining the two parent tables in alphabetical order: e.g., class_student or article_tag.

10. Exercises

  1. 1. In a One-to-Many relationship between Authors and Books, which table physically contains the Foreign Key column?
  1. 2. What specific SQL constraint must be added to a Foreign Key to enforce a strict One-to-One relationship?

11. Database Design Challenges

A Music Application has Playlists and Songs. A Playlist contains many Songs. A Song can be added to many Playlists. Design the specific SQL table structure required to resolve this relationship, including the names of the tables and the precise locations of the Foreign Keys. *(Answer: It is an M:N relationship requiring a Junction Table. Tables: playlists, songs, and a junction table playlist_song. The junction table will contain playlist_id (FK) and song_id (FK)).*

12. MCQ Quiz with Answers

Question 1

When mapping a strict One-to-Many (1:N) relationship from a conceptual ER Diagram to a physical SQL schema (e.g., One Company employs Many Workers), what is the inviolable rule regarding Foreign Key placement?

Question 2

Why is a Junction Table (Pivot Table) an absolute mathematical necessity when resolving a Many-to-Many (M:N) relationship in a Relational Database?

13. Interview Questions

  • Q: Explain the structural mechanics of resolving a Many-to-Many relationship. Why is attempting to solve it using Comma-Separated Values (CSV) in a single column considered a catastrophic anti-pattern?
  • Q: Walk me through the exact DDL schema design for a One-to-One relationship between a User table and a Driver_License table. How do you guarantee mathematically that a user cannot have two licenses?

14. FAQs

Q: Can a Junction Table have its own extra columns? A: Absolutely! This is highly common. In an order_items junction table (connecting Orders and Products), you almost always add extra columns like quantity and price_at_checkout.

15. Summary

You now possess the architectural rules for relationship mapping. By knowing exactly where to place Foreign Keys on the "Many" side, enforcing UNIQUE constraints for isolation, and constructing Junction Tables for complex networks, you can flawlessly translate any business requirement into a physical SQL schema.

16. Next Chapter Recommendation

We know how to map data, but how do we know if our tables are designed *efficiently*? Is there a mathematical formula to prove our tables are structurally sound? Yes. In Chapter 9: Database Normalization Basics, we will introduce the rigorous, academic science of Data Normalization.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·