Detailed explanation of three relationship examples of MySQL foreign keys

Detailed explanation of three relationship examples of MySQL foreign keys

This article uses examples to describe the three relationships of MySQL foreign keys. Share with you for your reference, the details are as follows:

Because of the foreign key constraint, the two tables form three types of relationships:

  • Many to One
  • Many-to-Many
  • One-to-one

One-to-many or many-to-one

Many to One

create table press(
  id int primary key auto_increment,
  name varchar(20)
);
create table book(
  id int primary key auto_increment,
  name varchar(20),
  press_id int not null,
     constraint fk_book_press foreign key(press_id) references press(id)
  on delete cascade
  on update cascade
);

# First insert records into the associated table insert into press(name) values
('Beijing Industrial Mine Press'),
('People's Music is Not Good to Listen to Publishing House'),
('Intellectual property is useless to publishers')
;
# Insert records into the associated table insert into book(name,press_id) values
('Nine Yang Magic',1),
('Nine Yin Manual',2),
('Nine Yin White Bone Claw',2),
('Dugu Jiujian',3),
('Ten Slaps to Subdue the Dragon', 2),
('Sunflower Collection',3)
;

Query results:

mysql> select * from book;
+----+-----------------+----------+
| id | name | press_id |
+----+-----------------+----------+
| 1 | Nine Yang Magic Skill | 1 |
| 2 | Nine Yin Manual | 2 |
| 3 | Nine Yin Bone Claw | 2 |
| 4 | Dugu Jiujian | 3 |
| 5 | Ten Slaps of the Dragon | 2 |
| 6 | Sunflower Manual | 3 |
+----+-----------------+----------+
rows in set (0.00 sec)
mysql> select * from press;
+----+--------------------------------+
| id | name |
+----+--------------------------------+
| 1 | Beijing Industrial Mine Press |
| 2 | People's Music Publishing House |
| 3 | Intellectual property is useless |
+----+--------------------------------+
rows in set (0.00 sec)

Many-to-many, introducing a third table

Many-to-Many

# Create the associated table author table. The previous book table has been created in the many-to-one relationship. create table author(
  id int primary key auto_increment,
  name varchar(20)
);
#This table stores the relationship between the author table and the book table. To query the relationship between the two, just query this table. create table author2book(
  id int not null unique auto_increment,
  author_id int not null,
  book_id int not null,
  constraint fk_author foreign key(author_id) references author(id)
  on delete cascade
  on update cascade,
  constraint fk_book foreign key(book_id) references book(id)
  on delete cascade
  on update cascade,
  primary key(author_id,book_id)
);
#Insert four authors, with their IDs arranged in order insert into author(name) values('egon'),('alex'),('wusir'),('yuanhao');
# Representative works of each author egon: Nine Yang Magic Art, Nine Yin Manual, Nine Yin Bones Claw, Dugu Jiujian, Ten Palms of the Dragon Subduing, Sunflower Collection alex: Nine Yang Magic Art, Sunflower Collection wusir: Dugu Jiujian, Ten Palms of the Dragon Subduing, Sunflower Collection yuanhao: Nine Yang Magic Art # Insert corresponding data into author2book table insert into author2book(author_id,book_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(2,1),
(2,6),
(3,4),
(3,5),
(3,6),
(4,1)
;

# Now you can check the relationship between the author and the book corresponding to author2bookmysql> select * from author2book;
+----+-----------+---------+
| id | author_id | book_id |
+----+-----------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 5 |
| 6 | 1 | 6 |
| 7 | 2 | 1 |
| 8 | 2 | 6 |
| 9 | 3 | 4 |
| 10 | 3 | 5 |
| 11 | 3 | 6 |
| 12 | 4 | 1 |
+----+-----------+---------+
rows in set (0.00 sec)

One-to-one situation

One-to-one

#For example: A user can only register one blog #Two tables: user table (user) and blog table (blog)
# Create a user table create table user(
  id int primary key auto_increment,
  name varchar(20)
);
# Create a blog table create table blog(
  id int primary key auto_increment,
  url varchar(100),
  user_id int unique,
  constraint fk_user foreign key(user_id) references user(id)
  on delete cascade
  on update cascade
);
#Insert records into the user table insert into user(name) values
('alex'),
('wusir'),
('egon'),
('xiaoma')
;
#Insert records into blog table insert into blog(url,user_id) values
('http://www.cnblog/alex',1),
('http://www.cnblog/wusir',2),
('http://www.cnblog/egon',3),
('http://www.cnblog/xiaoma',4)
;
# Query wusir's blog address select url from blog where user_id=2;

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Detailed explanation of the basic functions and usage of MySQL foreign keys
  • Optimization of data tables in MySQL database, analysis of foreign keys and usage of three paradigms
  • Detailed explanation of creating a data table in MySQL and establishing primary and foreign key relationships
  • Examples of common operations on MySQL foreign key constraints [view, add, modify, delete]
  • Reasons and solutions for MySQL failing to create foreign keys
  • Introduction to MySQL method of deleting table data with foreign key constraints
  • Simple implementation of ignoring foreign key constraints when deleting MySQL tables
  • MySQL adds a foreign key error: 1215 Cannot add the foreign key constraint solution
  • How to use foreign keys in MySQL to implement cascade delete and update
  • Mysql table creation foreign key error solution

<<:  Vue recursively implements custom tree components

>>:  Detailed explanation of the time representation example of the Linux time subsystem

Recommend

Index in MySQL

Preface Let's get straight to the point. The ...

Native JS to implement hover drop-down menu

JS implements a hover drop-down menu. This is a s...

How to install nginx on win10

Because the company asked me to build a WebServic...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

mysql5.7 remote access settings

Setting up remote access in mysql5.7 is not like ...

MySQL Best Practices: Basic Types of Partition Tables

Overview of MySQL Partitioned Tables As MySQL bec...

Implementation of MYSQL (telephone number, ID card) data desensitization

1. Data desensitization explanation In daily deve...

Detailed explanation of the principle of js Proxy

Table of contents What is Proxy Mode? Introducing...

MySQL 5.7.18 zip version installation tutorial

The mysql 5.7.18 zip version of MySQL is not like...

Detailed explanation of Nginx current limiting configuration

This article uses examples to explain the Nginx c...

Detailed explanation of MySQL solution to USE DB congestion

When we encounter a fault, we often think about h...