From 1975caa93212d65b2389f51eea4c09a269b04cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com> Date: Thu, 7 Sep 2023 21:52:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\345\244\247\350\214\203\345\274\217.md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20230907 \346\225\260\346\215\256\345\272\223\344\270\211\345\244\247\350\214\203\345\274\217.md" diff --git "a/03 \350\265\226\345\277\203\345\246\215/20230907 \346\225\260\346\215\256\345\272\223\344\270\211\345\244\247\350\214\203\345\274\217.md" "b/03 \350\265\226\345\277\203\345\246\215/20230907 \346\225\260\346\215\256\345\272\223\344\270\211\345\244\247\350\214\203\345\274\217.md" new file mode 100644 index 0000000..b8c84c6 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20230907 \346\225\260\346\215\256\345\272\223\344\270\211\345\244\247\350\214\203\345\274\217.md" @@ -0,0 +1,138 @@ +## 数据库三大范式(规则) + +### 第一范式 + +要求字段内容不可再分割,为保证数据原子性 + +例如地址信息表,一个地址可以拆分为省、市、区、街道和详细地址 + +### 第二范式 + +要求在满足第一范式的基础上,要求非主键字段要完全依赖主键(非主键要依赖整个联合主键)而不能只依赖部分 + +例如成绩表,学生学号和课程编号在成绩表中缺一不可 + +| 学号 | 姓名 | 年龄 | 课程名称 | 成绩 | 学分 | +| ---- | ---- | ---- | -------- | ---- | ---- | +| | | | | | | + +1. 假设学号是表中的唯一主键,那由学号就可以确定姓名和年龄了,但是却不能确定课程名称和成绩 + +2. 假设课程名称是表中的唯一主键,那由课程名称就可以确定学分了,但是却不能确定姓名、年龄和成绩 + +3. 虽然通过学号和课程名称的联合主键,可以确定除联合主键外的所有的非主键值,但是基于上述两个假设,也不符合第二范式的要求 + +所以把表拆分开才会符合第二范式的要求 + +学生表 - 学号做主键 + +课程表 - 课程名称做主键 + +成绩表 - 学号和课程名称做联合主键 + +### 第三范式 + +满足第二范式的前提,要求非主键属性要直接依赖于主键 + +| 学号 | 姓名 | 班级 | 班主任 | +| ---- | ---- | ---- | ------ | +| | | | | + +这个表中,学号是主键,它可以唯一确定姓名、班级、班主任,符合了第二范式,但是在非主键字段中,我们也可以通过班级推导出该班级的班主任,所以它是不符合第三范式的 + +学生表 + +| 学号 | 姓名 | 班级 | +| ---- | ---- | ---- | +| | | | + +班级表 + +| 班级 | 班主任 | +| ---- | ------ | +| | | + +通过把班级与班主任的映射关系另外做成一张映射表,我们就成功地消除了表中的传递依赖了 + +```mysql +create database school charset utf8; + +use school; + +# 院系 +create table department( + de_id int primary key auto_increment, + de_name varchar(10) +); + +# 专业 +create table major( + ma_id int primary key auto_increment, + ma_name varchar(10), + de_id int, + foreign key (de_id) references department(de_id) +); + +# 班级 +create table clazz( + cla_id int primary key auto_increment, + cla_name varchar(10), + ma_id int, + foreign key (ma_id) references major(ma_id) +); + +# 学生 +create table student( + st_id int primary key auto_increment, + st_name varchar(10), + st_sex char(1), + cla_id int, + foreign key (cla_id) references clazz(cla_id) +); + +# 课程 +create table course( + cou_id int primary key auto_increment, + cou_name varchar(10), + t_id int, + foreign key (t_id) references teacher (t_id) +); + +# 学生+课程 +create table performance( + id int primary key auto_increment, + st_id int, + foreign key (st_id) references student(st_id), + cou_id int, + foreign key (cou_id) references course(cou_id), + score int +); + +# 教师 +create table teacher( + t_id int primary key auto_increment, + t_name varchar(10) +); + +# 教室 +create table classroom( + r_id int primary key auto_increment, + r_nsme varchar(10) +); + +# 班级+课程+教室+教室 +create table timetable( + time_id int primary key auto_increment, + time varchar(255), + cla_id int, + foreign key (cla_id) references clazz(cla_id), + cou_id int, + foreign key (cou_id) references course(cou_id), + t_id int, + foreign key (t_id) references teacher (t_id), + r_id int , + foreign key (r_id) references classroom (r_id) +); + +``` + -- Gitee