Database Fundamentals
SQL vs NoSQL, ACID properties, and when to use what. The foundation of every system.
1The Filing System Analogy
2SQL (Relational) Databases
How Data is Organized
| id | name | |
|---|---|---|
| 1 | Alice | alice@mail.com |
| 2 | Bob | bob@mail.com |
| id | user_id | total |
|---|---|---|
| 101 | 1 | $50 |
| 102 | 1 | $30 |
| 103 | 2 | $75 |
SELECT users.name, orders.total FROM users JOIN orders ON users.id = orders.user_idPopular SQL Databases
3NoSQL Databases
Four Types of NoSQL
Store JSON-like documents. Each document can have different fields.
Simple key→value mapping. Extremely fast lookups. Like a giant hash map.
Rows can have different columns. Optimized for writes and time-series data.
Store nodes and relationships. Optimized for traversing connections.
4SQL vs NoSQL: Visual Comparison
| Aspect | SQL | NoSQL |
|---|---|---|
| Schema | Fixed, predefined columns | Flexible, dynamic |
| Scaling | Vertical (bigger server) | Horizontal (more servers) |
| Relationships | JOINs across tables | Embed or denormalize |
| Transactions | Strong ACID support | Often eventual consistency |
| Query Language | Standardized SQL | Varies by database |
| Best For | Complex queries, relationships | Scale, flexibility, speed |
5ACID Properties
ACID ensures reliable transactions in SQL databases. Critical for financial data, inventory, and any data that must be consistent.
All or nothing. Either entire transaction succeeds, or nothing changes.
Database moves from one valid state to another. All rules/constraints are satisfied.
Concurrent transactions don't interfere. Each sees a consistent snapshot.
Once committed, data survives crashes, power outages, disasters.
6When to Use What
✓Choose SQL When
- →Complex relationships: Orders → Products → Categories
- →Need JOINs: Reports across multiple tables
- →ACID required: Banking, inventory, payments
- →Schema is stable: Known data structure upfront
✓Choose NoSQL When
- →Massive scale: Millions of ops/second
- →Flexible schema: Data structure evolves
- →Simple queries: Mostly key lookups
- →High write throughput: Logs, events, IoT
Many modern systems use BOTH (polyglot persistence). PostgreSQL for orders/users (need ACID), Redis for sessions/cache (need speed), Elasticsearch for search (need full-text).