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

Updated: January 31, 2024 By: Guest Contributor Post a comment

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.