SQL JOIN Tables Long Practice

A Step-by-Step Guide to Database Relationships and Queries

Step 1: Understanding the Problem

The Cat Care Database

Imagine you're building a database for a cat care center. This is similar to how a daycare center might track children and their parents, but with cats, owners, and toys. We need to model real-world relationships where:

• A cat can have multiple owners (like a child with co-parents)

• An owner can have multiple cats (like a parent with multiple children)

• A cat can have many toys (like a child with many belongings)

• Each toy belongs to just one cat (like a personal item belonging to one child)

Real-World Analogy

Think of this like a preschool's record-keeping system:

• Children (Cats) - Each child has a name and age

• Parents (Owners) - Each parent has a first and last name

• Personal Items (Toys) - Each item belongs to one specific child

• Enrollment Records (Cat_Owners) - Records which children belong to which parents

Step 2: Devising a Plan

1. Design database schema with proper relationships

2. Create tables with appropriate constraints

3. Implement data manipulation operations

4. Write complex queries to retrieve related data

5. Add cascade deletion behavior

6. Implement data updates

Step 3: Carrying Out the Plan

Database Schema Design

-- Create the owners table
CREATE TABLE owners (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    first_name TEXT,
    last_name TEXT
);

-- Create the cats table
CREATE TABLE cats (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    birth_year INTEGER
);

-- Create the junction table for many-to-many relationship
CREATE TABLE cat_owners (
    cat_id INTEGER,
    owner_id INTEGER,
    FOREIGN KEY (cat_id) REFERENCES cats(id) ON DELETE CASCADE,
    FOREIGN KEY (owner_id) REFERENCES owners(id) ON DELETE CASCADE
);

-- Create the toys table with one-to-many relationship
CREATE TABLE toys (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    cat_id INTEGER,
    FOREIGN KEY (cat_id) REFERENCES cats(id) ON DELETE CASCADE
);

Let's understand each table's purpose:

• owners - Stores information about pet owners, like a parent directory

• cats - Contains cat details, similar to a student roster

• cat_owners - Links cats to owners, like class enrollment records

• toys - Records toys and their ownership, like personal belongings inventory

Complex Queries

-- Find Hermione's cats
SELECT DISTINCT cats.name
FROM cats
JOIN cat_owners ON cats.id = cat_owners.cat_id
JOIN owners ON cat_owners.owner_id = owners.id
WHERE owners.first_name = 'Hermione';

-- Find toys belonging to Hermione's cats
SELECT DISTINCT toys.name
FROM toys
JOIN cats ON toys.cat_id = cats.id
JOIN cat_owners ON cats.id = cat_owners.cat_id
JOIN owners ON cat_owners.owner_id = owners.id
WHERE owners.first_name = 'Hermione';

These queries demonstrate how to:

• Navigate through multiple related tables

• Use JOIN operations to connect related data

• Filter results based on specific criteria

• Avoid duplicate results using DISTINCT

Advanced Queries

-- Find cats born after 2015
SELECT DISTINCT owners.first_name, owners.last_name
FROM owners
JOIN cat_owners ON owners.id = cat_owners.owner_id
JOIN cats ON cat_owners.cat_id = cats.id
WHERE cats.birth_year > 2015;

-- Find cats owned by specific owners
SELECT DISTINCT cats.name
FROM cats
JOIN cat_owners ON cats.id = cat_owners.cat_id
JOIN owners ON cat_owners.owner_id = owners.id
WHERE owners.first_name = 'George' AND owners.last_name = 'Beatty'
   OR owners.first_name = 'Melynda' AND owners.last_name = 'Abshire';

Step 4: Looking Back and Reflecting

Verifying Our Implementation

For each query, we should verify:

• All relationships are properly maintained

• Cascade deletions work as expected

• Queries return correct and complete results

• No unexpected duplicates appear in results

Possible Enhancements

We could improve the database by:

• Adding additional constraints (NOT NULL, UNIQUE)

• Including more metadata (created_at, updated_at)

• Adding indexes for frequently queried columns

• Implementing data validation triggers

Additional Practice

Try These Queries

• Find all cats that have more than one owner

• List owners who have cats born in different years

• Find the owner with the most toys across all their cats

• List cats who share toys with the same name