Using Foreign Keys to Build Relationships in SQL | HiringMine
Community · 4 min read Using Foreign Keys to Build Relationships in SQL So far, we’ve learned about Primary Keys and Foreign Keys. Now it’s time to understand how these two things actually help us build relationships inside a real database
MS Muhammad Sufiyan Software Engineer · 5d ago
Backend Engineering Hub T-
So far, we’ve learned about Primary Keys and Foreign Keys .
Now it’s time to understand how these two things actually help us build relationships inside a real database.
The big idea is very simple:
All relationships in SQL are created using foreign keys. Whether it’s:
One-to-Many Many-to-One One-to-One Many-to-Many 👉 A foreign key is always involved.
Why Foreign Keys Matter Databases don’t “understand” relationships automatically.
They don’t know:
which photo belongs to which user which comment belongs to which photo which user wrote which comment We must explicitly store this information , and we do that using foreign keys .
Example: Comments, Users, and Photos Let’s continue with our photo-sharing app example.
Real-World Rules From the app’s behavior, we already know:
A comment has exactly one user A comment belongs to exactly one photo A user can have many comments A photo can have many comments So we have two one-to-many relationships :
Relationship
One Side
Many Side
Users → Comments
User
Comments
Photos → Comments
Photo
Comments
Golden Rule for Foreign Keys The “many” side of a relationship ALWAYS gets the foreign key.
Designing the Tables
Users Table users
-----
id (PK)
username
id is the primary key Users do NOT belong to anything else So ❌ no foreign key needed here
Photos Table photos
------
id (PK)
url
user_id (FK)
Each photo belongs to one user A user can have many photos So the photos table gets user_id
Comments Table comments
--------
id (PK)
content
user_id (FK)
photo_id (FK)
Comments are interesting because:
A comment belongs to one user A comment belongs to one photo But users and photos both have many comments 👉 That’s why comments has TWO foreign keys :
Reading a Comment Row (Very Important) Let’s say we have this row in comments:
id: 5
content: "Nice shot!"
user_id: 4
photo_id: 3
Go to users table Find user with id = 4 ✅ That user wrote the comment Go to photos table Find photo with id = 3 ✅ That’s the photo the comment belongs to That’s how relationships work in SQL.
Why This Feels Confusing at First “This can’t be how real databases work…” But yes — this is exactly how it’s done .
✔ Once you learn it once, it works everywhere
Primary Key vs Foreign Key (Quick Comparison)
Primary Key Identifies one unique row Always unique Usually named id Never changes Integer or UUID Every table must have one
Foreign Key References a primary key in another table Can have duplicate values Exists only when a table “belongs to” something else Usually named like: user_idphoto_idCan change if the relationship changes
Naming Convention We’ll Follow user_id → points to users.idphoto_id → points to photos.idThis keeps schemas clear and readable .
One Last Important Rule ✅ Primary keys never change ✅ Foreign keys CAN change Changing a foreign key means:
“This record now belongs to a different parent.”
Final Takeaway Relationships = Foreign Keys The many side gets the foreign key Primary keys identify Foreign keys connect HIRINGMINE CAREER SIGNAL This writing is proof of expertise. Explore the author’s verified skills, projects and availability—or start a professional conversation.
View career profile Hire this authorMS WRITTEN BY Muhammad Sufiyan Software Engineer. Writing about practical work and career growth.
View profile