build-a-sql-database / lesson-04.md
Lesson 04 · In-memory tables

A table

A table pairs a schema with the rows that follow it. Today you build the table that holds your data and hand back every row you put in.

The goal

Build a table from a schema, append rows to it, and iterate the rows back in insertion order.

Start here - the target
TO DO
Scenario: Appending and iterating table rows
Givenan empty table with schema (id INTEGER), (name TEXT)
Whenthe rows [1, "alice"] and [2, "bob"] are appended
Theniterating the table yields exactly those two rows in that order
Andthe table row count is 2
Background

A table is the whole picture: a schema describing the columns, and the rows that conform to it, kept in the order they were inserted. That order matters - without an ORDER BY, a query returns rows in insertion order, and everything you build on top will rely on scanning them predictably.

Right now Append takes any row without checking it. That is deliberate: storing rows and validating them are two ideas, and today is only the first. Tomorrow you make the table refuse a row that does not match its schema.

Make it work
type Table struct { Schema Schema; Rows []Row }
func (t *Table) Append(r Row) { t.Rows = append(t.Rows, r) }
// iteration is just ranging over t.Rows
CheckpointDONE
A table stores a schema and its rows and gives them back in order. Commit and stop here.