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)
);
相关推荐
如夏倾城25 分钟前
大模型(Large Model)解析:从数据类型、模型架构到任务选型
服务器·网络·数据库·语言模型·架构·audiolm
船神丿男人1 小时前
Redis 缓存击穿与雪崩实测:同键合并、TTL 错峰与迟到回填
数据库·redis·缓存
yuzhiboyouye1 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
小南家的青蛙2 小时前
MySQL跨环境数据同步方案全解析
mysql·复制
富贵儿9983 小时前
NL2SQL 实践手记 04:多表查询为什么更难——让模型找对表、连对关系
数据库·sql·ai·自然语言处理·langchain
Decay__3 小时前
PostgreSQL 索引与 SQL 优化 —— B-tree、GiST、GIN、BRIN、执行计划与索引设计实战
运维·数据库·sql·postgresql
FungLeo3 小时前
FengCMS ·PHP+MySQL 升级实录 · 性能测量方法论:先建基线、再判定,以及为什么暂缓 UNION 改写
mysql·php·工程实践·fengcms
FungLeo7 小时前
FengCMS · PHP+MySQL 升级实录 · 开源发布工程:GitHub Releases + SHA-256+ 三版发布说明
mysql·开源·php·fengcms
Decay__8 小时前
PostgreSQL 内存与锁调优 —— shared_buffers、work_mem、MVCC 与锁等待排查
运维·数据库·sql·postgresql
FungLeo8 小时前
FengCMS · PHP+MySQL升级实录 · 存量站升级方法论:备份 → 副本验收→migrate.php→ 停写切换
android·mysql·php·工程实践·fengcms