1.1The Relational Model & SQL Architecture
The SQL Book/Chapter 1/1.1

1.1 The Relational Model & SQL Architecture

How relational databases structure information into mathematical relations (tables), tuples (rows), and attributes (columns).

Conceptual Deep Dive

In 1970, Edgar F. Codd revolutionized data processing by introducing the Relational Model. Unlike hierarchical or document storage where data is nested in unpredictable trees, relational systems organize information into strict mathematical relations (known in SQL as tables).

SQL (Structured Query Language) is a declarative language. In procedural languages like Rust, Python, or C++, you specify the exact control flow: 'initialize an array, loop through indices, filter matches, and append to a list.' In SQL, you describe the desired state of the output, and the database query planner (Cost-Based Optimizer) calculates the most efficient way to fetch, join, and filter the data using B-Trees, hash joins, or table scans.

Every relation satisfies three core invariants:

  1. First Normal Form (1NF): Each cell contains atomic, scalar values (no nested lists or unstructured blobs without strict typing).
  2. Primary Key Identity: Every tuple possesses an immutable unique identifier that distinguishes it from every other entity.
  3. Referential Integrity: Foreign keys establish verifiable mathematical mappings between distinct relations.

Syntax Specification

SQL Syntax:DDLDMLClausesFunctionsTypesStrings
-- Basic anatomy of a relational query
SELECT column1, column2, expression AS alias
FROM primary_table
JOIN related_table ON primary_table.id = related_table.foreign_id
WHERE filter_condition
ORDER BY sort_column ASC;
Key Takeaways
  • Declarative paradigm: You describe what data you need, not how the CPU iterates over disk pages.
  • Mathematical relations: Tables are unordered sets of tuples where columns represent typed attributes.
  • The query planner optimizes index traversal and execution plans under the hood.
Common Gotchas & Pitfalls
  • Tables are sets, not lists: Rows have no inherent order unless an explicit ORDER BY clause is provided.
  • Assuming sequential evaluation: SQL clauses are not executed in the order they are written (e.g. FROM and WHERE happen before SELECT).
⚙️ Under the Hood: SQLite Query Planner Internals

The SQLite query parser creates an Abstract Syntax Tree (AST), passes it to the code generator, and generates a bytecode VDBE (Virtual Database Engine) instruction sequence that runs against B-Tree disk pages.

Multiple Interactive Examples (3 Scenarios)

Each example includes input tables, formatted SQL code, execution breakdown, output tables, and live SQLite WASM execution.

Example 1 of 3Basic

Basic Table Inspection & Attribute Selection

Problem to Solve:

Retrieve the student identifier, full name, and grade level from the student body registry.

📋 Input Data (Source Table Before Query)
Table: studentsCore student registry with academic standing
idnameagegradefavorite_subjectgpa
1Emma Watson1611thComputer Science3.9
2Lucas Silva1510thMath3.7
3Sophia Chen1712thPhysics4
4Liam Miller1611thArt3.4
5Aria Patel1510thBiology3.8
💻 SQL Query
SQLite WASM
SELECT id, name, grade
FROM students;
⚙️ What Is Getting Done Under The Hood:
  1. Accesses the 'students' relation from the SQLite master catalog.
  2. Extracts only the attributes 'id', 'name', and 'grade', discarding 'age', 'favorite_subject', and 'gpa'.
  3. Streams the tuples to the output cursor without sorting overhead.
📊 Output Result Table
5 row(s) returned
3 columns projected across all 5 registered students:
idnamegrade
1Emma Watson11th
2Lucas Silva10th
3Sophia Chen12th
4Liam Miller11th
5Aria Patel10th
Pro Tip: Using explicit column names instead of SELECT * saves bandwidth and prevents schema breaking changes in client code.
Example 2 of 3Analytical

Inspecting Database System Catalog & Master Table

Problem to Solve:

Discover all active relational tables in the database along with their underlying schema DDL definitions.

💻 SQL Query
SQLite WASM
SELECT type, name, tbl_name
FROM sqlite_master
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
ORDER BY name ASC;
⚙️ What Is Getting Done Under The Hood:
  1. Queries the root schema metadata table 'sqlite_master'.
  2. Filters for relational tables while excluding internal engine storage tables (prefixed with sqlite_).
  3. Orders the table names alphabetically.
📊 Output Result Table
4 row(s) returned
Catalog listing of all user tables in the active SQLite database:
typenametbl_name
tablecoursescourses
tableenrollmentsenrollments
tableproductsproducts
tablestudentsstudents
Pro Tip: In PostgreSQL this corresponds to pg_catalog.pg_tables or INFORMATION_SCHEMA.TABLES.
Example 3 of 3Edge Case

Schema Column Introspection via PRAGMA

Problem to Solve:

Inspect the column types, default values, and primary key constraints of the courses table.

📋 Input Data (Source Table Before Query)
Table: coursesAcademic course catalog
course_idcourse_nameteachercreditsdepartment
101Intro to PythonDr. Turing4Computer Science
102Calculus IProf. Newton4Mathematics
103World HistoryMs. Earhart3Humanities
104Creative WritingMr. Poe2Arts
💻 SQL Query
SQLite WASM
PRAGMA table_info(courses);
⚙️ What Is Getting Done Under The Hood:
  1. Invokes the SQLite PRAGMA introspection command on the 'courses' table.
  2. Returns column index (cid), column name, affinity type, notnull flag, default value, and primary key index (pk).
  3. Confirms that course_id is designated as primary key (pk = 1).
📊 Output Result Table
5 row(s) returned
Metadata description of each column in the courses schema:
cidnametypenotnulldflt_valuepk
0course_idINTEGER0NULL1
1course_nameTEXT1NULL0
2teacherTEXT0NULL0
3creditsINTEGER0NULL0
4departmentTEXT0NULL0
Pro Tip: Explaino-Structura and the Supabase Studio panel use this exact PRAGMA query to render the live UI schema editor.
Press ← or → arrow keys to turn pages