Relational Model (Tables, Schemas, Constraints)
Deconstruct Edgar F. Codd's relational algebra: Relations (tables), Tuples (rows), Attributes (columns), Foreign Key constraints, and Referential Integrity.
Relational Schema Design & Referential Integrity Constraints π
Relational model entity relationships: Primary Keys (PK), Foreign Keys (FK), Unique Constraints (UK), Check Constraints, and Cascading Delete rules.
01.1. The Mathematical Foundation: Edgar F. Codd's Relational Model
Introduced by IBM researcher Edgar F. Codd in 1970, the Relational Model organizes data into mathematical relations based on first-order predicate logic and set theory:
- Relation (Table): A named set of ordered rows with identical column structures.
- Tuple (Row / Record): A single data entity containing an ordered collection of attribute values.
- Attribute (Column / Field): A named domain value with a strict data type (e.g.,
INTEGER,VARCHAR(255),DECIMAL(10,2),TIMESTAMP). - Schema: The formal, declarative contract defining tables, columns, data types, indexes, and integrity constraints.
Why it won: Unlike older hierarchical or network databases that required procedural code to navigate hardcoded pointers, the relational model separates the logical data structure from the physical storage implementation, allowing declarative querying via SQL.
02.2. The 4 Essential Integrity Constraints
A relational database engine actively enforces four categories of data integrity constraints on every write operation:
- Entity Integrity (Primary Key Constraint): Every table must have a Primary Key (PK) containing unique, non-null values that uniquely identify every individual row.
- Referential Integrity (Foreign Key Constraint): A Foreign Key (FK) links a column in Table A to the Primary Key of Table B. The database prohibits inserting a row in Table A with an invalid FK referencing a non-existent row in Table B.
- Domain Integrity (Check Constraints & Data Types): Restricts column values to valid types and ranges (e.g.,
CHECK (age >= 18),CHECK (status IN ('PENDING', 'PAID'))). - User-Defined Integrity (Unique Constraints & Triggers): Enforces business rules (e.g.,
UNIQUE(email)ensures no two users share an email address).
03.3. Foreign Key Cascading Actions (`ON DELETE` / `ON UPDATE`)
When a parent row is deleted, what happens to child rows referencing it?
ON DELETE RESTRICT / NO ACTION(Default): The database throws an error and prohibits deleting the parent if child rows exist (e.g., cannot delete aProductif historicOrderItemsreference it).ON DELETE CASCADE: Deleting the parent automatically deletes all child rows (e.g., deleting anOrderautomatically deletes its childOrderItems).ON DELETE SET NULL: Deleting the parent sets the child's FK column toNULL(e.g., deleting aDepartmentsets employeedepartment_idtoNULL).
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
total_amount DECIMAL(12, 2) NOT NULL CHECK (total_amount >= 0),
status VARCHAR(32) NOT NULL CHECK (status IN ('PENDING', 'PAID', 'CANCELLED')),
CONSTRAINT fk_user FOREIGN KEY (user_id)
REFERENCES users(id) ON DELETE RESTRICT
);
-- Crucial: Always create explicit index on Foreign Key columns!
CREATE INDEX idx_orders_user_id ON orders(user_id);βοΈArchitectural Trade-offs & Production Realities
Architectural Advantages
- Strict schema enforcement prevents bad or corrupted data from ever entering the database.
- Declarative SQL allows expressive relational joins across multiple tables.
- Referential integrity eliminates orphaned child records.
Trade-offs & Constraints
- Schema migrations on massive tables (100M+ rows) require careful online migration tooling (gh-ost / pt-online-schema-change) to avoid locking tables.
- Rigid schemas make dynamic polymorphic data modeling more complex (though modern `JSONB` columns in PostgreSQL bridge this gap).
Stripe uses relational databases with strict foreign key constraints and check constraints to manage financial balances, ensuring that double-entry accounting transactions can never point to non-existent accounts or record negative transaction fees.
π― Staff+ Engineering Takeaways
- Relational model structures data into mathematical tables (relations), rows (tuples), and typed columns (attributes).
- Primary Keys guarantee row uniqueness; Foreign Keys guarantee referential integrity.
- Check constraints enforce business rules at the database engine layer.
- Always create explicit database indexes on Foreign Key columns.
Topic Knowledge Assessment π§
Step through 2 scenario questions to test your staff-level grasp.
What happens when you delete a row from a parent table if the child table specifies "ON DELETE RESTRICT"?
How clear and staff-actionable was this system breakdown?