Databases: SQL vs NoSQL

Choosing a database is the most critical decision in system design. Do you want strict order or total flexibility?

đŸ›ī¸ Relational (SQL)

Think: Excel Sheets. Data is stored in strict tables with Rows and Columns. Tables are linked (Joined) by IDs.

Examples: PostgreSQL, MySQL, SQL Server

📑 Document (NoSQL)

Think: JSON Files. Data is stored as flexible documents. One user can have 3 fields, the next can have 50. No rules.

Examples: MongoDB, DynamoDB, Firestore

đŸ•šī¸ The "Schema Enforcer" Game

Mission: Try to save a "User" object that has an unexpected new field: "hobby": "Chess".

SQL DB Strict Schema
Incoming Data
{
  "id": 1,
  "name": "Alice",
  "hobby": "Chess" // Unexpected!
}
Schema: id (INT), name (VARCHAR)
idname
1Alice
Waiting...
NoSQL DB Flexible Schema
Incoming Data
{
  "id": 2,
  "name": "Bob",
  "hobby": "Gaming" // New field? OK!
}
Collection: Users
(Accepts Anything)
{ "id": 2, "name": "Bob", "hobby": "Gaming" }
Waiting...

âš–ī¸ When to use which?

✅ Choose SQL if...

  • Structure rarely changes (e.g., Accounting systems).
  • Data integrity is #1 priority (Banks).
  • You need complex queries (JOINing 5 tables).

✅ Choose NoSQL if...

  • Data structure changes constantly (e.g., Game Analytics).
  • Speed & Scale > Strict Consistency (Social Feeds).
  • You are a startup and don't know your schema yet.