Chapter 6

Working with Multiple Tables

Understand how tables connect and relate to each other to form powerful database systems.

๐Ÿ”— The Power of Connected Data

In Chapter 5, you learned about individual tables. But in the real world, data is rarely stored in just one table. Instead, it's organized across multiple related tables that work together.

๐ŸŽฏ Real-Life Analogy: School Filing System

Imagine a school's filing cabinets:

  • Cabinet 1: Student information (ID, name, address)
  • Cabinet 2: Class enrollments (which student is in which class)
  • Cabinet 3: Course information (course name, teacher, room number)

These cabinets are separate, but they're connected by student IDs and course IDs. You can start with a student and find all their classes, or start with a class and find all enrolled students.

That's exactly how database tables work!

๐Ÿ’ก What You'll Learn

By the end of this chapter, you'll be able to:

  • Understand why data is split across multiple tables
  • Recognize how tables connect through common fields
  • Identify different types of relationships (one-to-one, one-to-many, many-to-many)
  • Read and follow connections between related tables
  • Appreciate the benefits of proper database structure

๐Ÿค” Why Not Just Use One Big Table?

Great question! Let's see what happens when we try to put everything in one table.

โŒ One Giant Table (Bad Approach)

Student ID Student Name Course Name Teacher Room
101 Sarah Math Mr. Smith Room 201
101 Sarah Science Dr. Jones Room 305
101 Sarah English Ms. Lee Room 102
102 Mike Math Mr. Smith Room 201

Problems:

  • Sarah's name is repeated 3 times (redundant!)
  • Math info is repeated for every student
  • If Sarah changes her name, we must update 3 rows
  • If Math room changes, we update many rows
  • Lots of wasted space and risk of errors

โœ… Multiple Related Tables (Good Approach)

Students Table:

Student ID Name
101 Sarah
102 Mike

Courses Table:

Course ID Course Name Teacher Room
C1 Math Mr. Smith 201
C2 Science Dr. Jones 305

Enrollments Table:

Student ID Course ID
101 C1
101 C2
102 C1

Benefits:

  • No repetitionโ€”each name appears once
  • Easy updatesโ€”change Sarah's name in one place
  • Less storage space
  • No inconsistencies or errors
  • Flexibleโ€”easy to add new students or courses

๐ŸŽฏ Key Principle: Avoid Redundancy

Each piece of information should be stored in exactly ONE place.

This principle is called normalization in database design. It prevents:

  • Data inconsistencies (Sarah's name spelled differently in different rows)
  • Update anomalies (forgetting to update all copies)
  • Wasted storage space
  • Maintenance nightmares

โšก Quick Check: Why Multiple Tables?

Test your understanding of why we use multiple tables:

1. Using multiple related tables reduces data redundancy (repetition).

2. Using one big table makes it easier to update information.

3. Normalization is the principle of avoiding redundancy by storing each piece of data in one place.

๐Ÿ”€ Types of Relationships

Tables can relate to each other in three main ways:

1๏ธโƒฃ One-to-One Relationship

Definition: One record in Table A relates to exactly one record in Table B.

Students

Student ID: 101
Name: Sarah

1:1

Student Lockers

Student ID: 101
Locker #: 205

Real-World Examples of One-to-One:

  • Person โ†’ Passport (one person has one passport)
  • Employee โ†’ Employee ID Badge
  • Student โ†’ Locker
  • Country โ†’ Capital City

2๏ธโƒฃ One-to-Many Relationship

Definition: One record in Table A can relate to many records in Table B.

Teachers

Teacher ID: T1
Name: Mr. Smith

1:Many

Students

Student 1
Student 2
Student 3
...30 students

Real-World Examples of One-to-Many:

  • Teacher โ†’ Students (one teacher teaches many students)
  • Customer โ†’ Orders (one customer places many orders)
  • Author โ†’ Books (one author writes many books)
  • Department โ†’ Employees
  • Category โ†’ Products

This is the MOST COMMON relationship type!

3๏ธโƒฃ Many-to-Many Relationship

Definition: Many records in Table A can relate to many records in Table B.

Students

Sarah
Mike
Emma

Many:Many

Courses

Math
Science
English

Important: Many-to-many relationships require a bridge table (also called junction table or associative table)!

Students

Student ID Name
101 Sarah
102 Mike

Enrollments (Bridge)

Student ID Course ID
101 C1
101 C2
102 C1

Courses

Course ID Name
C1 Math
C2 Science

Real-World Examples of Many-to-Many:

  • Students โ†” Courses (students take many courses; courses have many students)
  • Actors โ†” Movies (actors appear in many movies; movies have many actors)
  • Products โ†” Orders (products appear in many orders; orders contain many products)
  • Tags โ†” Blog Posts (posts have many tags; tags apply to many posts)

๐Ÿ”‘ Quick Reference: Relationship Types

Relationship Description Example Bridge Table?
One-to-One 1 โ†” 1 Person โ†’ Passport No
One-to-Many 1 โ†” Many Teacher โ†’ Students No
Many-to-Many Many โ†” Many Students โ†” Courses Yes (Required!)

๐ŸŽฏ Practice: Match Examples to Relationship Types

Drag each real-world example to the correct relationship type:

๐Ÿ›‚ Employee โ†’ ID Badge
๐Ÿ‘ฉโ€๐Ÿซ Teacher โ†’ Students
๐ŸŽฌ Actors โ†” Movies
๐ŸŒ Country โ†’ Capital City
๐Ÿ“š Category โ†’ Products
๐Ÿ“ Students โ†” Courses
1๏ธโƒฃ One-to-One (1:1)
Each record matches exactly one other
2๏ธโƒฃ One-to-Many (1:Many)
One record relates to many
3๏ธโƒฃ Many-to-Many (Many:Many)
Many relate to many (needs bridge table)

โœจ Benefits of Proper Structure

Why go through all this trouble of splitting data across multiple tables?

1. Avoid Repetition

Each piece of data is stored once.

Example: Sarah's name appears once in Students table, not repeated in every enrollment.

2. Easy Updates

Change data in one place, and it's updated everywhere.

Example: If a course room changes, update one row in Courses table.

3. Data Consistency

No risk of conflicting information.

Example: Can't have Sarah's name spelled differently in different places.

4. Scalability

Easy to add new records without restructuring.

Example: Add 100 new students without changing table structure.

5. Flexibility

Can query data in multiple ways.

Example: Find all courses for a student OR all students in a course.

6. Storage Efficiency

Less redundancy = less storage space.

Example: Store "Algebra II" once, not once per student.

Without Proper Structure

Scenario: Math class moves from Room 201 to Room 305

One giant table: Must update 30 rows (one per student)

Risk: Miss a row โ†’ inconsistent data

Time: Error-prone and slow

With Proper Structure

Scenario: Math class moves from Room 201 to Room 305

Multiple related tables: Update 1 row in Courses table

Risk: Noneโ€”change happens once

Time: Fast and reliable

๐ŸŽฏ Practice Exercises

Exercise 1: Identify the Relationship Type

For each scenario, identify if it's One-to-One, One-to-Many, or Many-to-Many:

  1. Library โ†’ Books (one library has many books)
  2. Employee โ†’ Desk (one employee has one assigned desk)
  3. Movies โ†’ Actors (movies have many actors; actors appear in many movies)
  4. Department โ†’ Manager (one department has one manager)
  5. Products โ†’ Categories (products belong to many categories; categories contain many products)
Show Answers
  1. One-to-Many (one library โ†’ many books)
  2. One-to-One (one employee โ†’ one desk)
  3. Many-to-Many (needs bridge table)
  4. One-to-One (one department โ†’ one manager)
  5. Many-to-Many (needs bridge table)

Exercise 2: Design Tables

Scenario: Design a database for a library system that tracks:

  • Books (title, author, ISBN)
  • Members (name, member ID, email)
  • Checkouts (which member has which book, due date)

Questions:

  1. How many tables do you need?
  2. What are the primary keys?
  3. What are the foreign keys?
  4. What type of relationship is Members โ†” Books?
Show Answers
  1. 3 tables: Books, Members, Checkouts
  2. Primary keys:
    • Books: ISBN
    • Members: Member ID
    • Checkouts: Checkout ID (or composite key of Member ID + ISBN)
  3. Foreign keys in Checkouts table:
    • Member ID (refers to Members)
    • ISBN (refers to Books)
  4. Many-to-Many: Members can check out many books; books can be checked out by many members (over time). Checkouts is the bridge table.

๐Ÿ“ Knowledge Check

1. Why is it better to use multiple related tables instead of one big table?

2. What is a foreign key?

3. In a one-to-many relationship between Teachers and Students, what does this mean?

4. What is a bridge table (junction table)?

5. Students and Courses have what type of relationship?

6. What is the main benefit of avoiding data redundancy?

7. In the school database example, which table is the bridge table?

8. To find all courses a specific student is taking, you would:

9. What is the main purpose of a primary key in a table?

10. In a one-to-one relationship between Person and Passport tables, what is true?