MySQL database - multi-table query (1) - multi-table relationships (one-to-many, many-to-many, one-to-one), overview of multi-table queries (concepts, Cartesian product, classification)

Table of contents

Overview

Multiple table relationship

One-to-many (many-to-one)

many to many

One to one

Multi-table query overview

concept

Cartesian Product

Classification


Multi-table query

  • Multiple table relationship
  • Multi-table query overview
  • inner join
  • outer join
  • self-connection
  • subquery
  • Multi-table query case

Overview

During project development, when designing the database table structure, the table structure will be analyzed and designed based on business needs and the relationship between business modules. Since businesses are interrelated, there are various connections between each table structure. Basically there are three types:

  • One-to-many (many-to-one)
  • many to many
  • One to one

Multiple table relationship

One-to-many (many-to-one)

  • Case: Relationship between departments and employees
  • Relationship: One department corresponds to multiple employees, and one employee corresponds to one department.
  • Implementation: Create a foreign key on the many side, pointing to the primary key of the one side

Just like the example of constraints shown in the previous article.

many to many

  • Case: Relationship between students and courses
  • Relationship: A student can take multiple courses, and a course can be chosen by multiple students.
  • Implementation: Create a third intermediate table. The intermediate table contains at least two foreign keys, which are respectively associated with the primary keys of the two parties.

Create a table to demonstrate many-to-many relationships

create table student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
)comment '学生表';
insert into student (name,no)
    values ('黛绮丝','2000100101'),('谢逊','2000100102'),('殷天正','2000100103'),('韦一笑','2000100104');

create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
)comment '课程表';
insert into course (name)
    values ('Java'),('PHP'),('MySQL'),('Hadoop');

create table student_course(
    id int auto_increment primary key  comment '主键',
    studentid int not null comment '学生ID',
    courseid int not null  comment '课程ID',
    constraint fk_courseid foreign key (courseid) references  course (id),
    constraint fk_student foreign key  (studentid) references  student (id)
)comment '学生课程中间表';
insert into student_course (studentid, courseid)
    values (1,1),(1,2),(1,3),(2,2),(2,3),(3,4);

Next, you can look at the relationship between tables:

One to one

  • Case: Relationship between users and user details
  • Relationship: one-to-one relationship, mostly used for single table splitting. The basic fields of one table are placed in one table, and other detailed fields are placed in another table to improve operational efficiency.
  • Implementation: Add a foreign key to either party, associate the primary key of the other party, and set the foreign key to be unique (unique)
create table tb_user(
    id int auto_increment primary key  comment '主键ID',
    name varchar(10) comment '姓名',
    age int comment '年龄',
    gender char(1) comment '1:男,2:女',
    phone char(11) comment '手机号'
)comment '用户基本信息表';

create table tb_user_edu(
    id int auto_increment primary key comment '主键ID',
    degree varchar(20) comment'学历',
    major varchar(50) comment '专业',
    primaryschool varchar(50) comment '小学',
    middleschool varchar(50) comment '中学',
    university varchar(50) comment '大学',
    userid int unique comment '用户ID',
    constraint fk_userid foreign key (userid) references tb_user (id)
)comment '用户教育信息表';

insert into tb_user(id, name, age, gender, phone)
    values (null, '黄渤' ,45, '1', '1800001111'),
           (null, '冰冰' ,35, '2' , '1800002222'),
           (null, '码云',55, '1', '1800008888'),
           (null, '李彦宏' ,50, '1', '1800009999');

insert into tb_user_edu (id, degree, major, primaryschool, middleschool, university, userid)
    values (null,'本科','舞蹈','静安区第一小学', '静安区第-中学', '北京舞蹈学院',1),
           (null, '硕士','表演','朝阳区第小学','朝阳区第一中学', '北京电影学院' ,2),
           (null,'本科','英语','杭州市第小学','杭州市第一中学','杭州师范大学',3),
           (null, '本科','应用数学','阳泉第一小学','阳泉区第一中学','清华大学',4);

Multi-table query overview

concept

Refers to querying data from multiple tables.

For example, to query these two tables:

To start, we can use a statement like this:

select * from emp,dept;

Then I found that the query result was 25 pieces of data

Obviously, this is not the result we want. This situation occurs because there will be invalid Cartesian products when performing multi-table queries, and we need to eliminate invalid Cartesian products.

Cartesian Product

Cartesian product refers to all combinations of two sets A and B in mathematics.

For example, if A, B and 1, 2, 3, 4 are combined, there are eight different combinations:

  • A 1
  • A 2
  • A 3
  • A 4
  • B 1
  • B 2
  • B 3
  • B 4

Therefore, when querying multiple tables, it is necessary to eliminate invalid Cartesian products.

select * from emp,dept where emp.dept_id = dept.id;

Then we can find the results we want.

Classification

1. Connection query

  • Inner join: equivalent to querying the intersection data of A and B
  • Outer join:

                Left outer join: Query all data in the left table and some data at the intersection of the two tables

                Right outer join: Query all data in the right table and some data at the intersection of the two tables

  • Self-join: current connection query with itself, self-join must use table alias

2. Subquery

Only a brief understanding in the overview


end


Learn from: Dark Horse Programmer - MySQL Database Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/132390344