Day 43: Introduction to NoSQL Databases
Introduction:
NoSQL databases provide a way to store and retrieve data that is modeled in means other than the tabular relations used in relational databases. They are designed for specific data models and have flexible schemas for building modern applications. NoSQL databases are widely used for big data and real-time web applications.
Key Concepts:
NoSQL: Stands for "Not Only SQL" and represents a wide variety of database technologies.
Document Store: Stores data as documents (e.g., MongoDB).
Key-Value Store: Stores data as key-value pairs (e.g., Redis).
Column Store: Stores data in columns rather than rows (e.g., Apache Cassandra).
Graph Database: Stores data in nodes and edges to represent relationships (e.g., Neo4j).
Common NoSQL Databases and Examples:
Document Store (MongoDB):
MongoDB stores data in flexible, JSON-like documents.
// Example of a document in MongoDB
{
"studentID": 1,
"firstName": "John",
"lastName": "Doe",
"age": 20,
"courses": [
{ "courseID": 101, "courseName": "Mathematics" },
{ "courseID": 102, "courseName": "Physics" }
]
}
Key-Value Store (Redis):
Redis stores data as key-value pairs.
// Example of setting and getting a key-value pair in Redis
SET student:1:firstName "John"
GET student:1:firstName
Column Store (Apache Cassandra):
Apache Cassandra stores data in columns for scalability and high availability.
-- Example of creating a table in Cassandra and inserting data
CREATE TABLE students (
studentID INT PRIMARY KEY,
firstName TEXT,
lastName TEXT,
age INT
);
INSERT INTO students (studentID, firstName, lastName, age) VALUES (1, 'John', 'Doe', 20);
Graph Database (Neo4j):
Neo4j stores data in a graph format with nodes and relationships.
// Example of creating nodes and relationships in Neo4j
CREATE (john:Student {studentID: 1, firstName: "John", lastName: "Doe", age: 20})
CREATE (math:Course {courseID: 101, courseName: "Mathematics"})
CREATE (john)-[:ENROLLED_IN]->(math)
Practice Exercise:
Document Store: Write a MongoDB document for a student named "Alice" enrolled in "Chemistry" and "Biology" courses.
Key-Value Store: Write Redis commands to set and get the age of a student with the key "student:2:age".
Column Store: Write a Cassandra query to create a table for courses and insert a course named "Chemistry".
Graph Database: Write a Neo4j query to create a student node for "Alice" and a relationship to a course named "Chemistry".
// MongoDB document for a student named "Alice"
{
"studentID": 2,
"firstName": "Alice",
"lastName": "Johnson",
"age": 22,
"courses": [
{ "courseID": 103, "courseName": "Chemistry" },
{ "courseID": 104, "courseName": "Biology" }
]
}
// Redis commands to set and get the age of a student
SET student:2:age "22"
GET student:2:age
-- Cassandra query to create a table for courses and insert a course named "Chemistry"
CREATE TABLE courses (
courseID INT PRIMARY KEY,
courseName TEXT
);
INSERT INTO courses (courseID, courseName) VALUES (103, 'Chemistry');
// Neo4j query to create a student node for "Alice" and a relationship to a course named "Chemistry"
CREATE (alice:Student {studentID: 2, firstName: "Alice", lastName: "Johnson", age: 22})
CREATE (chemistry:Course {courseID: 103, courseName: "Chemistry"})
CREATE (alice)-[:ENROLLED_IN]->(chemistry)
Important Tips:
NoSQL databases offer flexibility and scalability, making them suitable for various applications such as big data, real-time web apps, and IoT.
Each type of NoSQL database has its strengths and is suitable for different use cases. Choose the one that best fits your application's requirements.
Regularly back up your NoSQL databases and monitor performance to ensure data integrity and availability.
Understanding NoSQL databases expands your database management skills and equips you with the tools to handle modern data challenges.
0 Comments