4. Introduction to Databases (Why Files Fail)
Introduction to Databases
Every real application needs to store data permanently. You might think: Why not just use a JSON file? This module explains why JSON files fail for real applications — and what databases do instead.
The Problem with JSON Files
Let's see what happens when you try to use a file as a database:
// This seems to work... for one user on your laptop const fs = require('fs'); function findUserByEmail(email) { const data = fs.readFileSync('users.json', 'utf8'); const users = JSON.parse(data); return users.find(u => u.email === email); } // But here are the REAL problems:
Problem 1 — Concurrency: When 100 users write to the same file simultaneously, data gets corrupted. File systems aren't designed for multiple simultaneous writers.
Problem 2 — Speed: To find one user by email, you must read the ENTIRE file into memory. With 1 million users, that's impossible.
Problem 3 — Relationships: How do you link a user to their 100 orders? With files, you're on your own. Relational databases were built for exactly this.
Problem 4 — Durability: If your server crashes in the middle of writing to a file, the file becomes corrupted. Databases guarantee that either the write fully completes or it's fully rolled back.
The Two Types of Databases
Data organized in tables with rows and columns. Strict schema. Great for relationships and complex queries.
Examples: PostgreSQL, MySQL, SQLite
Best for: Financial systems, e-commerce, user accounts
Data stored as flexible JSON-like documents. Schemaless. Great for rapid iteration and varied data shapes.
Examples: MongoDB, Firebase, DynamoDB
Best for: Content management, logs, rapid prototypes
Key Database Concepts
- Table: Like a spreadsheet. Stores related data (e.g., a
userstable). - Row: One record in a table (e.g., one specific user).
- Column: One piece of data about a record (e.g.,
email,age). - Primary Key: A unique identifier for each row (e.g., user ID 1, 2, 3).
- Foreign Key: A link to a row in another table (e.g., an order has a user_id that points to a user).
What You'll Learn Next
In the next module, you'll write your first SQL queries: SELECT, INSERT, UPDATE, and DELETE. You'll be able to run these in your browser using the built-in editor — SQL is for copying and learning, not for execution in this sandbox.
Knowledge Check
Ready to test your understanding of 4. Introduction to Databases (Why Files Fail)?