Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Explain the difference between nosql and relational DB

Question

table to organize data into rows and columns
schema-less and document-oriented, key-value, wide-column

While a variety of differences exist between relational database management systems (RDBMS) and NoSQL databases, one of the key differences is the way the data is modeled in the database. In this section, we'll work through an example of modeling the same data in a relational database and a NoSQL database. Then, we'll highlight some of the other key differences between relational databases and NoSQL databases.

RDBMS vs NoSQL: Data Modeling Example
Let's consider an example of storing information about a user and their hobbies. We need to store a user's first name, last name, cell phone number, city, and hobbies.

In a relational database, we'd likely create two tables: one for Users and one for Hobbies.

Image description

Image description

In order to retrieve all of the information about a user and their hobbies, information from the Users table and Hobbies table will need to be joined together.

The data model we design for a NoSQL database will depend on the type of NoSQL database we choose. Let's consider how to store the same information about a user and their hobbies in a
document database

{
   "_id": 1,
   "first_name": "Leslie",
   "last_name": "Yepp",
   "cell": "8125552344",
   "city": "Pawnee",
   "hobbies": ["scrapbooking", "eating waffles", "working"]
}
Enter fullscreen mode Exit fullscreen mode

While the example above highlights the differences in data models between relational databases and NoSQL databases, many other important differences exist, including:

  1. Flexibility of the schema
  2. Scaling technique
  3. Support for transactions Reliance on data to object mapping To learn more about the differences between relational databases and NoSQL databases, A relational database is a type of database that uses a structure called a table to organize data into rows and columns. It follows the principles of the relational model, where data is stored in tables, and relationships between tables are established using keys. Key Characteristics:

Tables: Data is organized into tables, each with predefined columns and data types.
Schema: The database schema defines the structure of tables, including relationships, constraints, and keys.
ACID Properties: Transactions in relational databases adhere to ACID properties (Atomicity, Consistency, Isolation, Durability).
SQL (Structured Query Language): SQL is used to query and manipulate data in relational databases.
Example:
Consider a simple relational database for a library:

-- Table: Books
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    Title VARCHAR(255),
    Author VARCHAR(255),
    PublishedYear INT
);

-- Table: Users
CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    UserName VARCHAR(255),
    Email VARCHAR(255)
);

-- Table: Borrowings
CREATE TABLE Borrowings (
    BorrowingID INT PRIMARY KEY,
    BookID INT,
    UserID INT,
    BorrowDate DATE,
    ReturnDate DATE,
    FOREIGN KEY (BookID) REFERENCES Books(BookID),
    FOREIGN KEY (UserID) REFERENCES Users(UserID)
);
Enter fullscreen mode Exit fullscreen mode

In this example, we have three tables (Books, Users, Borrowings) with relationships defined between them.

NoSQL Database:

Definition:

NoSQL (Not Only SQL) databases are a category of databases that do not use the traditional tabular relational database model.
They are designed to h*andle unstructured, semi-structured, or structured data* and are often used for large-scale distributed systems and real-time applications.
Key Characteristics:

Schema-less: NoSQL databases are typically schema-less, allowing flexibility in the structure of the data.
Horizontal Scalability: They are designed for horizontal scalability, making them suitable for distributed and large-scale environments.
Various Data Models: NoSQL databases support various data models, including document-oriented, key-value, wide-column, and graph databases.
BASE Properties: NoSQL databases often follow the BASE model (Basically Available, Soft state, Eventually consistent) rather than the strict ACID properties.
Example:
Consider a simple document-oriented NoSQL database for a blogging platform:

// Collection: Posts
{
    "_id": ObjectId("5f8b631b4d55a65136b03b02"),
    "title": "Introduction to NoSQL",
    "content": "NoSQL databases provide flexibility in data modeling...",
    "author": "John Doe",
    "tags": ["NoSQL", "Database"],
    "comments": [
        {
            "username": "Alice",
            "comment": "Great article!"
        },
        {
            "username": "Bob",
            "comment": "I have a question about MongoDB."
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have a document in a NoSQL collection (equivalent to a table in a relational database) storing information about a blog post. The document is flexible and can include nested structures, such as an array of comments.

Top comments (0)