关系型数据库MySQL开发要点之多表设计案例详解代码实现

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介: 关系型数据库MySQL开发要点之多表设计案例详解代码实现

什么是多表设计

项目开发

在进行数据库表结构设计时 根据数据模型和业务关系

会根据业务需求和业务模块之间的关系分析设计表结构

由于业务之间互相关联 所以表结构之间也存在着各种联系

主要分为以下三种

一对多

每个部门下是有多个员工的

但是一个员工只能归属一个部门

完成部门表的设计

建表并添加数据模型

先创建员工表

CREATE TABLE tb_emp(
    id int unsigned primary key auto_increment comment 'ID',
    username varchar(20) not null unique comment '用户名',
    password varchar(32) default '123456' comment '密码',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别 1男 2女',
    image varchar(300) comment '图像',
    job tinyint unsigned comment '职位 1班主任 2讲师 3学工主管 4教研主任',
    entrydate date comment '入职时间',
    creat_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)comment '员工表';

再创建部门表

CREATE TABLE  tb_dept(
    id int unsigned primary key auto_increment comment 'ID',
    name varchar(10) not null comment '部门名称',
    creat_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)comment '部门表';

最后完善员工表信息

在员工表中关联部门的ID

完整代码

CREATE TABLE tb_emp(
    id int unsigned primary key auto_increment comment 'ID',
    username varchar(20) not null unique comment '用户名',
    password varchar(32) default '123456' comment '密码',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别 1男 2女',
    image varchar(300) comment '图像',
    job tinyint unsigned comment '职位 1班主任 2讲师 3学工主管 4教研主任',
    entrydate date comment '入职时间',
    dept_id int unsigned comment '归属的部门id',
    creat_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)comment '员工表';
 
CREATE TABLE  tb_dept(
    id int unsigned primary key auto_increment comment 'ID',
    name varchar(10) not null comment '部门名称',
    creat_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
)comment '部门表';

一一关联 父表 子表

测试

外键约束操作

我们在项目开发阶段 额外添加外键 直接在图形化界面中操作就行了

这样就好了

成功关联

我们在以后开发中应该使用逻辑外键

重点:一对一设计

案例 用户和身份证号之间的关系

一对一关系 多用于单表的拆分

将一张表的基础字段放在一张表中 其他字段放在另一张表里

以操作操作效率

如果在后端开发中我们遇到了对一对一的关系的反复查询的时候

我们建议使用一对一的关系建立数据库

一对一可以看成是一种特殊的一对多关系

代码实现

USE bigdate1421;
 
DROP TABLE tb_user;
DELETE  FROM tb_user;
 
DROP TABLE tb_user_card;
DELETE  FROM tb_user_card;
 
-- 一对一 用户和身份证
CREATE TABLE tb_user(
    id int unsigned primary key auto_increment comment 'ID',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别 1男 2女',
    phone char(11) comment '手机号',
    degree varchar(10) comment '学历'
) comment '用户信息表';
 
insert into tb_user values
    (1,'高畅',2,'111','大学'),
    (2,'栾增旭',1,'333','大学'),
    (3,'刘岩',1,'222','大学'),
    (4,'郑子烨',2,'444','大学'),
    (5,'于芯怡',2,'555','大学');
 
-- 第一张表式用户基本信息表 第二张表是用户身份信息表
CREATE TABLE tb_user_card(
    id int unsigned primary key auto_increment comment 'ID',
    nationality varchar(10) not null comment '民族',
    idcard char(18) not null comment '身份证号',
    expire_begin date not null comment '有限期限_开始',
    user_id int unsigned not null unique comment '用户ID',
    constraint fk_user_id foreign key (user_id) references tb_user(id)
)comment '用户身份信息表';
 
insert into tb_user_card values
    (1,'汉','111111111111111111','2024-05-07',1),
    (2,'汉','222222222222222222','2024-05-07',2),
    (3,'汉','333333333333333333','2024-05-07',3),
    (4,'汉','444444444444444444','2024-05-07',4),
    (5,'汉','555555555555555555','2024-05-07',5);

展示

重点:多对多设计

学生与老师的关系

一个学生可以有多个老师

一个老师也可以有多个学生

多对多时借助外键是很难实现的

我们要借助中间表来实现

代码实现

USE bigdate1421;
 
-- 多对多 学生与课程
 
-- 第一张表
create table tb_student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';
 
insert into tb_student(name, no) values
    ('高畅','5'),
    ('栾增旭','4'),
    ('刘岩','3'),
    ('郑子烨','2'),
    ('于芯怡','1');
 
-- 第二张表
create table tb_course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
)comment '课程表';
 
insert into tb_course (name) values
    ('Java'),
    ('Javascript'),
    ('golang'),
    ('python');
 
-- 第三张表 中间表
create table tb_student_course(
    id int auto_increment primary key comment '主键ID',
    student_id int not null comment '学生ID',
    course_id int not null comment '课程ID',
    constraint fk_courseid foreign key (course_id) references tb_course(id),
    constraint fk_studentid foreign key (student_id) references tb_student(id)
)comment '学生课程中间表';
 
insert into tb_student_course (student_id, course_id) values
    (1,1),
    (1,2),
    (1,3),
    (2,1),
    (2,2),
    (2,3),
    (2,4),
    (3,1),
    (3,3),
    (4,3),
    (4,4);

展示

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
5月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
434 158
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
996 152
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
806 156
|
5月前
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
401 156
|
5月前
|
Cloud Native 关系型数据库 MySQL
免费体验!高效实现自建 MySQL 数据库平滑迁移至 PolarDB-X
PolarDB-X 是阿里云推出的云原生分布式数据库,支持PB级存储扩展、高并发访问与数据强一致,助力企业实现MySQL平滑迁移。现已开放免费体验,点击即享高效、稳定的数据库升级方案。
免费体验!高效实现自建 MySQL 数据库平滑迁移至 PolarDB-X
|
5月前
|
存储 JSON 数据建模
鸿蒙 HarmonyOS NEXT端云一体化开发-云数据库篇
云数据库采用存储区、对象类型、对象三级结构,支持灵活的数据建模与权限管理,可通过AGC平台或本地项目初始化,实现数据的增删改查及端侧高效调用。
266 1
|
5月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
5月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
5月前
|
SQL 关系型数据库 MySQL
Mysql数据恢复—Mysql数据库delete删除后数据恢复案例
本地服务器,操作系统为windows server。服务器上部署mysql单实例,innodb引擎,独立表空间。未进行数据库备份,未开启binlog。 人为误操作使用Delete命令删除数据时未添加where子句,导致全表数据被删除。删除后未对该表进行任何操作。需要恢复误删除的数据。 在本案例中的mysql数据库未进行备份,也未开启binlog日志,无法直接还原数据库。
|
5月前
|
Ubuntu 安全 关系型数据库
安装与配置MySQL 8 on Ubuntu,包括权限授予、数据库备份及远程连接指南
以上步骤提供了在Ubuntu上从头开始设置、配置、授权、备份及恢复一个基础但完整的MySQL环境所需知识点。
571 7

推荐镜像

更多