Sling Academy
Home/MySQL/One-to-Many Relationship in MySQL 8: A Practical Guide

One-to-Many Relationship in MySQL 8: A Practical Guide

Last updated: January 31, 2024

Introduction

In database systems, a one-to-many relationship is a type of cardinality that refers to the relationship between two entities (tables) where one of the entities can have many related entities. In practical terms, consider the example where one author can write many books, but each book is written by only one author. The author to books relationship is a classic example of a one-to-many relationship.

Creating Tables with One-to-Many Relationships

To illustrate the one-to-many relationship, we’ll go through an example using MySQL 8. We will create two tables: authors and books. The authors table will have a primary key called id, and the books table will have a foreign key called author_id referring to the authors table.

CREATE TABLE authors (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

CREATE TABLE books (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    published_date DATE,
    author_id INT,
    FOREIGN KEY (author_id) REFERENCES authors(id)
);

Note: It is vital to ensure that the data type of the author_id column in the books table matches the data type of the id column that it refers to in the authors table.

Adding Data to the Tables

With the tables created, we can now insert some data.

INSERT INTO authors (name) VALUES ('J.K. Rowling'), ('George R.R. Martin'), ('Leo Tolstoy');

INSERT INTO books (title, published_date, author_id) VALUES ('Harry Potter and the Philosopher's Stone', '1997-06-26', 1),
   ('A Game of Thrones', '1996-08-06', 2),
   ('War and Peace', '1869-01-01', 3),
   ('Harry Potter and the Chamber of Secrets', '1998-07-02', 1);

This inserts data into both tables and associates the books with authors using the author_id.

Querying Data with Joins

To retrieve the data that shows the relationships between the tables, you can use a JOIN statement.

SELECT
    authors.name AS author_name,
    books.title AS book_title,
    books.published_date
FROM
    books
JOIN
    authors ON books.author_id = authors.id;

The query result will display a list of books with the names of their authors and publication dates.

Handling Orphaned Rows

Sometimes you may have books that do not have a corresponding author due to various reasons, such as deletions. To protect against this, you can use the ON DELETE CASCADE clause when defining the foreign key.

ALTER TABLE books
ADD CONSTRAINT FK_Author
FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE;

With this clause, deleting an author will automatically delete all associated books, preventing orphaned rows.

Conclusion

Understanding and properly implementing one-to-many relationships in MySQL is crucial for database integrity and performance. Always remember to consider referential integrity and potential orphan records when designing your database. With this guide, you should now be able to confidently work with this type of relationship in MySQL 8.

Next Article: Working with FOREIGN KEY in MySQL 8: A Developer’s Guide

Previous Article: Understanding CHECK constraint in MySQL 8: A Practical Guide

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples
  • Right Join in MySQL 8: A Practical Guide