SQL Database Relationships

Understanding the Problem

We need to create a database structure for a music organization system that involves:

Real-world analogy: Think of a professional orchestra. One orchestra (band) can have many musicians, but each musician typically belongs to one primary orchestra. However, each musician can play multiple instruments, and each instrument can be played by multiple musicians.

Devising a Plan

  1. Create the bands table (already provided)
  2. Modify musicians table to include band relationship
  3. Create instruments table (already provided)
  4. Create join table for musicians and instruments
  5. Add foreign key constraints

Solution Implementation

Basic Solution

-- Enable foreign key constraints
PRAGMA foreign_keys = 1;

-- Drop tables if they exist (in correct order to avoid constraint violations)
DROP TABLE IF EXISTS musician_instruments;
DROP TABLE IF EXISTS instruments;
DROP TABLE IF EXISTS musicians;
DROP TABLE IF EXISTS bands;

-- Create bands table
CREATE TABLE bands (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name VARCHAR(100)
);

-- Create musicians table with band foreign key
CREATE TABLE musicians (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100),
    band_id INTEGER,
    FOREIGN KEY (band_id) REFERENCES bands(id)
);

-- Create instruments table
CREATE TABLE instruments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    type VARCHAR(100) NOT NULL
);

-- Create join table for many-to-many relationship
CREATE TABLE musician_instruments (
    musician_id INTEGER,
    instrument_id INTEGER,
    FOREIGN KEY (musician_id) REFERENCES musicians(id),
    FOREIGN KEY (instrument_id) REFERENCES instruments(id),
    PRIMARY KEY (musician_id, instrument_id)
);

Code Explanation

One-to-Many Relationship (Bands to Musicians)

In the musicians table, we added:

Many-to-Many Relationship (Musicians to Instruments)

We created a join table called musician_instruments with:

Example Usage

-- Insert sample data
INSERT INTO bands (name) VALUES ('Jazz Ensemble');
INSERT INTO musicians (first_name, last_name, band_id) 
    VALUES ('John', 'Coltrane', 1);
INSERT INTO instruments (type) VALUES ('Saxophone');
INSERT INTO musician_instruments (musician_id, instrument_id)
    VALUES (1, 1);

Verification and Testing

To verify our solution works:

Additional Concepts

Alternative Approaches

Real-World Applications

This database structure is similar to many real-world scenarios: