One-to-Many (1:N) Relationships
One-to-Many (1:N) Relationships: The Backbone of Relational Modeling
The One-to-Many (1:N) relationship is the single most ubiquitous structural pattern in relational database design. A department employs many workers, a customer places many orders, a blog author writes many posts, and a class has many students. In every case, a single record in the Parent Table correlates to zero, one, or hundreds of records in the Child Table.
1. The Cardinal Rule of 1:N Relationships
2. DDL Implementation with Referential Actions
Let us build a complete e-commerce relationship: Customers (1) -> Orders (N):
3. Querying 1:N Relationships
Joining Parent and Child:
Aggregating across the 1:N Boundary:
4. Identifying Orphaned Child Rows
In poorly maintained legacy databases where foreign keys were omitted, child tables frequently contain orphaned records (child rows pointing to non-existent parent IDs). You can detect them using a LEFT JOIN:
5. Best Practices & Common Pitfalls
- Always Index the Foreign Key: While MySQL automatically indexes foreign keys in child tables, ensure composite indexes align with your query filters (e.g.,
INDEX idx_cust_date (customer_id, order_date)). - Careful with RESTRICT vs CASCADE: While
ON DELETE CASCADEis convenient, in accounting and billing, deleting a customer must never delete historical financial transactions. Always useON DELETE RESTRICTfor financial records.
Multiple Choice Questions
1. In a One-to-Many (1:N) relationship between authors and books, which table must contain the Foreign Key column?
A. The authors table B. The books table (the "Many" side) C. A third junction table D. Neither table Answer: B Explanation: In a 1:N relationship, the foreign key always resides on the "Many" side (books), storing the primary key of the "One" side (author).
2. What referential action prevents an administrator from deleting a customer if that customer has existing order records?
A. ON DELETE CASCADE B. ON DELETE SET NULL C. ON DELETE RESTRICT D. ON DELETE IGNORE Answer: C Explanation: ON DELETE RESTRICT (or NO ACTION) actively halts and rejects the deletion of a parent record as long as associated child records exist.
3. What is an "orphaned" row in a child table?
A. A row that contains encrypted text B. A child record whose foreign key references a parent ID that no longer exists in the parent table C. A row with a null primary key D. A row that was created more than 10 years ago Answer: B Explanation: An orphaned row is a child record pointing to a non-existent parent, caused by missing foreign key constraints or uncoordinated manual deletions.
4. Which query pattern detects orphaned records in a child table orders that has no matching customers parent?
A. orders JOIN customers ON orders.id = customers.id B. orders LEFT JOIN customers ON orders.customer_id = customers.customer_id WHERE customers.customer_id IS NULL C. orders CROSS JOIN customers D. SELECT FROM orders WHERE customer_id = 0 Answer: B Explanation:* A LEFT JOIN paired with WHERE parent.id IS NULL isolates child records that failed to match any parent record.
5. Why should aggregate queries joining a 1:N relationship (e.g. Customers to Orders) use LEFT JOIN instead of INNER JOIN when generating customer reports?
A. LEFT JOIN runs 10x faster B. To ensure customers who have placed zero orders are still included in the report with a count of 0 C. INNER JOIN deletes unmatched customers D. LEFT JOIN eliminates duplicates automatically Answer: B Explanation: An INNER JOIN would silently omit customers with zero orders; a LEFT JOIN retains them so COUNT(order_id) reports 0.
Many-to-Many (M:N) Relationships & Junction Tables
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| One-to-One (1:1) Relationships | Many-to-Many (M:N) Relationships & Junction Tables |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.