# 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.
2. Explain Like I'm 5
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 TraceSlotted Page Disk Architecture
SELECT * FROM users WHERE id = 42; -- Examining 8KB Block 0x05Active InputStep 1: 8KB Page Header & Item Pointers
LSN: 0/1A45B, Flags: 0x0
Offset: 8140, Len: 48B
Offset: 8080, Len: 60B
Every disk page begins with a 24-byte header containing LSN (Log Sequence Number) and line pointer count.
4. Real-World Example
A university system stores students and courses.
| id | first_name | gpa | enrolled_at |
|---|---|---|---|
| 1 | Alice | 3.85 | 2024-09-01 |
| 2 | Bob | 3.40 | 2024-09-01 |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectCREATE SCHEMA IF NOT EXISTS university;
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 );
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| Column Homogeneity | All values in a column must match the defined data type. |
| Row Uniqueness | Every row should be uniquely identifiable via a Primary Key. |
| Atomic Values | First Normal Form requires that cells contain single scalar values, not nested arrays or unparsed text. |
| Unordered heap storage | Standard 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 ]
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.
| Feature | Heap Table (PostgreSQL) | Index-Organized Table (MySQL InnoDB) |
|---|---|---|
| Storage Layout | Unordered pages with free-space map | Physically ordered inside PK B+ Tree leaves |
| Primary Key Lookup | Index scan -> Heap page fetch | Direct leaf lookup (Index-Only Scan) |
| Secondary Index | Points to physical disk CTID (block:offset) | Points to Primary Key value |
| Insert Speed | Very 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
In a standard relational table, what ensures that every record can be distinctly identified?