Skip to main content

Relational Database Concepts

Relational databases work because they encourage a simple idea: store each kind of fact once, and connect facts through keys.

This section gives you the vocabulary and mental model you'll use throughout the rest of the course.

Core Concepts

The Relational Building Blocks

ConceptWhat it isThink of it as
TableA collection of similar thingsA list with a fixed set of fields
RowOne item in a tableOne record (one contact, one order)
ColumnOne field on every rowOne attribute (name, email)
Primary keyA unique identifier for a rowA stable ID you can reference
RelationshipA connection between tables"This order belongs to that customer"

A Relationship Example (Conceptual)

This ERD shows a one-to-many relationship: one contact can have many phone numbers.

erDiagram
CONTACT ||--o{ PHONE_NUMBER : has

CONTACT {
integer contact_id PK
text full_name
}

PHONE_NUMBER {
integer phone_id PK
integer contact_id FK
text phone
text label
}

Why Relationships Matter: Less Duplication

If you repeat the same "customer" information on every order row, you create update problems.

-- This result set shows a common duplication problem.
-- The same email appears on multiple order rows.
VALUES
(1001, 'ada@example.com', 'INV-001', 39.99),
(1001, 'ada@example.com', 'INV-002', 12.50);
column1column2column3column4
1001ada@example.comINV-00139.99
1001ada@example.comINV-00212.50

In a relational design, you'd typically store the customer once and reference them from orders.

customers.customer_idcustomers.email
1001ada@example.com
orders.invoice_noorders.customer_idorders.total
INV-001100139.99
INV-002100112.50

SQLite-Specific Nuances

SQLite Nuance

SQLite can enforce relationships (foreign keys), but enforcement depends on configuration.

You'll learn how to enable and use foreign keys safely in Foreign keys.

Common Pitfalls / Best Practices

Pitfall

Storing multiple values in one column (like "red,blue,green").

It looks convenient at first, but it makes searching, updating, and validating your data much harder.

Best Practice

Keep values atomic (one fact per column) and model repeating data with separate tables.

That is the foundation of clean joins, correct counts, and simpler updates.

Quick Challenge

You have two tables: authors and posts. One author can write many posts.

Which side is "one" and which side is "many"?

View Solution
  • authors is the "one" side
  • posts is the "many" side
erDiagram
AUTHOR ||--o{ POST : writes

AUTHOR {
integer author_id PK
text name
}

POST {
integer post_id PK
integer author_id FK
text title
}