The Relational Model, Tables & Keys 🔑
In 1970, an Oxford-educated computer scientist at IBM named Edgar F. Codd published a landmark research paper that changed computing forever:
“A Relational Model of Data for Large Shared Data Banks.”
Before Codd, databases were complex "hierarchical" or "network" webs of physical disk pointers. If you altered how a disk file was formatted, every single computer program querying it would break!
Codd introduced the Relational Model: an elegant, mathematical abstraction based on set theory where:
- Data is presented as simple two-dimensional Tables (relations).
- Tables are connected to each other using Keys instead of hardcoded disk pointers.
Let's dissect how this model works in practice.
1. Anatomy of a Database Table 📊
In a relational database, data is organized into Tables (also referred to mathematically as Relations).
A table is a grid made of:
- Columns (Attributes / Fields): Vertical structures that define what kind of data is stored (e.g.,
id,name,email,created_at). Every column has a strict data type. - Rows (Records / Tuples): Horizontal entries representing one single, distinct real-world entity (e.g., one specific student or one specific transaction).
Table: students
┌────────────┬──────────────────┬──────────────────────┬─────────────┐
│ student_id │ full_name │ email │ enrolled_at │ <-- Column Headers (Attributes)
├────────────┼──────────────────┼──────────────────────┼─────────────┤
│ 1 │ Aarav Sharma │ aarav@example.com │ 2026-08-15 │ <-- Row 1 (Record/Tuple)
│ 2 │ Rahul Sharma │ rahul@example.com │ 2026-08-16 │ <-- Row 2 (Record/Tuple)
│ 3 │ Ananya Patel │ ananya@example.com │ 2026-08-17 │ <-- Row 3 (Record/Tuple)
└────────────┴──────────────────┴──────────────────────┴─────────────┘
▲
└─ Atomic Values (Cells)
Key Properties of a Relational Table:
- Atomic Values: Each cell should hold only one atomic piece of data (never pack multiple values like
"Aarav, Priya, Rahul"into a single cell). - Column Consistency: Every value in a column must adhere to that column's defined data type (e.g., you cannot put the text
"FREE"in anINTEGERprice column). - Row Uniqueness: No two rows should be completely indistinguishable duplicates.
2. Common SQL Data Types 🏷️
When designing a table, every column must be assigned a data type so the DBMS knows how much disk space to allocate and how to validate inputs:
| Data Type | Description | Common Use Case |
|---|---|---|
INT / INTEGER | Whole numbers (positive or negative) without decimals | IDs, quantities, age |
BIGINT | Very large integers (up to ) | Financial transactions, high-scale IDs |
DECIMAL(p, s) / NUMERIC | Exact fixed-point numbers with precision and scale | Money, currency balance (DECIMAL(10, 2)) |
VARCHAR(n) | Variable-length character string up to characters | Names, emails, titles (VARCHAR(100)) |
TEXT | Unlimited length text string | Blog post body, comments, bio |
BOOLEAN | True or False | is_active, email_verified |
DATE | Calendar date (YYYY-MM-DD) | Birthdays, joining date |
TIMESTAMP | Date and time down to milliseconds | created_at, last_login_at |
3. The Primary Key (PK): Your Unique Fingerprint 🆔
Imagine a college with 5,000 students. Two students could easily share the exact same name: "Rahul Sharma". They might even have the same birth date!
How does the database tell them apart?
A Primary Key (PK) is a column (or combination of columns) that uniquely identifies each row in a table.
Strict Primary Key Rules:
- Uniqueness: No two rows can ever have the same Primary Key value.
- NOT NULL: A Primary Key can never be empty or
NULL. - Immutability: Once assigned, a Primary Key value should ideally never change throughout its lifecycle.
-- Example Table Definition with a Primary KeyCREATE TABLE students ( student_id SERIAL PRIMARY KEY, -- Auto-incrementing unique integer full_name VARCHAR(100) NOT NULL, email VARCHAR(150) UNIQUE NOT NULL);4. The Foreign Key (FK): Linking Tables Together 🔗
Recall our bookstore example from the previous topic. Instead of stuffing customer details into the orders table, we split the data into two separate, clean tables:
- A
customerstable (stores customer information once). - An
orderstable (stores orders, referencing which customer bought them).
How do we link an order to its customer? Using a Foreign Key!
A Foreign Key (FK) is a column in one table that points directly to the Primary Key of another table.
TABLE: customers TABLE: orders
┌────────────┬─────────────┐ ┌──────────┬─────────────┬────────┐
│ id (PK) │ name │ │ order_id │ customer_id │ amount │
├────────────┼─────────────┤ ├──────────┼─────────────┼────────┤
│ 1 │ Aarav Sharma│ ◀───────┐ │ 501 │ 1 │ 450 │
│ 2 │ Rahul Verma │ └───┼──────────┼─────────────┼────────┤
└────────────┴─────────────┘ │ 502 │ 1 │ 1200 │
├──────────┼─────────────┼────────┤
│ 503 │ 2 │ 350 │
└──────────┴─────────────┴────────┘
▲
Foreign Key (FK)
Points to customers.id (PK)
In SQL:
CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, customer_id INT REFERENCES customers(id), -- Foreign Key constraint! amount DECIMAL(10, 2) NOT NULL);Referential Integrity: The Anti-Corruption Shield 🛡️
When you declare a Foreign Key constraint, the database engine enforces Referential Integrity:
- If someone tries to insert an order with
customer_id = 999, but customer 999 does not exist incustomers, the DBMS immediately rejects the insert with an error. - If someone tries to delete
Aarav Sharmafromcustomerswhile active orders reference herid, the DBMS blocks the deletion to prevent orphaned data!
5. Types of Table Relationships 🤝
By connecting Primary Keys and Foreign Keys, relational databases represent three primary relationship patterns:
1. One-to-One (1:1) 2. One-to-Many (1:N) 3. Many-to-Many (N:M)
┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌───────┐ ┌────────┐
│ User │────│ Profile│ │Customer│───<│ Order │ │ Student├──<│Enroll │>──┤ Course │
└────────┘ └────────┘ └────────┘ └────────┘ └────────┘ └───────┘ └────────┘
(Junction
Table)
- One-to-One (1:1): Each user has exactly one private profile settings row.
- One-to-Many (1:N): One customer can place many orders; each order belongs to only one customer (the most common pattern in software!).
- Many-to-Many (N:M): A student can enroll in multiple courses, and a course has multiple students. This is implemented using a middle Junction Table (bridge table) with two foreign keys.
Flashcard Checkpoint 🎴
What are the two non-negotiable rules of any Primary Key column?
2. NOT NULL: The value can never be empty or missing.
What is the purpose of a Foreign Key constraint in relational databases?