๐ท Part 9: Relationships in SQL vs Referencing in NoSQL
This is a key concept that helps readers understand how to link data across tables (SQL) or documents (NoSQL), which is essential for real-world applications like e-commerce, school systems, or social networks.
๐ Introduction
Most databases store related data: customers and orders, students and classes, or products and reviews.
-
In SQL, we use foreign keys and JOINs to link tables.
-
In NoSQL (like MongoDB), we use manual referencing or embedding to connect documents.
Understanding these relationship methods is essential for building scalable and maintainable database designs.
๐ธ 1. Relationships in SQL (Using Joins)
SQL supports:
-
One-to-One
-
One-to-Many
-
Many-to-Many
…through foreign keys and JOINs.
✅ Example: Students and Classes (One-to-Many)
๐ Tables:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Classes (
ClassID INT PRIMARY KEY,
StudentID INT,
Subject VARCHAR(50),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);
๐ Fetching Related Data:
SELECT Students.Name, Classes.Subject
FROM Students
JOIN Classes ON Students.StudentID = Classes.StudentID;
➡️ This JOIN connects each student to their class records.
๐น 2. Relationships in MongoDB (Referencing)
In NoSQL, there are two ways to handle related data:
✅ A. Manual Referencing (Normalized approach)
// students collection
{
_id: ObjectId("stu101"),
name: "Aisha Khan"
}
// classes collection
{
student_id: ObjectId("stu101"),
subject: "Math"
}
To retrieve, you need two queries:
const student = db.students.findOne({ name: "Aisha Khan" });
const classes = db.classes.find({ student_id: student._id });
✅ B. Embedding (Denormalized approach)
{
name: "Aisha Khan",
classes: [
{ subject: "Math" },
{ subject: "Science" }
]
}
➡️ This is faster for reads, but harder to update if the same class is shared across students.
๐ Comparison Table: SQL Joins vs NoSQL Referencing
Feature | SQL (Joins) | MongoDB (Referencing/Embedding) |
---|---|---|
Relationship type | Foreign keys & JOINs | Manual references or embedded documents |
Querying | Single JOIN query | Multiple queries or nested documents |
Read performance | May be slower due to joins | Fast if embedded |
Update complexity | Centralized & simple | More complex with embedded structures |
Data duplication | Minimal | Possible with embedding |
๐ง Best Practices
✅ Use SQL when:
-
Data relationships are complex and frequently queried together
-
You want strong referential integrity
✅ Use NoSQL referencing when:
-
You need flexibility or horizontal scaling
-
Data isn’t always accessed together
๐ Quick Summary
- SQL uses foreign keys and JOINs for linking tables.
- NoSQL uses manual referencing or embedding to relate documents.
- Joins are powerful but may affect performance in large datasets.
- Embedding is fast but risks data duplication and update complexity.
✅ What’s Next?
In Part 10, we’ll dive into Transactions and Consistency — how SQL ensures ACID compliance, and how NoSQL handles consistency across distributed systems.
- Click Next for:
practice assignment comparing SQL JOINs and MongoDB referencing