Engine Atlas

Engine-neutral Foundation 01

Tables, rows, and keys

What does the schema allow, and which key conditions do the current rows already establish?

Separate a schema from its current rows, then test how primary and foreign keys decide whether a proposed order is valid.

Concept · relationalDerived · fixture
Foundation 01 · Engine-neutral

What does the schema allow, and which key conditions do the current rows already establish?

customers holds customer rows and orders holds order rows. orders.customer_id → customers.id connects them.

Separate the schema from current rows

The schema defines which columns and constraints customers and orders allow. Current rows are the values stored under that structure right now.

Schema definition
CREATE TABLE customers (
  id INTEGER NOT NULL,
  name VARCHAR(80) NOT NULL,
  country_code CHAR(2) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE orders (
  id INTEGER NOT NULL,
  customer_id INTEGER NOT NULL,
  status VARCHAR(20) NOT NULL,
  ordered_at TIMESTAMP NOT NULL,
  total_cents INTEGER NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

A candidate key is a minimal column set the schema declares can identify a row uniquely. This lesson selects customers.id and orders.id as primary keys; orders.customer_id references customers.id.

customers · parent rows
idnamecountry_code
101MinaKR
102NoahUS
103LinaDE
orders · current child rows
idcustomer_idstatusordered_attotal_cents
5001101PAID2026-07-28 09:10:003200
5002102PENDING2026-07-28 10:20:007500
5003101SHIPPED2026-07-29 13:40:0012800

Choose a proposed order

Only one or two values change between the rows. Predict from the schema and current rows first.

Choose a proposed order

Predict the result

Only one or two values change between the rows. Predict from the schema and current rows first.

Predict the result

Constraint checks and final rows

Constraint checks

Only one or two values change between the rows. Predict from the schema and current rows first.

orders · current child rows
idcustomer_idstatusordered_attotal_cents
5001101PAID2026-07-28 09:10:003200
5002102PENDING2026-07-28 10:20:007500
5003101SHIPPED2026-07-29 13:40:0012800
Review terms
schema
The structure that defines a table's columns, data types, keys, and constraints.
current rows
The values stored in the table under that schema right now.
relation / table
A relation in the relational model and its SQL table representation, whose rows share one column structure.
row
One item representing an entity or fact, with one value for each column.
column
An attribute with the same meaning and domain across every row.
candidate key
A minimal column set the schema declares can identify a row uniquely; coincidentally distinct sample values are not enough.
primary key
The candidate key selected as the row's primary identity.
foreign key
A constraint declaring that columns in one table reference a candidate or primary key in another.
referential integrity
The rule that keeps a foreign-key value connected to an allowed parent row.

Source boundary

The results apply this lesson's constraints to fixed fixture rows to explain relational concepts. For a deterministic explanation, the lesson checks the primary key before the foreign key. This is not a claim about one DB engine's error order or physical storage.

Complete lesson without JavaScript

The schema defines which columns and constraints customers and orders allow. Current rows are the values stored under that structure right now.

A candidate key is a minimal column set the schema declares can identify a row uniquely. This lesson selects customers.id and orders.id as primary keys; orders.customer_id references customers.id.

CREATE TABLE customers (
  id INTEGER NOT NULL,
  name VARCHAR(80) NOT NULL,
  country_code CHAR(2) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE orders (
  id INTEGER NOT NULL,
  customer_id INTEGER NOT NULL,
  status VARCHAR(20) NOT NULL,
  ordered_at TIMESTAMP NOT NULL,
  total_cents INTEGER NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

customers · parent rows

id name country_code
101 Mina KR
102 Noah US
103 Lina DE

orders · current child rows

id customer_id status ordered_at total_cents
5001101PAID2026-07-28 09:10:003200
5002102PENDING2026-07-28 10:20:007500
5003101SHIPPED2026-07-29 13:40:0012800

Results for the three proposed rows

  1. Reuse an existing order id

    orders.id already exists, so the PRIMARY KEY (id) constraint named orders.primary-key rejects the insert.

  2. Reference a missing customer

    No customers.id row matches orders.customer_id, so the FOREIGN KEY (customer_id) constraint named orders.customer-foreign-key rejects the insert.

  3. Connect a new order to an existing customer

    orders.id passes PRIMARY KEY (id) and orders.customer_id references an existing customers.id, so FOREIGN KEY (customer_id) also passes and the proposed row is added to orders.

The results apply this lesson's constraints to fixed fixture rows to explain relational concepts. For a deterministic explanation, the lesson checks the primary key before the foreign key. This is not a claim about one DB engine's error order or physical storage.

Next connection

FOUNDATION 02 · Constraints and relationships

The next Foundation connects NOT NULL, UNIQUE, CHECK, and relationship cardinality to these rows.

Planned