Skip to content
Back to 50 Concepts
Topic 2 of 50foundationsBeginner

# Topic 2: Database & Tables

Relational database architecture: schemas, tables, columns, slotted page row storage, primary keys, and relationships.

1. Definition

A database is an organized container of schemas, while a table is a structured collection of rows (records) and columns (attributes) representing an entity.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

A database is a giant filing cabinet. Each drawer is a schema, each folder is a table, each sheet of paper is a row, and each form field on that paper is a column.

3. Animation & Visual Simulation

Interactive Engine Trace
Interactive Engine Simulation

Slotted Page Disk Architecture

Step 1 of 3
SQL>SELECT * FROM users WHERE id = 42; -- Examining 8KB Block 0x05Active Input

Step 1: 8KB Page Header & Item Pointers

Engine Action: Inspect Page Header (24 bytes)
Page Header (24B)

LSN: 0/1A45B, Flags: 0x0

Line Pointer #1

Offset: 8140, Len: 48B

Line Pointer #2

Offset: 8080, Len: 60B

How the Database Executes This:

Every disk page begins with a 24-byte header containing LSN (Log Sequence Number) and line pointer count.

💡 Item pointers grow from bottom-up; raw row tuples grow top-down from end of page.

4. Real-World Example

A university system stores students and courses.

Students Table
idfirst_namegpaenrolled_at
1Alice3.852024-09-01
2Bob3.402024-09-01
Relational walkthrough: Every row represents a unique student; every column has a strict type (e.g. gpa is NUMERIC(3,2)).

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Step 1: Create a dedicated schema
CREATE SCHEMA IF NOT EXISTS university;
Namespaces keep tables organized and avoid naming collisions.
Step 2: Create a structured table
CREATE TABLE university.students (
  id INT PRIMARY KEY,
  first_name VARCHAR(50) NOT NULL,
  gpa NUMERIC(3, 2) CHECK (gpa >= 0.0 AND gpa <= 4.0),
  enrolled_at DATE NOT NULL DEFAULT CURRENT_DATE
);
Columns enforce strict types, default values, and domain integrity rules.

6. Important Rules & Behaviors

RuleBehavioral Explanation
Column HomogeneityAll values in a column must match the defined data type.
Row UniquenessEvery row should be uniquely identifiable via a Primary Key.
Atomic ValuesFirst Normal Form requires that cells contain single scalar values, not nested arrays or unparsed text.
Unordered heap storageStandard tables are heaps: rows are placed into any available 8 KB page slot with free space.
Maximum tuple size (TOAST)Attributes exceeding ~2 KB are compressed and stored out-of-line in secondary TOAST tables.

7. Deep Dive: Physical Slotted Page Storage

On disk, tables are stored in fixed-size blocks (typically 8 KB in PostgreSQL). The page header contains pointers (item pointers) growing downward, while row tuples grow upward from the bottom of the page.

-- Slotted Page Layout:
-- [ Page Header | Line Pointer 1 | Line Pointer 2 | ... Free Space ... | Tuple 2 | Tuple 1 ]
Caution: Variable-length text columns that exceed page capacity are automatically moved off-table into TOAST storage.

8. Advantages & Disadvantages

Advantages

  • •Predictable memory and disk layout.
  • •Fast column projection and indexing.
  • •Automatic type safety at the storage engine level.

Disadvantages & Trade-offs

  • •Altering column types on multi-million row tables can require rewrite locks.
  • •Sparse tables with many nullable columns can consume unnecessary page space.

9. Common Interview Questions

Q1.How does a relational database physically store a table on disk?

Relational engines store tables as an array of fixed-size disk blocks (typically 8 KB pages). Each page contains a 24-byte header, an array of 4-byte line pointers growing downwards from the top, and actual row tuples growing upwards from the bottom. The gap between them is free space. This slotted design allows rows to be moved inside the page during defragmentation without invalidating the page line pointer.

Q2.What is the difference between a Heap Table and an Index-Organized (Clustered) Table?

In a Heap Table (default in PostgreSQL), rows are placed in any disk page with available free space in no particular order, and secondary indexes store physical tuple pointers (CTID). In an Index-Organized Table (default in MySQL InnoDB), the table rows are physically sorted and stored directly inside the leaf nodes of the Primary Key B+ Tree.

FeatureHeap Table (PostgreSQL)Index-Organized Table (MySQL InnoDB)
Storage LayoutUnordered pages with free-space mapPhysically ordered inside PK B+ Tree leaves
Primary Key LookupIndex scan -> Heap page fetchDirect leaf lookup (Index-Only Scan)
Secondary IndexPoints to physical disk CTID (block:offset)Points to Primary Key value
Insert SpeedVery fast (appends to available slot)Requires B+ Tree traversal & node splits

Q3.What is column alignment padding and how does column ordering impact table size?

CPUs access memory faster when data is aligned to word boundaries (e.g. 8 bytes for BIGINT, 4 bytes for INT). When a SMALLINT (2 bytes) is followed by a BIGINT (8 bytes), the database must insert 6 bytes of empty padding to align the BIGINT. Ordering columns from largest to smallest (8-byte, then 4-byte, 2-byte, 1-byte) completely eliminates padding waste.

Q4.What is the High Water Mark (HWM) in a table and why does deleting rows not shrink the file?

The High Water Mark is the maximum physical disk block ever allocated to the table. When rows are deleted, the database marks the page slots as dead (free), but does not deallocate the operating system file blocks back to disk to avoid filesystem fragmentation. To return disk space to the OS, PostgreSQL requires VACUUM FULL and MySQL requires OPTIMIZE TABLE.

Q5.What is TOAST (The Oversized-Attribute Storage Technique) in PostgreSQL?

Since PostgreSQL pages are fixed at 8 KB, row tuples cannot span across page boundaries. When any attribute (such as TEXT, VARCHAR, or JSONB) exceeds approximately 2 KB, PostgreSQL compresses it. If it is still too large, it splits the data into 2 KB chunks and stores them in a separate TOAST table, leaving an 18-byte pointer in the main row.

10. Interview-Ready Answer

“A relational database organizes entities into schemas and tables. Each table enforces a strict schema of typed columns, storing records in fixed-size slotted disk pages with primary keys ensuring row identity.”

11. Quick Test

Scenario Question:

In a standard relational table, what ensures that every record can be distinctly identified?

Select your answer: