Day 30 of #100DaysOfCode — Introduction to Database
Source: Dev.to
100 Days of Code – Why Databases Matter
Over the past few days of my 100 Days of Code challenge I have:
- Built front‑end interfaces with React
- Styled UI components
- Transitioned to back‑end development with Node.js and Express.js
Connecting the front‑end to the back‑end taught me how APIs send and receive requests and data. The next logical piece was a reliable way to store, retrieve, and manage that data – enter databases.
What Is a Database?
A database is an organized place to store data so it can be easily saved, found, and updated.
Think of it as a super‑powered spreadsheet that can:
- Handle millions of rows
- Serve multiple users simultaneously
- Model complex relationships between data
Imagine Instagram without a database – every photo, like, and comment would disappear the moment you closed the app.
Why Applications Need Databases
| Reason | What It Means |
|---|---|
| Remember things | User accounts, orders, messages |
| Share data | All users see the same information |
| Search efficiently | Find one user out of 100 million instantly |
| Stay reliable | Data survives crashes, restarts, and updates |
Without a database an app has no memory – it’s like a person with amnesia who forgets everything after sleeping.
Types of Databases
| Category | Data Model | Example(s) | Best For |
|---|---|---|---|
| Relational | Tables with rows & columns (SQL) | PostgreSQL, MySQL, SQLite | Structured data, financial systems, e‑commerce |
| Document | JSON‑like documents (schema‑less) | MongoDB, Firestore | Content, catalogs, user profiles |
| Key‑Value | Simple key → value pairs | Redis, DynamoDB | Caching, sessions, leaderboards |
| Graph | Nodes & edges (relationships) | Neo4j, Amazon Neptune | Social networks, recommendation engines, fraud detection |
| Wide‑Column | Columns stored together (instead of rows) | Cassandra, HBase | IoT data, time‑series, massive analytics |
| Search | Full‑text indexed documents | Elasticsearch, Solr | Search bars, log analysis |
| Time‑Series | Timestamp‑oriented records | InfluxDB, TimescaleDB | Metrics, stock prices, sensor data |
Quick Code Snippets
SQL (Relational)
SELECT * FROM users WHERE age > 25;
JSON (Document)
{
"name": "Ali",
"age": 25,
"hobbies": ["coding", "chess"]
}
When to Use Which Database?
| Situation | Typical Choice |
|---|---|
| Structured data with relationships | Relational (PostgreSQL) |
| Flexible / nested data | Document (MongoDB) |
| Need blazing speed / caching | Key‑Value (Redis) |
| Social graph / recommendations | Graph (Neo4j) |
| Logs, metrics, monitoring | Time‑Series (InfluxDB) |
| Search functionality | Elasticsearch |
Most real‑world apps combine 2–3 types – e.g., PostgreSQL as the primary store, Redis for caching, and Elasticsearch for search.
Relational Concepts – Relationships
A relation connects two tables through a common field (usually a primary/foreign key).
| Type | Diagram | Description |
|---|---|---|
| One‑to‑One (1:1) | User (1) ──── (1) UserProfile | Each user has exactly one profile, and each profile belongs to exactly one user. |
| One‑to‑Many (1:N) | Customer (1) ──── (N) Orders | One customer can place many orders; each order belongs to one customer. |
| Many‑to‑Many (M:N) | Students (N) ──── [Enrollments] ──── (N) Courses | A junction (link) table stores the pairs of student‑course relationships. |
SQL JOINs – Combining Data
| Join Type | What It Returns | Typical Use |
|---|---|---|
| INNER JOIN | Only rows where the join condition matches in both tables. | Most common – fetch related records. |
| LEFT (OUTER) JOIN | All rows from the left table plus matching rows from the right; non‑matches get NULL. | Keep all “primary” records even if they have no related data. |
| RIGHT (OUTER) JOIN | All rows from the right table plus matching rows from the left; non‑matches get NULL. | Mirror of LEFT JOIN. |
| FULL OUTER JOIN | All rows from both tables; NULL where there’s no match. | When you need a complete picture of both sides. |
| CROSS JOIN | Cartesian product – every row of A paired with every row of B. | Rare; useful for generating combinations. |
| SELF JOIN | A table joined to itself (usually with an alias). | Hierarchical data (e.g., employees → managers). |
Example Tables
| Customers | |
|---|---|
id | name |
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| Orders | |
|---|---|
id | customer_id |
| 1 | 1 |
| 2 | 1 |
| 3 | 99 |
Sample Queries
-- INNER JOIN (only matching rows)
SELECT *
FROM Customers
INNER JOIN Orders ON Customers.id = Orders.customer_id;
-- Returns Alice & Bob (Carol has no orders, order #3 has no matching customer)
-- LEFT JOIN (all customers, orders if any)
SELECT *
FROM Customers
LEFT JOIN Orders ON Customers.id = Orders.customer_id;
-- Returns Alice, Bob, and Carol (Carol’s order columns are NULL)
-- RIGHT JOIN (all orders, customers if any)
SELECT *
FROM Customers
RIGHT JOIN Orders ON Customers.id = Orders.customer_id;
-- Returns Alice, Bob’s orders, and the orphan order #3 (customer columns NULL)
-- FULL OUTER JOIN (everything)
SELECT *
FROM Customers
FULL OUTER JOIN Orders ON Customers.id = Orders.customer_id;
-- Returns Alice, Bob, Carol, and the orphan order #3
-- CROSS JOIN (Cartesian product)
SELECT *
FROM Customers
CROSS JOIN Orders;
-- 3 customers × 3 orders = 9 rows (use with caution)
-- SELF JOIN (employees & managers example)
SELECT e.name AS employee, m.name AS manager
FROM Employees e
JOIN Employees m ON e.manager_id = m.id;
Visual Summary of Joins
A ●───● B → INNER JOIN (intersection)
A ●───○ B → LEFT JOIN (all of A)
A ○───● B → RIGHT JOIN (all of B)
A ○───○ B → FULL JOIN (everything)
The INNER and LEFT joins are the ones you’ll use most often.
TL;DR
- Databases store data reliably and make it searchable.
- Different database types (relational, document, key‑value, graph, wide‑column, search, time‑series) solve different data problems.
- Relationships (1:1, 1:N, M:N) link tables meaningfully.
- SQL JOINs (INNER, LEFT, RIGHT, FULL, CROSS, SELF) let you combine related data efficiently.
These concepts form the backbone of modern software, enabling applications to handle complex datasets, retrieve information quickly, and maintain reliable connections among data across the system.
Thanks for reading. Feel free to reach out with questions or comments!
Feel free to share your thoughts!