๐งช Practice Task – CRUD Basics in SQL & MongoDB
Here's a Practice Task + Mini Assignment that complements Part 6: CRUD Operations in SQL vs NoSQL. It's designed to reinforce the differences in syntax and logic between SQL and MongoDB, while staying simple and highly practical for beginners.
๐ฏ Objective:
Perform basic Create, Read, Update, and Delete operations in both SQL and NoSQL (MongoDB).
๐น Scenario: School Students Data
You are managing a student database and need to do the following:
-
Add a new student
-
Fetch the student's data
-
Update the student’s class
-
Delete the student’s record
๐ A. SQL Practice
1. Create
INSERT INTO Students (StudentID, Name, Class)
VALUES (2, 'Ravi Kumar', '10B');
2. Read
SELECT * FROM Students WHERE StudentID = 2;
3. Update
UPDATE Students SET Class = '10C' WHERE StudentID = 2;
4. Delete
DELETE FROM Students WHERE StudentID = 2;
๐ B. MongoDB Practice
1. Create
db.students.insertOne({
student_id: 2,
name: "Ravi Kumar",
class: "10B",
marks: [{ subject: "English", score: 82 }]
});
2. Read
db.students.findOne({ student_id: 2 });
3. Update
db.students.updateOne(
{ student_id: 2 },
{ $set: { class: "10C" } }
);
4. Delete
db.students.deleteOne({ student_id: 2 });
๐ Mini Assignment: Build Your Own CRUD Set
๐น Instructions:
-
Choose a student name and add them to both SQL and NoSQL.
-
Add a subject and score for them (embedded in NoSQL).
-
Read and display their data.
-
Change their class and update their English score to 90.
-
Finally, delete the record in both systems.
๐ก Challenge (Optional):
-
In SQL, insert marks into a separate
Marks
table and join it to fetch the student’s full profile. -
In MongoDB, use
$push
to add a new subject to themarks
array.
✅ Bonus: MongoDB $push
Example
db.students.updateOne(
{ student_id: 2 },
{ $push: { marks: { subject: "Science", score: 88 } } }
);
Do you Know: The $push operator is used to add a new element to an array field in MongoDB.
Tell Us in Comments:
Which syntax do you find easier — SQL or MongoDB? Share your thoughts in the comments!
Also, Try the assignment above and share your solution in the comments.
To know about CRUD opreations read Part 6: CRUD Operations in SQL vs NoSQL – A Beginner's Guide
✅ What’s Next?
Next: solution for the mini assignment in both SQL and NoSQL
No comments:
Post a Comment