Spotify Database

Overview

Three tables. Users, songs, and listens. Ratings and timestamps track user preferences.

Users Table (4 rows)

user_idnameemailNotes
1Mickeymickey@example.comBlue bars for user_id=1 in tables
2Minnieminnie@example.comOrange bars for user_id=2 in tables
3Daffydaffy@example.comPurple bars for user_id=3 in tables
4Plutopluto@example.comCyan bars for user_id=4 in tables

Keys: user_id (PRIMARY KEY)

PRIMARY KEY: The unique identifier for each rowβ€”no duplicates, no NULLs.

Songs Table (10 rows)

song_id title artist genre
1 Evermore Taylor Swift Pop
2 Willow Taylor Swift Pop
3 Shape of You Ed Sheeran Rock
4 Photograph Ed Sheeran Rock
5 Shivers Ed Sheeran Rock
6 Yesterday Beatles Classic
7 Yellow Submarine Beatles Classic
8 Hey Jude Beatles Classic
9 Bad Blood Taylor Swift Rock
10 DJ Mix DJ NULL

Primary Keys: song_id

Listens Table (9 rows)

listen_iduser_idsong_idratinglisten_time
1114.52024-08-30 14:35:00
2124.2NULL
3163.92024-08-29 10:15:00
4224.7NULL
5274.62024-08-28 09:20:00
6283.92024-08-27 16:45:00
7312.9NULL
8324.92024-08-26 12:30:00
936NULLNULL

Primary Keys: listen_id

Foreign Keys: Listens.user_id β†’ Users.user_id, Listens.song_id β†’ Songs.song_id

FOREIGN KEY: This links to another table's PRIMARY KEY, ensuring you can't reference users or songs that don't exist.


Database Schema (CREATE TABLE Statements)

Learn to read SQL DDL (Data Definition Language) that creates these tables:

Users Table

CREATE TABLE users (
  user_id    INT           PRIMARY KEY,     -- Unique ID for each user
  name       VARCHAR(100)  NOT NULL,        -- User's name (required)
  email      VARCHAR(255)  UNIQUE NOT NULL  -- Email (required, no duplicates)
);

Songs Table

CREATE TABLE songs (
  song_id    INT           PRIMARY KEY,     -- Unique ID for each song
  title      VARCHAR(255)  NOT NULL,        -- Song title (required)
  artist     VARCHAR(100)  NOT NULL,        -- Artist name (required)  
  genre      VARCHAR(50)                    -- Genre (optional - can be NULL)
);

Listens Table

CREATE TABLE listens (
  listen_id    INT          PRIMARY KEY,     -- Unique ID for each listen
  user_id      INT          NOT NULL,        -- Which user (required)
  song_id      INT          NOT NULL,        -- Which song (required)
  rating       DECIMAL(2,1),                 -- Rating 0.0-5.0 (optional)
  listen_time  TIMESTAMP,                    -- When played (optional)

  FOREIGN KEY (user_id) REFERENCES users(user_id),   -- Must be valid user
  FOREIGN KEY (song_id) REFERENCES songs(song_id)    -- Must be valid song
);

SQL Schema Reference Guide

Data Types

Type Meaning Example
INT Whole numbers 1, 42, 999
VARCHAR(n) Text up to n characters 'Taylor Swift' (max 100)
DECIMAL(p,s) Decimal with p total digits, s after decimal 4.5 (max 9.9)
TIMESTAMP Date and time 2024-08-30 14:35:00
Modern Types New & Popular Used For
JSONB JSON documents (PostgreSQL) {"playlists": ["workout", "chill"]}
BLOB Binary large objects Album cover images, audio files
VECTOR(n) AI embeddings [0.1, -0.3, 0.8, ...] for song similarity

Constraints

Constraint Purpose Example in Our Database
PRIMARY KEY Unique identifier for each row. Can be multiple columns user_id, song_id, listen_id
Or composite: (student_id, class_id)
UNIQUE No duplicates allowed email (no two users can share)
NOT NULL Required - must have a value name, title, artist
FOREIGN KEY References PRIMARY KEY in another table user_id β†’ users, song_id β†’ songs
(no constraint) Optional - can be NULL genre, rating, listen_time