Reading — step 1 of 3
Parsing DDL Statements
CREATE TABLE — Defining Schema
CREATE TABLE is a DDL (Data Definition Language) statement. Unlike queries that read or modify data, DDL defines the structure of your database.
The Syntax
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER)
This defines a table named users with three columns, each having a name and a type.
Parsing Strategy
After tokenizing, the token stream for this statement looks like:
KEYWORD CREATE → KEYWORD TABLE → IDENTIFIER users → SYMBOL (
→ IDENTIFIER id → KEYWORD INTEGER → SYMBOL ,
→ IDENTIFIER name → KEYWORD TEXT → SYMBOL ,
→ IDENTIFIER age → KEYWORD INTEGER → SYMBOL )
The parser consumes tokens left-to-right in a recursive descent pattern:
parse_create_table():
expect(KEYWORD, "CREATE")
expect(KEYWORD, "TABLE")
table_name = expect(IDENTIFIER)
expect(SYMBOL, "(")
columns = parse_column_list()
expect(SYMBOL, ")")
Storing Schema in Memory
You need a data structure to hold the schema:
This is your system catalog — the metadata that describes all tables. Real databases store this in special internal tables (SQLite uses sqlite_master, PostgreSQL uses pg_catalog).
Column Types
We support two types for now:
| Type | Description | Example Values |
|---|---|---|
INTEGER | Whole numbers | 1, 42, -7, 0 |
TEXT | Variable-length string | 'Alice', 'hello' |
SQLite actually uses a dynamic type system internally — the type declared in CREATE TABLE is an "affinity" hint, not a strict constraint. We'll be stricter in our implementation.
Error Handling
Your parser must handle these cases:
- Duplicate table:
CREATE TABLEfor a name that already exists should return an error - Missing table:
.schemafor an unknown table should return an error
How SQLite Does It
SQLite stores the complete CREATE TABLE SQL text in the sqlite_master table. When the database is opened, it replays these SQL statements to rebuild the schema. This is simple but elegant — the schema is self-describing.
Your Task
Parse CREATE TABLE statements, store the schema in memory, and implement .schema and .tables commands to inspect it.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…