πŸͺ TechCookies
HomeDSASystem DesignMy Progress
Free
Log inStart free
TechCookies β€” Practice Β· Learn Β· PrepareTechCookies β€” Practice Β· Learn Β· Prepare
ConceptsPracticeSD challengesPricingPrivacyTermsContact
Β© 2026 TechCookies
πŸ“šDatabase Relationships and Foreign KeysFree
8 sections
~27 min total
30 quick quizzes
3 SD challenges linked
0 of 8 doneΒ·~28 min left
Conceptsβ€ΊDatabase Relationships and Foreign Keysβ€ΊWhat Is a Relationship in a Database?
0 / 8
0%
8 sections~27 min
1
What Is a Relationship in a Database?
Relationships link rows across tables using foreign keys to eliminate data duplication
ReadQuiz
~4 min
β‹―
One-to-Many Relationships
One row in a parent table connects to many rows in a child table via foreign key
ReadQuizCode
~4 min
β‹―
Many-to-Many Relationships
Multiple rows from one table relate to multiple rows in another table
ReadQuizCode
~4 min
β‹―
Junction Tables for Many-to-Many
An intermediary table with two foreign keys represents each many-to-many connection
ReadQuizCode
~4 min
β‹―
Foreign Key Constraints and Referential Integrity
Database enforces that foreign key values must reference existing primary key rows
ReadQuizCode
~4 min
β‹―
CASCADE DELETE vs RESTRICT vs SET NULL
ON DELETE clauses control what happens to child rows when a parent is deleted
ReadQuizCode
~4 min
β‹―
System Design: Music Streaming App
Real-world application of relationships and constraints in a complete streaming database schema
ReadQuizCode
~4 min
β‹―
Practice test
30 questions
~10 min
Section 1 of 8ReadQuick quiz
What Is a Relationship in a Database?
Relationships link rows across tables using foreign keys to eliminate data duplication
~4 min read
3 quick quizzes

A relationship describes how rows in one table connect to rows in another table. Instead of duplicating data everywhere, we split information into separate tables and then link them together using special columns called foreign keys.

Why not just put everything in one table?

Imagine storing every song along with the full artist name, artist country, and artist bio repeated for every single song. If the artist changes their name, you'd need to update thousands of rows. Relationships solve this by storing the artist once and referencing them by ID.

BAD (flat table β€” data duplication): β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ song_id β”‚ song_title β”‚ artist_name β”‚ artist_country β”‚ bio β”‚ β”‚ 1 β”‚ Blinding Lightsβ”‚ The Weeknd β”‚ Canada β”‚ ... β”‚ β”‚ 2 β”‚ Starboy β”‚ The Weeknd β”‚ Canada β”‚ ... β”‚ β”‚ 3 β”‚ Shape of You β”‚ Ed Sheeran β”‚ UK β”‚ ... β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

GOOD (normalized β€” linked tables): artists table: songs table: β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ id β”‚ name β”‚ β”‚ id β”‚ title β”‚ artist_id β”‚ β”‚ 1 β”‚ The Weeknd β”‚ β”‚ 1 β”‚ Blinding Lights β”‚ 1 β”‚ β”‚ 2 β”‚ Ed Sheeran β”‚ β”‚ 2 β”‚ Starboy β”‚ 1 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ 3 β”‚ Shape of You β”‚ 2 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


β˜‘ Quick check 1/3
Why is storing artist information (name, country, bio) directly in the songs table considered bad practice?
AIt causes data duplication and makes updates error-prone
BIt prevents sorting songs alphabetically
CIt requires more memory for each song row
DIt makes it impossible to query individual songs
Answer the quiz to continue
Notes
πŸ”
Loading…