Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

DBMS Cheatsheet

πŸ“Š Normal Forms

1NF: Atomic values, no repeating groups
2NF: 1NF + No partial dependency on composite key
3NF: 2NF + No transitive dependency
BCNF: Every determinant is a candidate key

πŸ”’ ACID Properties

PropertyMeaningImplementation
AtomicityAll or nothingWAL, undo logs
ConsistencyValid state transitionsConstraints, triggers
IsolationConcurrent transactions don’t interfereLocks, MVCC
DurabilityCommitted data survives crashesWAL, fsync

πŸ”€ Isolation Levels

LevelDirty ReadNon-RepeatablePhantomPerformance
Read Uncommittedβœ…βœ…βœ…Best
Read CommittedβŒβœ…βœ…Good
Repeatable ReadβŒβŒβœ…Moderate
Serializable❌❌❌Worst

πŸ”‘ Keys

Primary Key: Unique, NOT NULL, one per table
Foreign Key: References primary key of another table
Candidate Key: Minimal super key (can be PK)
Super Key: Set of attributes that uniquely identifies tuple
Composite Key: Multiple columns forming a key
Surrogate Key: Artificial (auto-increment)
Natural Key: Real-world data (email, SSN)

πŸ”— Joins

INNER JOIN: Only matching rows
LEFT JOIN: All left + matching right
RIGHT JOIN: All right + matching left
FULL OUTER JOIN: All from both
CROSS JOIN: Cartesian product
SELF JOIN: Table with itself

πŸ“ˆ Indexing

B-Tree: Balanced, sorted, O(log n), good for range queries
Hash: O(1) exact match, NOT for range
Clustered: Physical order = index order, one per table
Non-Clustered: Separate structure, multiple per table

When to index:
βœ… WHERE, JOIN, ORDER BY columns
βœ… High cardinality
βœ… Read-heavy tables

When NOT to index:
❌ Small tables
❌ Frequently updated columns
❌ Low cardinality

πŸ—„οΈ SQL vs NoSQL

AspectSQLNoSQL
SchemaFixedDynamic
ScalingVerticalHorizontal
ACIDFullVaries
JoinsNativeAvoided
Best ForStructured, relationalUnstructured, high-scale

πŸ“ CAP Theorem

C (Consistency): Every read gets latest write
A (Availability): Every request gets response
P (Partition Tolerance): Works despite network failures

Choose 2 of 3 (P is mandatory in distributed systems):
CP: Consistent but may reject requests (HBase, MongoDB)
AP: Available but may return stale data (Cassandra, DynamoDB)

πŸ—ƒοΈ Common SQL

-- Window Functions
SELECT name, salary,
  RANK() OVER (ORDER BY salary DESC) as rank,
  AVG(salary) OVER (PARTITION BY dept) as dept_avg
FROM employees;

-- CTE
WITH active AS (
  SELECT * FROM users WHERE status = 'active'
)
SELECT * FROM active WHERE age > 25;

-- Subquery
SELECT * FROM employees
WHERE dept_id IN (SELECT id FROM departments WHERE location = 'NYC');

-- Aggregate
SELECT dept, COUNT(*), AVG(salary)
FROM employees
GROUP BY dept
HAVING AVG(salary) > 50000;

πŸ”§ Transactions

BEGIN TRANSACTION;
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;  -- or ROLLBACK on failure

⚑ Quick Facts

  • Denormalization: Add redundancy for read performance
  • Sharding: Horizontal partitioning across databases
  • Replication: Copying data across servers (master-slave, master-master)
  • Connection pooling: Reuse DB connections (PgBouncer, HikariCP)
  • ORM: Maps tables to objects (SQLAlchemy, Hibernate)
  • Deadlock: Two transactions waiting for each other’s locks
  • Two-Phase Commit: Distributed transaction protocol (prepare + commit)
  • Materialized View: Pre-computed, stored view (faster reads)

πŸ”— Cross-References