Spotify Database

Overview

Three tables. Users listen to songs. Ratings and timestamps track 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: Uniquely identifies each row - no duplicates, no NULLs allowed.

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: Links to another table's PRIMARY KEY - ensures referential integrity (can't reference non-existent users or songs)


Database Schema (CREATE TABLE Statements)

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

1️⃣ 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)
);

2️⃣ 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)
);

3️⃣ 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"], "premium": true}
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