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
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
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
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:
โจ 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:
- Library โ Books (one library has many books)
- Employee โ Desk (one employee has one assigned desk)
- Movies โ Actors (movies have many actors; actors appear in many movies)
- Department โ Manager (one department has one manager)
- Products โ Categories (products belong to many categories; categories contain many products)
Show Answers
- One-to-Many (one library โ many books)
- One-to-One (one employee โ one desk)
- Many-to-Many (needs bridge table)
- One-to-One (one department โ one manager)
- 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:
- How many tables do you need?
- What are the primary keys?
- What are the foreign keys?
- What type of relationship is Members โ Books?
Show Answers
- 3 tables: Books, Members, Checkouts
- Primary keys:
- Books: ISBN
- Members: Member ID
- Checkouts: Checkout ID (or composite key of Member ID + ISBN)
- Foreign keys in Checkouts table:
- Member ID (refers to Members)
- ISBN (refers to Books)
- 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?