Relationships — Complete Guide
Relationships — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 10 of 100
Relationships
Basics → Advanced
Basics · 1 — SQL · ~6 min · MySQL — Foundations
What is this?
Relationships link tables: one customer has many orders; one order has many order_items. You model this with foreign keys and join queries. Cardinality is one-to-many or many-to-many (via a bridge table).
Why should you care?
Storing customer name inside every order row duplicates data and goes stale when someone updates their profile.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
-- One customer, many orders (conceptual link)
SELECT c.full_name, o.order_ref, o.total_inr
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id = 1;
What happened?
- JOIN matches orders.customer_id to customers.customer_id.
- You get name beside each order without copying name into orders.
- Filtering by customer_id shows one person’s purchase history.
Practice next
- Insert 2 customers and 3 orders pointing at them.
- Run the JOIN query; verify names align.
- Draw customer → orders on paper with PK/FK labels.
- LEFT JOIN instead of INNER to include customers with zero orders.
- Count orders per customer with GROUP BY customer_id.
Remember
FK columns point to parent primary keys. JOIN reads related rows together. Normalize to avoid duplicate strings.
DataFlow order history screen
App loads customer name once and lists orders via JOIN — not N+1 name lookups.
Outcome: Profile page stays fast and consistent.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!