MySQL 7:数据库设计

1.范式

1.1 第一范式(1NF)

(1)定义

• 数据库表的每一列 都是不可分割的原子数据项,而是集合、数组、对象等非原子数据。

使用数据库 提供的基本数据类型 (如INT, VARCHAR, DATE)来表示每一列**】**

• 在关系型数据库 的设计中,满足第一范式是对关系模式的基本要求不满足 第一范式的数据库就不能被称为关系数据库。


需求:定义 一个学生表 ,需要记录学生 信息和学校信息。

(2)反例

学校 是一个对象,可以继续进行拆分 ,所以不满足第一范式

(3)正例

学校 信息包含在一行中,每一列不能再进行拆分 ,此时已满足第一范式


1.2 第二范式(2NF)

(1)定义

• 在满足1NF的基础 上,进一步要求所有非关键字段 必须完全依赖整个候选键 ,而不能部分依赖( 只依赖于候选键的一部分)。

• 第二范式主要针对复合主键 (即由多个列 组成的主键)的情况;如果表只有一个单列主键 ,则自动满足2NF (因为**不存在"部分依赖"**问题)。

关键字段:主关键字段(主键)、 候选关键字段(候选键)

候选键:表中能够唯一标识****每一行的一个多个列的组合】

主键:从候选键 中选出的一个 / 多个**,**用于作为表的唯一标识符】


需求 :学生可以选修课程 ,课程有对应的学分学生考试后每门课程 会产生相应的成绩

(2)反例

一张学生课程成绩表 记录所有信息

(3)正例

针对需求应该设计三张表学生 表、课程 表和成绩表。

(4)不满足第二范式时可能出现的问题


1.3 第三范式

(1)定义

满足2NF的基础 上,进一步要求所有非关键字段必须直接依赖任意候选键 ,而不能传递依赖


需求:要求学生表记录 学生所属的学院,在满足2NF的基础上对学生表做出修改。

(2)反例

(3)正例

学院信息拆分 出来定义学院表 ,学生表与学院表做关联

2.设计过程


3.实体-关系图 (E-R图)

实体-关系 图(Entity-Relationship Diagram)简称 E-R图 ,也称作实体联系模型、实体关系模型,是一种用于描述数据模型的概念图 ,主要用于数据库设计阶段

画E-R图可以用在线网站https://app.diagrams.net/ https://www.processon.com/ 或下载软件https://www.drawio.com/


3.1 一对一关系(1:1)


3.2 一对多关系(1:N)


3.3 多对多关系(M:N)


4.练习:设计表

根据以上E-R图完成表的创建,并添加主键列

4.1 用户与账户的一对一关系

实体间一对一关系只需要在其中一个实体中添加对另一个实体的关联字段即可

sql 复制代码
# 在⽤⼾实体中添加对账⼾实体的关联
drop table if exists users;
create table users (
 id bigint primary key auto_increment,
 name varchar(20) not null, 
 nickname varchar(20),
 phone_num varchar(11), 
 email varchar(50),
 gender tinyint(1),
 account_id bigint
);

drop table if exists account;
create table account (
 id bigint primary key auto_increment,
 username varchar(20) not null,
 password varchar(32) not null
);


# 在账⼾实体中添加对⽤⼾实体的关联
drop table if exists users;
create table users (
 id bigint primary key auto_increment,
 name varchar(20) not null, 
 nickname varchar(20),
 phone_num varchar(11), 
 email varchar(50),
 gender tinyint(1)
);

drop table if exists account;
create table account (
 id bigint primary key auto_increment,
 username varchar(20) not null,
 password varchar(32) not null,
 users_id bigint
);

4.2 学生与班级的一对多关系

分别创建学生表和班级表,在学生表中添加一列班级表建立关联关系。

sql 复制代码
# 班级表
drop table if exists class;
create table class (
 id bigint primary key auto_increment,
 name varchar(20)
);


# 学⽣表
drop table if exists student;
create table student (
 id bigint primary key auto_increment,
 name varchar(20) not null, 
 sno varchar(10) not null,
 age int default 18,
 gender tinyint(1), 
 enroll_date date,
 class_id bigint
);

4.3 学生、课程与成绩的多对多关系

学生可以选修多门课程 ,每门课程考试 后会产生一个成绩 ,两个表之间没有办法直接建立关系,所以要用到一个记录成绩的中间表

sql 复制代码
# 学⽣表
drop table if exists student;
create table student (
 id bigint primary key auto_increment,
 name varchar(20) not null, 
 sno varchar(10) not null,
 age int default 18,
 gender tinyint(1), 
 enroll_date date,
 class_id bigint,
 foreign key (class_id) references class(id)
);

# 课程表
drop table if exists course;
create table course (
 id bigint primary key auto_increment,
 name varchar(20)
);


# 分数表
drop table if exists score;
create table score (
 id bigint primary key auto_increment,
 score float,
 student_id bigint,
 course_id bigint,
 foreign key (student_id) references student(id),
 foreign key (course_id) references course(id)
);
相关推荐
heze092 小时前
sqli-labs-Less-22
数据库·mysql·网络安全
墨雨晨曦882 小时前
如何保证redis和mysql数据一致性方案对比
数据库·redis·mysql
rfidunion2 小时前
springboot+VUE+部署(9。安装MySql)
spring boot·后端·mysql
枷锁—sha2 小时前
【Vulhub】Discuz! 7.2 faq.php SQL 注入深度复现手册 (转义逃逸篇)
数据库·sql·php
超级种码2 小时前
Redis:Redis 常见问题及解决思路
数据库·redis·缓存
计算机学姐3 小时前
基于SpringBoot的社区互助系统
java·spring boot·后端·mysql·spring·信息可视化·推荐算法
xcLeigh3 小时前
Oracle 迁移 KingbaseES 避坑指南:工具选型、参数配置与性能调优
数据库·oracle·工具·性能·金仓·kingbasees
JY.yuyu3 小时前
SQL Server数据库
数据库
June bug3 小时前
【配环境】安装配置Oracle JDK
java·数据库·oracle