...
Sign in

The Relational Model, Tables & Keys

Master the building blocks of relational databases: tables, rows, columns, data types, primary keys, and foreign key relationships with visual diagrams.

Share:

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:

  1. Data is presented as simple two-dimensional Tables (relations).
  2. 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:

  1. Atomic Values: Each cell should hold only one atomic piece of data (never pack multiple values like "Aarav, Priya, Rahul" into a single cell).
  2. 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 an INTEGER price column).
  3. 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 TypeDescriptionCommon Use Case
INT / INTEGERWhole numbers (positive or negative) without decimalsIDs, quantities, age
BIGINTVery large integers (up to ±9×1018\pm 9 \times 10^{18})Financial transactions, high-scale IDs
DECIMAL(p, s) / NUMERICExact fixed-point numbers with precision pp and scale ssMoney, currency balance (DECIMAL(10, 2))
VARCHAR(n)Variable-length character string up to nn charactersNames, emails, titles (VARCHAR(100))
TEXTUnlimited length text stringBlog post body, comments, bio
BOOLEANTrue or Falseis_active, email_verified
DATECalendar date (YYYY-MM-DD)Birthdays, joining date
TIMESTAMPDate and time down to millisecondscreated_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:

  1. Uniqueness: No two rows can ever have the same Primary Key value.
  2. NOT NULL: A Primary Key can never be empty or NULL.
  3. 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:

  1. A customers table (stores customer information once).
  2. An orders table (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 in customers, the DBMS immediately rejects the insert with an error.
  • If someone tries to delete Aarav Sharma from customers while active orders reference her id, 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)
  1. One-to-One (1:1): Each user has exactly one private profile settings row.
  2. One-to-Many (1:N): One customer can place many orders; each order belongs to only one customer (the most common pattern in software!).
  3. 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 🎴

Active Recall
Card

What are the two non-negotiable rules of any Primary Key column?

Click or Press Space to Flip
Key Takeaway
1. Uniqueness: Every single row must have a distinct value (no duplicates).
2. NOT NULL: The value can never be empty or missing.
Click to Flip Back
Active Recall
Card

What is the purpose of a Foreign Key constraint in relational databases?

Click or Press Space to Flip
Key Takeaway
A Foreign Key references the Primary Key of another table, ensuring Referential Integrity—the database prevents inserting invalid parent references or accidentally deleting parent records that have child rows.
Click to Flip Back

Knowledge Check 🧠

CheckpointCheckpoint
Question 1 of 2Score: 0/2

Why is storing monetary prices as FLOAT or DOUBLE considered bad practice in SQL?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!