Understanding Database Fundamentals

1. Understanding Database Management Systems (DBMS)

ComponentRoleExamples
Storage EnginePhysical data storage on disk/memoryInnoDB, WiredTiger, RocksDB
Query ProcessorParses, optimizes, executes queriesSQL parser, planner, executor
Transaction ManagerACID guarantees, concurrency controlMVCC, 2PL, WAL
Buffer ManagerCaches pages in RAMBuffer pool, page cache
Catalog/MetadataSchema definitions, statisticsinformation_schema, pg_catalog
Access ControlAuthentication, authorizationRoles, GRANT/REVOKE
Recovery ManagerCrash recovery from logsWAL, redo/undo logs
DBMS CategoryExamplesBest For
RDBMSPostgreSQL 16+, MySQL 8+, Oracle, SQL ServerOLTP, structured data, transactions
DocumentMongoDB 7+, Couchbase, FirestoreFlexible schema, JSON data
Key-ValueRedis 7+, DynamoDB, etcdCaching, sessions, leaderboards
Column-FamilyCassandra 5+, ScyllaDB, HBaseWrite-heavy, time-series, large scale
GraphNeo4j 5+, Amazon Neptune, ArangoDBRelationships, recommendations, fraud
Time-SeriesInfluxDB 3+, TimescaleDB, QuestDBMetrics, IoT, monitoring
SearchElasticsearch 8+, OpenSearch, MeilisearchFull-text search, log analytics
NewSQLCockroachDB 23+, YugabyteDB, TiDBDistributed SQL, global scale

2. Understanding Relational Databases (SQL)

ConceptDefinition
RelationTable with named columns (attributes) and rows (tuples)
TupleA single row in a table
AttributeA column with defined data type
DomainSet of permissible values for an attribute
Primary KeyUnique identifier for each row
Foreign KeyReference to primary key of another table
SchemaLogical structure: tables, columns, relationships
Relational AlgebraFormal basis: SELECT, PROJECT, JOIN, UNION

Example: Relational Model

CREATE TABLE customers (
  id        BIGSERIAL PRIMARY KEY,
  email     VARCHAR(255) NOT NULL UNIQUE,
  name      VARCHAR(100) NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE orders (
  id          BIGSERIAL PRIMARY KEY,
  customer_id BIGINT NOT NULL REFERENCES customers(id),
  total       NUMERIC(10,2) NOT NULL CHECK (total >= 0),
  placed_at   TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

3. Understanding NoSQL Databases

TypeData ModelQueryExample
DocumentJSON/BSON documentsField queries, aggregationMongoDB
Key-ValueOpaque value by keyGET/SET/DELRedis, DynamoDB
Wide-ColumnSparse rows with column familiesCQL by partition keyCassandra
GraphNodes & edges with propertiesCypher, GremlinNeo4j
TraitDescription
Schema-lessFlexible structure per record
Horizontal ScalingBuilt-in sharding/partitioning
Eventual ConsistencyTrades consistency for availability (typically)
DenormalizationData duplication for read performance
No JoinsJoins done in application or via embedding

4. Understanding ACID Properties

PropertyGuaranteeMechanism
AtomicityAll or nothingUndo logs, rollback
ConsistencyValid state to valid stateConstraints, triggers
IsolationConcurrent txs don't interfereLocks, MVCC
DurabilityCommitted data survives failureWAL, fsync

Example: ACID Transaction

BEGIN;
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;  -- both updates persist atomically, or neither does

5. Understanding BASE Properties

PropertyMeaning
Basically AvailableSystem always responds (may be stale)
Soft StateState may change without input (replication)
Eventual ConsistencyAll replicas converge given no new writes
Note: BASE favors availability and partition tolerance; ACID favors consistency. NoSQL stores typically lean BASE; RDBMS lean ACID.

6. Understanding CAP Theorem

PropertyDescription
Consistency (C)Every read receives the latest write
Availability (A)Every request gets a non-error response
Partition Tolerance (P)System operates despite network partitions
ChoiceTrade-offExamples
CPReject requests during partitionMongoDB, HBase, etcd
APReturn possibly stale dataCassandra, DynamoDB, Couchbase
CAOnly possible without partitions (single-node)Traditional RDBMS (non-distributed)
Warning: In distributed systems, partitions WILL happen — you must choose C or A during a partition. PACELC extends CAP for partition-free behavior (latency vs consistency).

7. Comparing SQL vs NoSQL

AspectSQLNoSQL
SchemaFixed, predefinedFlexible, dynamic
ScalingVertical (mostly)Horizontal
JoinsNative, optimizedLimited or app-side
TransactionsMulti-row ACIDOften single-row only
ConsistencyStrong by defaultTunable, often eventual
Query LanguageStandardized SQLVendor-specific APIs
Best FitOLTP, reporting, complex relationsHigh-volume, semi-structured, low latency

8. Understanding Database Schemas

LevelDescription
Physical SchemaHow data is stored (files, blocks, indexes)
Logical SchemaTables, columns, relationships, constraints
External Schema (View)User/application-specific projections
Conceptual SchemaEntity-relationship model, business rules

Example: Creating a Schema

CREATE SCHEMA sales AUTHORIZATION app_user;
CREATE TABLE sales.invoices (
  id     BIGSERIAL PRIMARY KEY,
  amount NUMERIC(12,2) NOT NULL
);
GRANT USAGE ON SCHEMA sales TO reporting_role;

9. Understanding Data Models

ModelStructureStrengths
RelationalTables & relationsJoins, integrity, mature tooling
DocumentHierarchical JSONSchema flexibility, aggregates
Key-ValueHash mapO(1) lookups, simplicity
Wide-ColumnSparse multi-dim mapMassive write throughput
GraphNodes + edgesTraversals, network analysis
ObjectPersistent objectsOO-language affinity
HierarchicalTreeParent-child fast access (IMS, XML)

10. Choosing Database Types for Applications

Use CaseRecommended
Banking / ERPPostgreSQL, Oracle (strong ACID)
Web Catalog / CMSMongoDB, PostgreSQL (JSONB)
Session / CacheRedis, Memcached
IoT / MetricsInfluxDB, TimescaleDB
Social GraphNeo4j, Amazon Neptune
SearchElasticsearch, OpenSearch
Global SaaSCockroachDB, Spanner, YugabyteDB
Event LogKafka + ClickHouse, Cassandra
Need transactions across rows? ──Yes──> SQL (Postgres/MySQL)
                  │No
                  ▼
Schema highly variable? ──Yes──> Document (MongoDB)
                  │No
                  ▼
Sub-ms key lookups? ──Yes──> Key-Value (Redis/DynamoDB)
                  │No
                  ▼
Heavy write + time data? ──Yes──> Time-Series / Wide-Column
                  │No
                  ▼
Relationship-heavy queries? ──Yes──> Graph (Neo4j)