Skip to content

Commit 5b3627e

Browse files
authored
Merge pull request #219 from codeharborhub/dev-1
ADD-DOCS: content created for R-DBMS
2 parents cc850b1 + db0f693 commit 5b3627e

File tree

8 files changed

+643
-0
lines changed

8 files changed

+643
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Relational Databases",
3+
"position": 7,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn how to store data in structured tables, use SQL to query information, and understand why RDBMS is the backbone of the banking and enterprise world."
7+
}
8+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
sidebar_position: 3
3+
title: "ACID Properties: The DNA of RDBMS"
4+
sidebar_label: "3. ACID Properties"
5+
description: "Learn the four essential rules that keep your database safe and reliable."
6+
---
7+
8+
In India, we use apps like **PhonePe, GPay, or Paytm** every single day. Have you ever wondered what happens if your internet cuts out exactly when you are sending ₹500 to a friend?
9+
10+
Does the money vanish? Does it get stuck in the "middle"?
11+
12+
**No.** This is thanks to **ACID Properties**. These are the 4 golden rules that every Relational Database must follow to ensure your data is 100% safe.
13+
14+
## The 4 Pillars of ACID
15+
16+
### 1. Atomicity (The "All or Nothing" Rule)
17+
18+
In a database, a "Transaction" is a series of steps. Atomicity ensures that either **every step** happens, or **none** of them do.
19+
20+
**The Real-World Example:**
21+
Imagine you are transferring ₹500 to your friend Rahul.
22+
1. Bank deducts ₹500 from your account.
23+
2. **Server Crashes!**
24+
3. Bank fails to add ₹500 to Rahul's account.
25+
26+
Because of **Atomicity**, the database will "Rollback." It sees the crash, cancels the first step, and puts the ₹500 back in your account as if nothing ever happened.
27+
28+
### 2. Consistency (The "Follow the Rules" Rule)
29+
30+
Every database has rules (Constraints). Consistency ensures that a transaction never leaves the database in a "weird" state.
31+
32+
**The Real-World Example:**
33+
If your bank has a rule that "Balance cannot be negative," and you try to send ₹1000 when you only have ₹500, the database will block the transaction immediately. The rules stay intact!
34+
35+
### 3. Isolation (The "No Cutting in Line" Rule)
36+
37+
Modern databases handle thousands of users at the same time. Isolation ensures that one person's transaction doesn't "leak" into another's.
38+
39+
**The Real-World Example:**
40+
Imagine there is only **1 last seat** left on a bus from Delhi to Mumbai.
41+
* You and another person click "Book" at the exact same microsecond.
42+
* **Isolation** makes them wait in a digital line. One person gets the seat, and the other gets a "Sold Out" message. They don't both get charged for the same seat!
43+
44+
### 4. Durability (The "Saved Forever" Rule)
45+
46+
Once the database tells you "Transaction Successful," that data is permanent.
47+
48+
**The Real-World Example:**
49+
Even if the bank's data center loses power or a server explodes 1 second after your transaction is finished, your balance will still be correct when the power comes back. The record is written to a permanent disk, not just temporary memory.
50+
51+
## Summary Table
52+
53+
| Property | Short Meaning | "Desi" Analogy |
54+
| :--- | :--- | :--- |
55+
| **A**tomicity | All or Nothing | Like a 'Paisa Vasool' deal—you get everything promised or your money back. |
56+
| **C**onsistency | Data Integrity | No "Jugaad" allowed—the rules are the rules. |
57+
| **I**solation | Separate Work | Mind your own business—don't let others' work mix with yours. |
58+
| **D**urability | Permanent Save | Written in stone—even if the world ends, the record stays. |
59+
60+
## Summary Checklist
61+
* [x] I understand that ACID keeps data safe during crashes.
62+
* [x] I know that "Atomicity" prevents partial updates.
63+
* [x] I understand why "Isolation" is needed for many users.
64+
* [x] I recognize that "Durability" means the data is permanent.
65+
66+
:::tip Career Advice
67+
Interviewers love asking about ACID. If you explain it using the "Bank Transfer" example like we did here, you will show them that you don't just memorize definitions—you actually understand how the real world works!
68+
:::
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
sidebar_position: 6
3+
title: "Database Migrations"
4+
sidebar_label: "6. Migrations"
5+
description: "Learn how to track and manage changes to your database schema using version control for data."
6+
---
7+
8+
Have you ever added a new column to a table on your laptop, but then your teammate's code crashed because *their* database didn't have that column?
9+
10+
In the old days, you had to send a `.sql` file to your friend and say, "Hey, run this command." **Database Migrations** solve this problem by automating the process.
11+
12+
## 🧐 What is a Migration?
13+
14+
A **Migration** is a small script (usually in JavaScript, Python, or SQL) that describes a specific change to your database structure.
15+
16+
* **Migration Files** are kept in your GitHub repository.
17+
* When a teammate pulls your code, they run one command, and their database is instantly updated to match yours.
18+
19+
## How Migrations Work: UP and DOWN
20+
21+
Every migration file has two main functions:
22+
23+
1. **UP:** What should happen when we apply the migration? (e.g., Create a table).
24+
2. **DOWN:** How do we "Undo" this change if we made a mistake? (e.g., Drop the table).
25+
26+
<Tabs>
27+
<TabItem value="js" label="💻 Migration Example (JavaScript)" default>
28+
29+
```javascript
30+
// Adding a 'phone_number' column to the Users table
31+
export async function up(knex) {
32+
return knex.schema.table('users', (table) => {
33+
table.string('phone_number').nullable();
34+
});
35+
}
36+
37+
// Undoing the change
38+
export async function down(knex) {
39+
return knex.schema.table('users', (table) => {
40+
table.dropColumn('phone_number');
41+
});
42+
}
43+
44+
```
45+
46+
</TabItem>
47+
<TabItem value="sql" label="🔍 Migration Example (Raw SQL)">
48+
49+
```sql
50+
-- UP: Apply the change
51+
ALTER TABLE Users ADD COLUMN phone_number VARCHAR(15);
52+
53+
-- DOWN: Undo the change
54+
ALTER TABLE Users DROP COLUMN phone_number;
55+
56+
```
57+
58+
</TabItem>
59+
</Tabs>
60+
61+
---
62+
63+
## The Migration Workflow
64+
65+
At **CodeHarborHub**, we follow this industrial workflow to keep our data in sync:
66+
67+
1. **Create:** You generate a new migration file (e.g., `20260313_add_bio_to_users.js`).
68+
2. **Write:** You define the `up` and `down` logic.
69+
3. **Run:** You execute the migration command (e.g., `npx knex migrate:latest`).
70+
4. **Commit:** You push the migration file to **GitHub**.
71+
5. **Sync:** Your teammates pull the code and run the same command. **Boom! Everyone is on the same page.**
72+
73+
```mermaid
74+
sequenceDiagram
75+
participant DevA as Developer A
76+
participant GH as GitHub (Code)
77+
participant DevB as Developer B
78+
79+
DevA->>DevA: Creates Migration Script
80+
DevA->>DevA: Runs 'Migrate' (Local DB updated)
81+
DevA->>GH: Pushes Script to Repo
82+
GH->>DevB: Pulls Code
83+
DevB->>DevB: Runs 'Migrate' (Local DB updated)
84+
Note over DevA,DevB: Both Databases are now identical!
85+
86+
```
87+
88+
---
89+
90+
## Why You Should NEVER Edit Tables Manually
91+
92+
If you go into your database tool (like pgAdmin or MySQL Workbench) and manually add a column:
93+
94+
* **Production will crash:** When you deploy your code, the live server won't have that column.
95+
* **Loss of History:** You won't know *when* or *why* that change was made.
96+
* **Team Friction:** Your teammates will constantly get "Table not found" errors.
97+
98+
## Summary Checklist
99+
100+
* [x] I understand that Migrations are "Version Control" for my database.
101+
* [x] I know that `up` applies changes and `down` reverts them.
102+
* [x] I understand that migration files should be committed to GitHub.
103+
* [x] I promise to never manually edit my database schema in production.
104+
105+
:::tip Career Secret
106+
In a job interview, if you mention that you use **Migrations** to manage your schema instead of manual SQL scripts, you immediately look like a 10x more experienced developer. It shows you know how to work in a **real team**.
107+
:::
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
sidebar_position: 7
3+
title: "Indexes & Query Performance"
4+
sidebar_label: "7. Performance"
5+
description: Learn "how to make your database queries lightning fast using Indexes and execution analysis."
6+
---
7+
8+
Imagine you are in a massive library with 1,000,000 books. If you want to find a book about "Node.js," you don't start at the first shelf and check every book. You go to the **Index Catalog** at the front, find the shelf number, and go straight there.
9+
10+
In a database, an **Index** does exactly the same thing.
11+
12+
## What is an Index?
13+
14+
An index is a separate data structure (usually a **B-Tree**) that stores a sorted version of a specific column. This allows the database to "jump" to the data instead of scanning the whole table.
15+
16+
### ✅ Pros:
17+
18+
* **Speed:** Searching becomes 100x to 1000x faster.
19+
* **Efficiency:** Reduces the load on your server's CPU and Memory.
20+
21+
### ❌ Cons:
22+
23+
* **Storage:** Indexes take up extra disk space.
24+
* **Write Speed:** Every time you `INSERT` or `UPDATE`, the database has to update the index too. **Don't index every column!**
25+
26+
## How to Create an Index
27+
28+
<Tabs>
29+
<TabItem value="single" label="☝️ Single Column" default>
30+
31+
If you frequently search for users by their email, add an index to the `email` column.
32+
33+
```sql
34+
CREATE INDEX idx_user_email ON Users(email);
35+
```
36+
37+
</TabItem>
38+
<TabItem value="composite" label="✌️ Composite Index">
39+
40+
If you often search for people using both `first_name` AND `last_name`, a composite index is better.
41+
42+
```sql
43+
CREATE INDEX idx_full_name ON Users(first_name, last_name);
44+
45+
```
46+
47+
</TabItem>
48+
</Tabs>
49+
50+
## Performance Analysis: The `EXPLAIN` Command
51+
52+
How do you know if your query is actually using an index or being slow? You ask the database to explain its "Query Plan."
53+
54+
```sql
55+
EXPLAIN ANALYZE
56+
SELECT * FROM Users WHERE email = 'aryan@codeharborhub.github.io';
57+
```
58+
59+
**What to look for in the output:**
60+
61+
* **Seq Scan (Sequential Scan):** BAD. The DB is reading every row in the table.
62+
* **Index Scan:** GOOD. The DB is using your index to find the data instantly.
63+
* **Cost:** Lower is better!
64+
65+
## Top 3 Performance Tips for Backend Devs
66+
67+
### 1. Avoid `SELECT *`
68+
69+
Only ask for the columns you need.
70+
71+
* `SELECT * FROM Users;` (Heavy and slow)
72+
* `SELECT name, email FROM Users;` (Light and fast)
73+
74+
### 2. Use `LIMIT`
75+
76+
If you have 1 million users, don't fetch them all at once. Use pagination.
77+
78+
```sql
79+
SELECT * FROM Users LIMIT 10 OFFSET 0;
80+
```
81+
82+
### 3. Index your Foreign Keys
83+
84+
Always create an index on columns used in `JOIN` operations. This prevents the database from getting confused when linking large tables.
85+
86+
## Summary Checklist
87+
88+
* [x] I understand that an Index is like a library catalog for my data.
89+
* [x] I know that Indexes speed up **Reads** but slow down **Writes**.
90+
* [x] I can use `EXPLAIN ANALYZE` to check my query performance.
91+
* [x] I know why `SELECT *` is a "Red Flag" in professional code.
92+
93+
:::tip Interview Secret
94+
If a Senior Developer asks you "How do you optimize a slow API?", your first answer should always be: **"I would check the database logs and see if we need to add an Index or optimize the SQL query using EXPLAIN."**
95+
:::
96+
97+
**Congratulations!** You've completed the Relational Database Module.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
sidebar_position: 1
3+
title: "Introduction to RDBMS"
4+
sidebar_label: "1. What is RDBMS?"
5+
description: "The fundamental building blocks of Relational Database Management Systems for beginners."
6+
---
7+
8+
In the world of Backend Development, data is everything. But how do we store it so that it stays organized, secure, and easy to find? We use a **Relational Database Management System (RDBMS)**.
9+
10+
## 🧐 The "Spreadsheet" Analogy
11+
12+
The easiest way to understand a Relational Database is to think of it as a collection of **Excel Spreadsheets** that are linked together.
13+
14+
Imagine you are running a school:
15+
* One sheet for **Students** (Name, Roll No, Age).
16+
* One sheet for **Courses** (Course Name, Teacher).
17+
* One sheet for **Enrollments** (Which Student is in which Course).
18+
19+
Instead of repeating a student's name 10 times, you just link their unique **Roll Number**. That is the "Relationship" in Relational Database!
20+
21+
## Core Components of RDBMS
22+
23+
Every Relational Database is built using these three specific parts:
24+
25+
<Tabs>
26+
<TabItem value="tables" label="📊 Tables" default>
27+
The basic container. Everything in RDBMS is a table.
28+
* **Rows (Records):** Represent a single item (e.g., One specific student).
29+
* **Columns (Fields):** Represent a property (e.g., Student's Email).
30+
</TabItem>
31+
<TabItem value="keys" label="🔑 Keys">
32+
Keys are the "Glue" that holds tables together.
33+
* **Primary Key (PK):** A unique ID that identifies a specific row (e.g., Roll No).
34+
* **Foreign Key (FK):** A link used to refer to a Primary Key in another table.
35+
</TabItem>
36+
<TabItem value="constraints" label="⛓️ Constraints">
37+
Rules that the data must follow.
38+
* **NOT NULL:** This column cannot be empty.
39+
* **UNIQUE:** No two rows can have the same value in this column.
40+
</TabItem>
41+
</Tabs>
42+
43+
## Visualizing the Relationship
44+
45+
Using a **Mermaid Diagram**, we can see how an E-commerce database actually "relates" different tables:
46+
47+
```mermaid
48+
erDiagram
49+
USER ||--o{ ORDER : places
50+
ORDER ||--|{ PRODUCT : contains
51+
USER {
52+
string username
53+
string email PK
54+
string password
55+
}
56+
ORDER {
57+
int order_id PK
58+
string user_email FK
59+
date created_at
60+
}
61+
PRODUCT {
62+
int product_id PK
63+
string name
64+
float price
65+
}
66+
67+
```
68+
69+
## Why do we use RDBMS?
70+
71+
:::tip The Golden Rule
72+
If your data is **structured** (fits into rows and columns) and needs to be **100% accurate** (like a bank balance), use an RDBMS.
73+
:::
74+
75+
1. **Data Integrity:** It ensures that you cannot delete a user if they still have active orders.
76+
2. **Scalability:** Modern RDBMS like PostgreSQL can handle millions of rows without slowing down.
77+
3. **Security:** You can control exactly who can read or write to specific tables.
78+
4. **Standard Language:** Almost every RDBMS uses **SQL**, making your skills transferable.
79+
80+
## ACID: The Security Shield
81+
82+
Relational Databases follow the **ACID** principle to ensure that even if the server crashes, your data remains safe.
83+
84+
:::info Quick Definition
85+
86+
* **Atomicity:** All parts of a transaction succeed, or none do.
87+
* **Consistency:** Data follows all defined rules.
88+
* **Isolation:** Transactions don't interfere with each other.
89+
* **Durability:** Once saved, data stays saved even during power failure.
90+
:::
91+
92+
## Summary Checklist
93+
94+
* [x] I know that RDBMS stands for Relational Database Management System.
95+
* [x] I understand that tables are linked using **Primary** and **Foreign Keys**.
96+
* [x] I recognize that data is stored in Rows and Columns.
97+
* [x] I understand why ACID properties are important for data safety.
98+
99+
:::note Homework
100+
Think about a Social Media app like Instagram. What tables would you need? (Hint: Users, Posts, Comments). Try to imagine which columns would be the "Primary Keys"!
101+
:::

0 commit comments

Comments
 (0)