菜鸟之路day31一一MySQL之多表设计

菜鸟之路day31一一MySQL之多表设计

作者:blue

时间:2025.5.9

文章目录

0.概述

内容学习自黑马程序员BV1m84y1w7Tb

一.多表设计

1.1一对多

一对多关系实现:在数据库表中多的一方,添加字段,来关联一的一方的主键

例子:一个部门对应多个员工,设计部门表如下

sql 复制代码
-- 部门
create table tb_dept (
    id int unsigned primary key auto_increment comment 'ID',
    name varchar(10) not null unique comment '部门名称',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
) comment '部门表';

为员工表新添加'部门ID'字段,关联部门表

sql 复制代码
alter table tb_emp
    add dept_id int unsigned null comment '部门ID';

注意这样简单的添加两张表之间的联系,只是在逻辑层面添加了两张表的联系

在数据库层面并没有物理层面上的联系

我们可以通过添加外键的方式为两张表添加物理层面上的联系,使两张表是存在实际关联的

在平时开发过程中,更常使用的是逻辑外键

1.2一对一

案例:用户与身份证信息的关系

关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他字段放在另一张表中,以提升操作效率

实现:在任意一方加入外键,惯量另一方的主键,并且设置外键为唯一的(UNIQUE)

示例:

sql 复制代码
-- 创建学生表
CREATE TABLE students (
       student_id INT PRIMARY KEY AUTO_INCREMENT,
       name VARCHAR(50) NOT NULL,
       age INT,
       gender INT,
       email VARCHAR(100) UNIQUE
);

-- 创建学生证表,与学生表一对一关系
CREATE TABLE student_identities (
             identity_id INT PRIMARY KEY AUTO_INCREMENT,
             student_id INT UNIQUE,
             identity_number VARCHAR(20) UNIQUE NOT NULL,
             issue_date DATE NOT NULL,
             expiry_date DATE NOT NULL,
             FOREIGN KEY (student_id) REFERENCES students(student_id)
);

1.3多对多

案例:学生与课程的关系

关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择

实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

示例:

学生选课表,他与学生表之间是多对多的关系,即一个学生可以选择多门课程,一个课程可以被多个学生选择,为了符合这种多对多的关系,我决定建立第三张中间表,中间表至少包含两个外键,分别关联两方主键。

sql 复制代码
-- 创建课程表
CREATE TABLE courses (
       course_id INT PRIMARY KEY AUTO_INCREMENT,
       course_name VARCHAR(100) NOT NULL,
       teacher VARCHAR(50),
       credit TINYINT
);

-- 创建选课关系表(中间表)
CREATE TABLE student_courses (
       id INT PRIMARY KEY AUTO_INCREMENT,
       student_id INT NOT NULL,
       course_id INT NOT NULL,
       FOREIGN KEY (student_id) REFERENCES students(student_id),
       FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

案例:

分类表

sql 复制代码
create table category
(
    id          int unsigned auto_increment comment '主键ID'
        primary key,
    name        varchar(20)                null comment '分类名称',
    type        tinyint unsigned           null comment '分类类型 1.菜品分类,2.套餐分类',
    sort        tinyint unsigned           not null comment '排序',
    status      tinyint unsigned default 0 null comment '状态字段: 0:停用 1:启用',
    create_time datetime                   not null,
    update_time datetime                   not null
)
    comment '分类表';

菜品表

sql 复制代码
create table dish
(
    id          int unsigned auto_increment comment '菜品id'
        primary key,
    name        varchar(20)                  not null comment '菜品名称',
    category_id int unsigned                 not null comment '分类id',
    price       decimal(8, 2)                not null comment '价格',
    image       varchar(300)                 not null comment '图像',
    description varchar(200)                 null,
    status      tinyint unsigned default '0' not null comment '状态',
    create_time datetime                     not null comment '创建时间',
    update_time datetime                     not null comment '更新时间'
)
    comment '菜品表';

套餐类

sql 复制代码
create table setmeal
(
    id          int auto_increment comment '主键ID'
        primary key,
    name        varchar(20)      not null comment '套餐名称',
    category_id int unsigned     not null,
    price       decimal(8, 2)    not null,
    image       varchar(300)     not null comment '图片',
    description varchar(200)     null comment '描述信息',
    status      tinyint unsigned not null comment '状态 0 停售 1 起售',
    create_time datetime         not null,
    update_time datetime         not null
)
    comment '套餐类';

套餐菜品关系表

sql 复制代码
create table setmeal_dish
(
    id         int unsigned auto_increment comment '主键ID'
        primary key,
    setmeal_id int unsigned     not null comment '套餐ID',
    dish_id    int unsigned     not null,
    copies     tinyint unsigned not null
)
    comment '套餐菜品关系表';
相关推荐
不羁。。3 分钟前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
yangchanghua1112 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance2 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai2 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
幻雨様2 小时前
UE5多人MOBA+GAS 45、制作冲刺技能
android·ue5
在努力的前端小白2 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务2 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
Jerry说前后端3 小时前
Android 数据可视化开发:从技术选型到性能优化
android·信息可视化·性能优化
Meteors.4 小时前
Android约束布局(ConstraintLayout)常用属性
android
alexhilton5 小时前
玩转Shader之学会如何变形画布
android·kotlin·android jetpack