Databases Beginner Course
Learn how the world stores its data. Master SQL, CRUD, joins, normalization, keys, indexes, transactions, Python database access, and build a library database capstone.
Start LearningWhat Is a Database?
14 minWhat you'll learn
- Define database
- Understand DBMS
- Know why databases beat files
A database is an organized collection of data managed by a DBMS (Database Management System). Unlike flat files, databases guarantee data integrity, concurrent access, fast queries, and durability. Every app you use — banking, social media, e-commerce — stores its data in a database. Understanding databases means understanding where and how the world's information lives.
# File (fragile) vs Database (structured)
# File: no constraints, no concurrency, slow search
# Database: schema, ACID, indexes, multi-user
print('Databases organize, protect, and query data')
Try it yourself
Name three real apps that use databases.
Banking, social media, e-commerce, healthcare, gaming.
Relational vs NoSQL Databases
16 minWhat you'll learn
- Distinguish SQL and NoSQL
- Know when to use each
- Understand data models
Relational databases (MySQL, PostgreSQL) store data in tables with rigid schemas and relationships — perfect for structured, transactional data. NoSQL databases (MongoDB, Redis) store documents, key-values, or graphs — flexible, horizontally scalable, ideal for unstructured or rapidly-changing data. The choice is a design decision: consistency and relationships (SQL) vs flexibility and scale (NoSQL).
# Relational: tables, rows, joins
# NoSQL: documents, key-value, graphs
comparison = {
'SQL': 'structured, ACID, joins, vertical scaling',
'NoSQL': 'flexible, horizontal scaling, eventual consistency'
}
print(comparison)
Try it yourself
Which is better for a social feed with millions of users?
NoSQL — horizontal scaling for massive unstructured data.
Tables, Rows & Columns
14 minWhat you'll learn
- Understand relational structure
- Learn rows vs columns
- Grasp primary keys
A relational database is made of tables — each table has columns (fields) and rows (records). A users table has columns like id, name, email; each user is a row. The PRIMARY KEY uniquely identifies each row. This tabular structure is the foundation of all SQL. Everything else — queries, joins, indexes — operates on this simple grid.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
age INTEGER
);
Try it yourself
What column type would you use for a primary key?
An auto-incrementing integer.
SQL Basics: SELECT & WHERE
18 minWhat you'll learn
- Write SELECT queries
- Filter with WHERE
- Understand the query flow
SELECT is the most important SQL command — it retrieves data. SELECT column FROM table fetches values; WHERE filters rows by condition. The query flow: FROM (pick table) → WHERE (filter rows) → SELECT (pick columns). Master SELECT + WHERE and you can answer most basic data questions. It's the 'print' of databases.
-- Get names of users over 18 SELECT name, email FROM users WHERE age > 18;
Try it yourself
Write a query to select all columns from a products table.
SELECT * FROM products;
SQL Basics: INSERT, UPDATE, DELETE
18 minWhat you'll learn
- Add, modify, remove rows
- Use parameterized queries
- Understand CRUD
CRUD — Create, Read, Update, Delete — is the full data lifecycle. INSERT adds rows; SELECT reads; UPDATE modifies; DELETE removes. Together they're the four operations every app performs on data. The critical safety rule: always use a WHERE clause with UPDATE and DELETE — without it, you modify or delete EVERY row.
-- CREATE
INSERT INTO users (name, email) VALUES ('Ali', '[email protected]');
-- UPDATE
UPDATE users SET age = 22 WHERE name = 'Ali';
-- DELETE
DELETE FROM users WHERE age < 18;
Try it yourself
Why is WHERE critical in DELETE?
Without WHERE, DELETE removes all rows — catastrophic data loss.
SQL Aggregate Functions
16 minWhat you'll learn
- Use COUNT, SUM, AVG, MIN, MAX
- Group with GROUP BY
- Summarize data
Aggregate functions collapse many rows into one summary value. COUNT counts rows, SUM totals a column, AVG averages, MIN/MAX find extremes. GROUP BY groups rows by a column before aggregating — total sales per region, average age per department. This turns raw data into insights: one line of SQL replaces loops of Python code.
SELECT department, COUNT(*) AS employee_count, AVG(salary) AS avg_salary FROM employees GROUP BY department;
Try it yourself
How do you count all rows in a table?
SELECT COUNT(*) FROM table_name;
SQL Joins
22 minWhat you'll learn
- Combine tables with JOIN
- Learn INNER/LEFT/RIGHT joins
- Resolve relationships
Joins combine rows from multiple tables based on a related column — the power that makes relational databases relational. INNER JOIN keeps only matching rows; LEFT JOIN keeps all left rows plus matches; RIGHT JOIN the opposite. Example: join orders with customers to show who ordered what. Joins connect the dots across your schema.
SELECT users.name, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id;
Try it yourself
Which join keeps ALL rows from the left table?
LEFT JOIN.
Database Normalization
24 minWhat you'll learn
- Understand normalization
- Learn 1NF, 2NF, 3NF
- Eliminate redundancy
Normalization organizes data to eliminate redundancy and prevent anomalies — the same data stored once, referenced everywhere. 1NF: atomic values, no repeating groups. 2NF: no partial dependencies. 3NF: no transitive dependencies. The goal: every fact stored exactly once. Normalized schemas are cleaner, consistent, and the standard for transactional systems.
# Unnormalized: orders has customer_name repeated # Normalized: customers table + orders references customer_id -- customers: (id, name, email) -- orders: (id, customer_id, total) ← no name duplication
No duplication — customer referenced by id
Try it yourself
What's the main goal of normalization?
Eliminate data redundancy and anomalies.
Primary & Foreign Keys
18 minWhat you'll learn
- Define primary and foreign keys
- Enforce referential integrity
- Build relationships
A primary key uniquely identifies a row in its table. A foreign key in another table references that primary key, creating a relationship and enforcing referential integrity — you can't have an order for a non-existent customer. This constraint is the backbone of relational design: related data stays consistent automatically, because the database rejects orphaned references.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
Try it yourself
What does referential integrity prevent?
Rows referencing non-existent parent rows.
Indexes & Query Performance
22 minWhat you'll learn
- Understand why indexes speed queries
- Create indexes
- Know the trade-off
Indexes are like a book's index — they let the database find rows without scanning the entire table. An index on a column enables O(log n) lookups instead of O(n) scans. The trade-off: indexes speed SELECT but slow INSERT/UPDATE/DELETE (must maintain the index), and consume storage. Index the columns you search and join on — not everything.
CREATE INDEX idx_users_email ON users(email); -- This query now uses the index SELECT * FROM users WHERE email = '[email protected]';
Try it yourself
Index's main downside?
Slower writes — INSERT/UPDATE must maintain the index.
Transactions & ACID
22 minWhat you'll learn
- Understand transactions
- Learn ACID properties
- Guarantee data safety
A transaction is a group of operations that succeed or fail TOGETHER — like transferring money (deduct from A, add to B: both or neither). ACID guarantees: Atomicity (all or nothing), Consistency (valid state), Isolation (concurrent transactions don't interfere), Durability (committed data survives crashes). Transactions are why banks don't lose money to race conditions.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT; -- both applied, or ROLLBACK undoes both
Try it yourself
What does Atomicity mean?
A transaction either fully completes or fully rolls back — no partial state.
SQL Views
16 minWhat you'll learn
- Create virtual tables
- Simplify complex queries
- Add a security layer
A view is a saved SELECT query that acts like a virtual table. It simplifies complex joins, encapsulates business logic, and can hide sensitive columns. Instead of writing a 10-line join every time, you create a view once and SELECT from it. Views are queries stored in the database — the ultimate DRY (Don't Repeat Yourself) tool for SQL.
CREATE VIEW active_users AS SELECT id, name, email FROM users WHERE is_active = 1; SELECT * FROM active_users;
Try it yourself
A view is like what in programming?
A function — saved logic you can call repeatedly.
Python + SQLite
22 minWhat you'll learn
- Connect Python to a database
- Execute SQL from Python
- Use parameterized queries
SQLite is a file-based database built into Python — zero setup, perfect for learning and small apps. The sqlite3 module connects Python and SQL: create a connection, get a cursor, execute SQL, commit, close. Parameterized queries (?) prevent SQL injection — never string-format user input into SQL. This is how apps talk to databases.
import sqlite3
conn = sqlite3.connect('app.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)')
cursor.execute('INSERT INTO users (name) VALUES (?)', ('Ali',))
conn.commit()
cursor.execute('SELECT * FROM users')
print(cursor.fetchall())
conn.close()
Try it yourself
Why use ? placeholders?
To prevent SQL injection by safely escaping user input.
SQL Injection & Security
22 minWhat you'll learn
- Understand SQL injection
- Prevent attacks
- Write secure queries
SQL injection is the #1 database vulnerability: attacker input like '; DROP TABLE users; --' gets executed as SQL when you concatenate strings. Prevention: parameterized queries (placeholders), ORMs, input validation, and least-privilege database accounts. Understanding this attack is essential — it's how millions of records get leaked every year.
# VULNERABLE (never do this)
# query = f"SELECT * FROM users WHERE name = '{user_input}'"
# SAFE
cursor.execute('SELECT * FROM users WHERE name = ?', (user_input,))
Try it yourself
What's the #1 defense against SQL injection?
Parameterized queries (prepared statements).
Object-Relational Mapping (ORM)
20 minWhat you'll learn
- Understand ORMs
- Use SQLAlchemy
- Map Python classes to tables
An ORM (Object-Relational Mapping) lets you work with databases using Python objects instead of raw SQL — a User class maps to a users table, attributes to columns. SQLAlchemy is Python's standard ORM. Benefits: type safety, cleaner code, database-agnostic (switch DBs without rewriting). Trade-off: some performance loss and hidden complexity. ORMs power most web frameworks.
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
Try it yourself
What does an ORM map?
Python classes/objects to database tables/rows.
Database Backup & Recovery
18 minWhat you'll learn
- Understand backup strategies
- Learn restore procedures
- Protect against data loss
Backups are your last line of defense — full backups (everything), incremental (changes since last backup), and continuous (transaction logs). The 3-2-1 rule: 3 copies, 2 media types, 1 offsite. Recovery means restoring to a point in time. No backup = data loss is permanent. This is operational database skill every developer needs.
# PostgreSQL backup
# pg_dump mydb > backup.sql
# restore
# psql mydb < backup.sql
# SQLite backup
import sqlite3
src = sqlite3.connect('app.db'); dst = sqlite3.connect('backup.db')
src.backup(dst)
Try it yourself
What's the 3-2-1 backup rule?
3 copies, 2 media types, 1 offsite.
Database Design Basics (ERD)
20 minWhat you'll learn
- Design entity-relationship diagrams
- Identify entities and relations
- Translate ERDs to tables
An Entity-Relationship Diagram (ERD) visualizes your data model: entities (tables), attributes (columns), and relationships (one-to-one, one-to-many, many-to-many). Before writing CREATE TABLE, you sketch the ERD. One-to-many (customer→orders) is the most common; many-to-many needs a junction table. Good design upfront prevents painful migrations later.
# Entities: Customer (1) --- (many) Orders # Many-to-many: Students <-> Courses needs enrollment table customers(id, name) orders(id, customer_id, total) enrollments(student_id, course_id)
Try it yourself
How do you model many-to-many in relational DB?
A junction table with two foreign keys.
Constraints & Data Integrity
18 minWhat you'll learn
- Use constraints
- Enforce valid data
- Prevent bad records
Constraints are rules the database enforces: NOT NULL (required), UNIQUE (no duplicates), CHECK (value range), DEFAULT (fallback), FOREIGN KEY (relationship). They make the database self-protecting — invalid data can't even enter. Better to reject bad data at write time than clean it up later. Constraints are your schema's immune system.
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price DECIMAL CHECK (price >= 0),
stock INTEGER DEFAULT 0,
UNIQUE(name)
);
Try it yourself
Which constraint ensures a column can't be empty?
NOT NULL.
Intro to PostgreSQL vs MySQL
20 minWhat you'll learn
- Compare popular databases
- Know feature differences
- Choose the right DB
PostgreSQL and MySQL are the two dominant open-source relational databases. PostgreSQL: feature-rich, standards-compliant, advanced types (JSON, arrays), the choice for complex applications. MySQL: fast, simple, widely used in web apps (WordPress), great for read-heavy workloads. Both are excellent — the choice depends on complexity and ecosystem. PostgreSQL is the modern default for new projects.
# PostgreSQL strengths: JSONB, full-text, complex queries, extensions
# MySQL strengths: simplicity, replication, huge hosting ecosystem
print('PostgreSQL = features; MySQL = simplicity + hosting')
Try it yourself
Which DB is known for advanced JSON support?
PostgreSQL.
Capstone: Build a Library Database
50 minWhat you'll learn
- Design a complete schema
- Apply CRUD, joins, constraints
- Query real relationships
Your capstone: build a library database. Design tables (books, authors, borrowers, loans), define relationships, add constraints, then write queries — find books by author, active loans, overdue books. This applies everything: schema design, keys, joins, aggregates, constraints. It's the complete, portfolio-ready database project.
CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER,
FOREIGN KEY (author_id) REFERENCES authors(id));
CREATE TABLE loans (id INTEGER PRIMARY KEY, book_id INTEGER,
due_date DATE,
FOREIGN KEY (book_id) REFERENCES books(id));
-- Query: books by author
SELECT books.title FROM books
JOIN authors ON books.author_id = authors.id
WHERE authors.name = 'JK Rowling';
Try it yourself
What query finds all overdue loans?
SELECT * FROM loans WHERE due_date < CURRENT_DATE;
You've completed all 20 lessons. Ready for more?
Continue to Databases Intermediate for advanced SQL, query optimization, and NoSQL.