Types of Database-tutorial
Now that you know how a database is structured with tables, rows, and columns
let us explore the two major types of databases you will encounter in the real world:
- Relational Databases (SQL-based)
- Non-Relational Databases (NoSQL-based)
Both store data but in different ways and for different needs.
1. Relational Databases (SQL Databases)
Relational databases organize data into tables (rows and columns).
Each table stores specific information and tables are connected through relationships.
Think of it like a well-structured Excel sheet each sheet (table) has its own data, and you can link them using a common column.
Key Concepts
Table: Customers
Table: Orders
Example
Output
Example: Storing Customer Data in MongoDB (Document Format)
Difference Between SQL and NoSQL
Key Concepts
Term | Meaning |
---|---|
Table | Collection of data (like a spreadsheet) |
Row (Record) | Single entry of data |
Column (Field) | Attribute or property of data |
Primary Key | Unique ID for each record |
Foreign Key | A link between two tables |
Example: Online Store Database
Let us say you own an e-commerce store, You have two tables Customers and Orders.
Table: Customers
CustomerID | Name | City |
---|---|---|
1 | Amit Kumar | Delhi |
2 | Rohit Das | Mumbai |
Table: Orders
OrderID | CustomerID | Product | Price |
---|---|---|---|
101 | 1 | Laptop | 50,000 |
102 | 2 | Headphones | 2,000 |
Here,
- The Customers table stores all customer details.
- The Orders table stores order information.
- The CustomerID column in both tables helps us connect which customer placed which order even if we don’t know the exact SQL command yet.
Later, when you learn SQL queries, you’ll see how we can combine this data to show results together.
Foreign Key
You can simply say:
Here, the customerID in the Orders table refers to the Customer in the Customers table.
This connection between tables is called a relationship, and you will learn more about it when we study Foreign Keys later.
Example
SELECT Customers.Name, Orders.Product, Orders.PriceFROM CustomersJOIN OrdersON Customers.CustomerID = Orders.CustomerID;
Output
Name | Product | Price |
---|---|---|
Amit Kumar | Laptop | 50,000 |
Rohit Das | Headphones | 2,000 |
Examples of Relational Databases
- MySQL
- PostgreSQL
- Oracle Database
- Microsoft SQL Server
2. Non-Relational Databases (NoSQL Databases)
Not all data fits perfectly into tables.
Sometimes we have unstructured or semi-structured data like images, comments, or user preferences.
That is where Non-relational (NoSQL) databases come in.
They store data in flexible forms such as:
- Documents (JSON, XML)
- Key-Value pairs
- Graphs
- Wide Columns
There are no fixed schemas each record can look different.
Real Life Analogy
Imagine a digital diary where every page (document) may have different details.
Unlike a relational table, there is no strict rule that every page must have the same columns.
Example: Storing Customer Data in MongoDB (Document Format)
{ "CustomerID": 1, "Name": "Amit Kumar", "City": "Delhi", "Orders": [ { "Product": "Laptop", "Price": 50000 }, { "Product": "Mouse", "Price": 1200 } ]}
Here:
- Everything about Amit her details and orders is stored together in one document.
- This makes retrieval fast and flexible.
Examples of Non-Relational Databases
- MongoDB (Document-based)
- Redis (Key-Value store)
- Cassandra (Wide Column)
- Neo4j (Graph-based)
- Firebase (Cloud-based)
Difference Between SQL and NoSQL
Feature | Relational (SQL) | Non-Relational (NoSQL) |
---|---|---|
Structure | Tables (rows & columns) | Documents, key-value, graphs |
Schema | Fixed | Flexible |
Language | SQL | JSON / APIs |
Best For | Structured data, transactions | Unstructured or big data |
Examples | MySQL, PostgreSQL | MongoDB, Firebas |
Two Minute Drill
- Databases are of two main types : Relational (SQL) and Non-Relational (NoSQL).
- Relational = Tables + Keys + Relationships.
- Non-relational = Documents + Flexibility + Scalability.
- NoSQL uses nested documents or references.
- Both are essential in modern development often used together.