当前位置:网站首页>MySQL --- 多表查询 - 表与表之间的关系
MySQL --- 多表查询 - 表与表之间的关系
2022-07-16 18:43:00 【小雪菜本菜】
多表关系
在进行项目开发过程中,在进行数据库表结构设计的时候,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着不同的联系,基本上分为三种:
一对多 (多对一、一个对应多个的关系,树状模型)
多对多 (网状模型)
一对一
一对多(多对一)
案例:部门与员工的关系
关系:一个部门对应多个员工,一个员工只能对应一个部门,一个班级对应多个学生,一个学生只能对应一个班级
实现:在多的一方建立外键,关联另一方的主键
员工表有很多数据,部门的编号、员工的姓名、员工的职位,都是员工的信息,最后一列是员工的部门编号 deptno,对于部门的信息,单独把部门表存储到另一个表里面,而不是存储在员工表的最后,分开之后可以减少数据冗余的问题,可以根据部门编号在部门表里面找到对应的信息
外键:另外一个表的主键

多对多
案例:学生与课程的关系
关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择
实现:建立第三张中间表,中间表至少包含两个外键(包含另外两个表的 id,studentid 与 courseid),分别关联两方主键
左边是学生表,学生表对应一些学生的信息,例如学生的 id,课程表也有一个课程 id,课程表和学生此时还没有建立关系,当我们的学生选择了课程之后,如果之间在学生的字段后面加上课程 id,由于学生要选择很多门课程,需要很多列用于存储课程的信息,不方便存储,引入一个中间表,只需要两个字段即可,第一列是学生 id,也就是黛绮丝,既选择了 java 课程也选择了 php 课程,通过 id 在学生课程关系表里面找到学生选择的课程,然后在课程表里面找到具体的课程信息

一对一
案例:用户与用户详细的关系
如果信息过多,表太长,对于查询来说过于复杂
关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率。

实现:在任意一方加入一个外键,关联另一方的主键,并且设置外键为唯一约束
将基本信息作为一个 user 表,详细信息放到另外一个表,通过用户的 id 表示这两个表之间的关系

边栏推荐
- Recommend a well written article on "I2C protocol explanation"
- R language uses data The table package sorts the dataframe row data (sort the data rows based on multiple fields and variables without reordering the actual data changes), and calculates the cumulativ
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping quantile of dataframe data
- Unity-2d pixel lattice ablation
- 浅谈数组方法重构再封装-forEach-Map——push(),unshift(),shift(),Map(),filter(),every(),some(), reduce()
- Hystrix 部署
- I2C通信协议实现在OLED显示屏数据显示
- R language uses the designtreatmentsc function of vtreat package to build a data processing plan, and uses vtreat package for data preparation
- About SQL: orcale SQL, why is the foreign key inventory ID statement of the merchant table invalid
- Tableau JDBC连接GraphDB
猜你喜欢
随机推荐
VMware recovery snapshot failed to create an anonymous paging file of 5040 MB: insufficient system resources to complete the requested service
Find out the motivation and needs of enterprise location, and carry out investment attraction work efficiently
Sword finger offer 55 - ii balanced binary tree
Discussion on ble Bluetooth battery service
OpenCV 教程 02: OpenCV 的核心操作
Apache log related
VirtualBox virtual machine failed to start, e_ FAIL( 0x80004005)
R language ggplot2 visualization: use the gghistogram function of ggpubr package to visualize the histogram, use the add parameter to add the mean dotted line, vertical line and horizontal axis to the
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型
putchar()
js 加快播放视频速度
Dynamically adding routes and refreshing the page will show a blank screen
MySQL查看数据库性能常用命令
Introduction to sap appgyver
Unity-2d pixel lattice ablation
Hystrix 部署
[cache] introduction of a new cache caffeine cache
R language uses LM function to build multiple regression model, writes regression equation according to model coefficient, and uses summary function to calculate the summary statistical information of
VirtualBox虚拟机无法启动,E_FAIL( 0x80004005)
[try to hack] NTLM and LM Foundation








