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 '用户信息表';

相关推荐

  1. MySql数据库01学习-第一DDL学习

    2024-04-12 00:02:02       16 阅读
  2. MySql数据库0-1学习-第二DML和DQL学习

    2024-04-12 00:02:02       13 阅读
  3. redis 01完整学习):redis 数据结构

    2024-04-12 00:02:02       44 阅读

最近更新

  1. splice方法的使用#Vue3

    2024-04-12 00:02:02       0 阅读
  2. 使用Dockerfile和ENTRYPOINT运行Python 3脚本

    2024-04-12 00:02:02       0 阅读
  3. 黑龙江等保测评对中小企业成本效益分析

    2024-04-12 00:02:02       0 阅读
  4. 6、Redis系统-数据结构-01-String

    2024-04-12 00:02:02       1 阅读
  5. STM32学习和实践笔记(39):I2C EEPROM实验

    2024-04-12 00:02:02       1 阅读
  6. Python面试题:请解释什么是反射(reflection)?

    2024-04-12 00:02:02       1 阅读
  7. Rudolf and k Bridges——Codeforces Round 933 (Div. 3) E

    2024-04-12 00:02:02       1 阅读

热门阅读

  1. 比武中的一段PHP代码

    2024-04-12 00:02:02       18 阅读
  2. Vue3+elementPlus组件递归

    2024-04-12 00:02:02       18 阅读
  3. Vue 3 组合式 API:优化代码的利器

    2024-04-12 00:02:02       15 阅读
  4. JQuery

    2024-04-12 00:02:02       14 阅读
  5. ubuntu下使用ndk编译libevnet

    2024-04-12 00:02:02       16 阅读
  6. 算法刷题记录 Day37

    2024-04-12 00:02:02       14 阅读
  7. 代码随想录训练营16day:二叉树5

    2024-04-12 00:02:02       14 阅读
  8. 21. 面试指导-高频面试题详解

    2024-04-12 00:02:02       17 阅读
  9. Frp多端口映射

    2024-04-12 00:02:02       11 阅读
  10. day04-MQ

    day04-MQ

    2024-04-12 00:02:02      13 阅读
  11. 二叉树链式存储详解

    2024-04-12 00:02:02       16 阅读
  12. c++手机通讯录管理系统280

    2024-04-12 00:02:02       17 阅读