Skip to content
INSERT INTO — Adding Rows
step 1/3

Reading — step 1 of 3

Parsing and Executing INSERT

~2 min readSQL Tokenizer & Parser

INSERT INTO — Adding Rows

INSERT INTO is a DML (Data Manipulation Language) statement. It adds new rows to an existing table.

The Syntax

INSERT INTO users VALUES (1, 'Alice', 30)

This inserts a single row with values matching the table's column order.

Parsing the Value List

After tokenizing:

KEYWORD INSERT → KEYWORD INTO → IDENTIFIER users
→ KEYWORD VALUES → SYMBOL (
→ NUMBER 1 → SYMBOL , → STRING Alice → SYMBOL , → NUMBER 30
→ SYMBOL )

The parser must:

  1. Identify the target table
  2. Parse the value list inside parentheses
  3. Match values to columns by position

Type Checking

Before storing the row, validate each value against the column's declared type:

Column: id INTEGER    → Value: 1        ✓ (integer matches INTEGER)
Column: name TEXT     → Value: 'Alice'  ✓ (string matches TEXT)
Column: age INTEGER   → Value: 'old'    ✗ (string doesn't match INTEGER)

This catches errors early. A type mismatch should produce a clear error message referencing the column name.

Column Count Validation

The number of values must exactly match the number of columns:

-- Table has 3 columns (id, name, age)
INSERT INTO users VALUES (1, 'Alice')           -- ERR: expected 3 values but got 2
INSERT INTO users VALUES (1, 'Alice', 30, 'X')  -- ERR: expected 3 values but got 4

Storage

For now, store rows as a list of tuples in memory:

python

This simple approach works well and is essentially what SQLite does before it writes to a B-tree on disk. The in-memory representation is a flat list of rows.

How PostgreSQL Handles INSERT

PostgreSQL's executor calls heap_insert(), which finds a page with enough free space (using the Free Space Map), formats the row as a heap tuple with a header containing transaction visibility info (for MVCC), and writes it. We'll build toward this level of sophistication.

Your Task

Parse INSERT INTO statements, validate types and column counts, store rows in memory, and implement .dump to verify the stored data.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…