DataBase Design Lesson 8 – Introduction to Relationships | Dataplexa
Database Design · Lesson 8

Introduction to Relationships

Discover how tables connect to each other through relationships, what makes them different from keys, and how to spot them in the BookVault database schema.

What Database Relationships Actually Are

A database relationship defines how rows in one table connect to rows in another table. Think of it like friendship connections on Facebook. Each person is a row. The friendship is the relationship.

But relationships aren't the same as keys. Keys are the mechanism. Relationships are the actual business connections between your data entities. When a customer places an order, that's a relationship. When a book belongs to a category, that's another relationship.

1
2
3
Identify Business Rules
Map Table Connections
Implement with Foreign Keys

Spotify has relationships between users and playlists, playlists and songs, artists and albums. Uber connects drivers to rides, rides to passengers, passengers to payment methods. Every business has these logical connections.

The Three Types of Relationships

Database relationships come in exactly three flavors. Each one handles a different pattern of how your business entities connect to each other.

One-to-Many: The Most Common Relationship

One customer can place many orders. One category can contain many books. One author can write many books. This is the bread and butter of database design. About 80% of your relationships will be one-to-many.

Here's what this looks like in BookVault. A single customer (like Sarah Johnson, customer_id = 1) can place order #101, order #102, and order #103. But each order belongs to exactly one customer.

-- One-to-Many: Customer to Orders
-- Each order references exactly one customer
SELECT 
    c.first_name,
    c.last_name,
    COUNT(o.order_id) as total_orders
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE c.customer_id = 1
GROUP BY c.customer_id, c.first_name, c.last_name;
first_name  | last_name | total_orders
------------|-----------|-------------
Sarah       | Johnson   | 3

What just happened?

We joined customers to orders using the foreign key customer_id. The COUNT shows Sarah has 3 orders. Try this: change the WHERE clause to see different customers' order counts.

One-to-One: The Rare Relationship

Each customer has exactly one profile. Each book has exactly one detailed description record. One-to-one relationships are rare because you usually just put all the data in the same table.

But sometimes you split them for performance. Netflix might separate user accounts from viewing preferences. The account data gets accessed constantly. The detailed preference data only loads when needed.

Many-to-Many: The Complex Relationship

Many books can be in many orders. Many students can enroll in many classes. Many actors can appear in many movies. This is where database design gets interesting.

You cannot implement many-to-many directly. You need a bridge table (also called junction table or linking table) that sits between the two main tables.

-- Many-to-Many: Books to Orders (via order_items)
-- order_items is the bridge table
SELECT 
    b.title,
    COUNT(DISTINCT oi.order_id) as times_ordered
FROM books b
INNER JOIN order_items oi ON b.book_id = oi.book_id
GROUP BY b.book_id, b.title
ORDER BY times_ordered DESC
LIMIT 5;
title                    | times_ordered
------------------------|---------------
The Great Gatsby         | 12
1984                     | 11
To Kill a Mockingbird    | 9
Pride and Prejudice      | 8
The Catcher in the Rye   | 7

What just happened?

The order_items table connects books to orders. Each row represents one book appearing in one order. We counted distinct order_ids to see which books get ordered most frequently.

Cardinality and Participation

Cardinality describes the numerical relationship between tables. We just covered that. But participation describes whether the relationship is required or optional.

Mandatory Participation

Every order MUST have a customer. No orphaned orders allowed.

Optional Participation

A customer can exist without any orders. New customers haven't ordered yet.

Think about Amazon. Every purchase must have a buyer (mandatory). But users can create accounts and never buy anything (optional participation from the user side).

How Participation Affects Your Database

Mandatory participation becomes a NOT NULL constraint on your foreign key. Optional participation allows NULL values.

-- Mandatory: Every order needs a customer
CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT NOT NULL,  -- Mandatory participation
    order_date DATE NOT NULL,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

-- Optional: Books don't need a category immediately
CREATE TABLE books (
    book_id INT PRIMARY KEY,
    title VARCHAR(200) NOT NULL,
    category_id INT,  -- Optional participation (can be NULL)
    FOREIGN KEY (category_id) REFERENCES categories(category_id)
);

Common Mistake: Making Everything Optional

New designers often allow NULL on every foreign key "just in case." This creates dirty data. If every order MUST have a customer in your business, enforce it with NOT NULL.

Relationships vs Foreign Keys

Here's where people get confused. Relationships are conceptual. Foreign keys are the physical implementation.

Relationship (Conceptual)

"Customers place orders"

"Books belong to categories"

"Orders contain books"

Foreign Key (Physical)

customer_id in orders table

category_id in books table

order_items bridge table

When you design a database, you start with relationships. "What business entities need to connect?" Then you implement those relationships using primary keys, foreign keys, and bridge tables.

The BookVault Relationship Map

Here are the core relationships in our bookstore database:

Relationship Type Implementation
Customer → Orders One-to-Many customer_id FK in orders
Author → Books One-to-Many author_id FK in books
Category → Books One-to-Many category_id FK in books
Orders ↔ Books Many-to-Many order_items bridge table

Data Insight

BookVault processes 50,000+ orders monthly. The order_items bridge table contains 200,000+ rows connecting books to orders. Without proper relationships, finding "books ordered together" would require scanning millions of records.

Why Relationships Matter for Business Logic

Relationships aren't just technical concepts. They enforce business rules at the database level. This prevents bad data from entering your system.

When Stripe processes a payment, the relationship between customers and payment methods ensures every charge has a valid customer. When Airbnb shows available properties, relationships between locations and listings prevent showing deleted properties.

-- This query fails if relationships aren't properly defined
-- Trying to create an order for non-existent customer
INSERT INTO orders (order_id, customer_id, order_date, status, total_amount)
VALUES (9999, 99999, '2024-01-15', 'pending', 49.99);

-- Database should reject this with foreign key constraint error

Good relationship design means your database automatically prevents impossible data scenarios. No orders without customers. No books with invalid categories. No payments without accounts.

Real-World Relationship Patterns

Every industry has common relationship patterns. Social media: users follow users (many-to-many). E-commerce: products have reviews (one-to-many). Healthcare: patients have appointments (one-to-many), doctors treat patients (many-to-many).

Understanding these patterns helps you design faster. Most business problems map to relationships you've seen before.

Next Steps: From Relationships to ER Diagrams

Now you understand what relationships are and why they matter. But how do you document them? How do you communicate your database design to other developers?

That's where Entity-Relationship diagrams come in. Visual representations of your tables and their relationships. We'll cover the conceptual design process, then move into logical and physical design phases.

But first, you need to master the difference between designing for understanding (conceptual), designing for computers (logical), and designing for performance (physical).

Quiz

1. In the BookVault database, what type of relationship exists between authors and books?


2. How is the many-to-many relationship between orders and books implemented in BookVault?


3. In BookVault, the orders table has a customer_id foreign key. What does mandatory participation mean for this relationship?


Up Next

Conceptual vs Logical vs Physical Design

Learn the three phases of database design and how relationships evolve from business concepts into working database schemas.