MySql数据库从0-1学习-第三天多表设计学习

项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种联系,基本上分为三种:

  • 一对多(多对一)
  • 多对多
  • 一对一

一对多

需求:根据需求,完成部门和员工表的设计

一对多,很多人会使用外键,推荐使用逻辑外键

在这里插入图片描述

# 用户表
create table tb_user
(
    id          bigint unsigned primary key auto_increment comment '用户id',
    name        varchar(50)      not null comment '用户名',
    password    varchar(50)      not null comment '密码',
    nick_name   varchar(50)      not null comment '昵称',
    age         tinyint unsigned not null comment '年龄',
    sex         tinyint unsigned not null comment '性别',
    phone       varchar(20)      not null comment '手机号',
    dept_id     bigint unsigned  not null comment '部门id',
    create_time datetime         not null comment '创建时间',
    update_time datetime         not null on update now() comment '更新时间'
) comment '用户表';
# 部门表
create table tb_dept
(
    id          bigint unsigned primary key auto_increment comment '部门id',
    name        varchar(50) not null comment '部门名称',
    create_time datetime    not null comment '创建时间',
    update_time datetime    not null on update now() comment '更新时间'
) comment '部门表';

一对一

例如某个表有10个字段,字段太多,特别臃肿,这个时候就需要进行拆分,将非必要信息存储在另一个表中,例如将用户表的其他信息拆分出去

# 用户表
create table tb_user
(
    id          bigint unsigned primary key auto_increment comment '用户id',
    name        varchar(50)     not null comment '用户名',
    password    varchar(50)     not null comment '密码',
    dept_id     bigint unsigned not null comment '部门id',
    create_time datetime        not null comment '创建时间',
    update_time datetime        not null on update now() comment '更新时间'
) comment '用户表';

# 用户表
create table tb_user_info
(
    id          bigint unsigned primary key auto_increment comment '用户信息id',
    user_id     bigint unsigned unique not null comment '用户id',
    nick_name   varchar(50)            not null comment '昵称',
    age         tinyint unsigned       not null comment '年龄',
    sex         tinyint unsigned       not null comment '性别',
    phone       varchar(20)            not null comment '手机号',
    create_time datetime               not null comment '创建时间',
    update_time datetime               not null on update now() comment '更新时间'
) comment '用户信息表';

多对多

学生和课程关系,一个学生对应多个课程,一个课程也对应多个学生

# 学生表
create table tb_student
(
    id          bigint unsigned primary key auto_increment comment '学生id',
    name        varchar(50) not null comment '学生名字',
    create_time datetime    not null comment '创建时间',
    update_time datetime    not null on update now() comment '更新时间'
) comment '学生表';

#中间表
create table tb_student_class
(
    id          bigint unsigned primary key auto_increment comment '中间表id',
    student_id  bigint unsigned not null comment '学生id',
    class_id    bigint unsigned not null comment '课程id',
    create_time datetime        not null comment '创建时间',
    update_time datetime        not null on update now() comment '更新时间'
) comment "学生和课程中间表";

# 课程表
create table tb_class
(
    id          bigint unsigned primary key auto_increment comment '课程id',
    name        varchar(50) not null comment '课程名字',
    create_time datetime    not null comment '创建时间',
    update_time datetime    not null on update now() comment '更新时间'
) comment '用户信息表';
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值