๐งช Practice Exercise: Normalization vs Denormalization
๐ฏ Objective:
Understand how to design data using normalization (SQL-style) and denormalization (NoSQL-style).
๐น Scenario:
You are managing a simple Library Database with Books and Authors.
๐ Task A: Normalize the Data (SQL style)
-
Create tables for
Books
andAuthors
. -
Each book should store a reference (
AuthorID
) to the author. -
Insert sample data for 2 authors and 3 books (some books by the same author).
-
Write an SQL query to fetch each book’s title with its author’s name.
๐ Task B: Denormalize the Data (NoSQL style)
-
Create a MongoDB collection called
books
. -
Store author details embedded inside each book document.
-
Insert the same sample data for 2 authors and 3 books as above.
-
Write a MongoDB query to find all books by a specific author’s name.
๐น Bonus Challenge
-
Consider the scenario when an author’s information (e.g., email) changes.
-
Explain briefly how this update would differ in normalized vs denormalized models.
๐ Sample Data to Use
AuthorID | AuthorName | |
---|---|---|
1 | Jane Austen | jane.austen@example.com |
2 | Mark Twain | mark.twain@example.com |
BookID | Title | AuthorID |
---|---|---|
101 | Pride and Prejudice | 1 |
102 | Emma | 1 |
103 | Adventures of Tom Sawyer | 2 |
๐ก Hint: Use a JOIN to connect books and authors in SQL.
✍️ Tried the exercise? Share your SQL or MongoDB approach in the comments below.
Next: solution for this exercise
No comments:
Post a Comment