文章目录
1. 多表之间的关系
-
多表之间的关系的分类
- 一对一
- 一对多
- 多对多
-
多表之间的关系的实现
-
一对一的实现(了解)
可以在任意一方添加外键指向另一方的主键,再将添加外键列设为唯一。
-
一对多的实现
在 “多” 的一方建立外键,指向 “一” 的一方的主键。
-
多对多的实现
多对多关系实现需要借助第三张中间表。中间表至少包含两个字段,这两个字段作为第三张表的外键,分别指向两张表的主键。
-
-
多表之间的关系的练习
-
需求分析
- 创建旅游线路分类表 tab_category
- 创建旅游线路表 tab_route
- 创建用户表 tab_user
- 建立三个表之间的关系
-
概念模型
-
物理模型
-
生成数据库
/*==============================================================*/ /* DBMS name: MySQL 5.0 */ /* Created on: 2019/8/2 3:13:40 */ /*==============================================================*/ drop table if exists tab_category; drop table if exists tab_route; drop table if exists tab_user; drop table if exists user_route_relation; /*==============================================================*/ /* Table: tab_category */ /*==============================================================*/ create table tab_category ( cid int not null auto_increment, cname varchar(100) not null, primary key (cid), unique key AK_unique_key (cname) ); alter table tab_category comment '旅游线路分类'; /*==============================================================*/ /* Table: tab_route */ /*==============================================================*/ create table tab_route ( rid int not null auto_increment, cid int, rname varchar(100) not null, rprice double, rdate date, primary key (rid), unique key AK_unique_key (rname) ); alter table tab_route comment '旅游线路'; /*==============================================================*/ /* Table: tab_user */ /*==============================================================*/ create table tab_user ( uid int not null auto_increment, username varchar(100) not null, password varchar(20) not null, primary key (uid), unique key AK_unique_key (username) ); alter table tab_user comment '旅游线路表'; /*==============================================================*/ /* Table: user_route_relation */ /*==============================================================*/ create table user_route_relation ( rid int not null, uid int not null, primary key (rid, uid) ); alter table tab_route add constraint FK_cate_route_relation foreign key (cid) references tab_category (cid) on delete restrict on update restrict; alter table user_route_relation add constraint FK_user_route_relation foreign key (rid) references tab_route (rid) on delete restrict on update restrict; alter table user_route_relation add constraint FK_user_route_relation2 foreign key (uid) references tab_user (uid) on delete restrict on update restrict;
-
2. 数据库设计范式
-
数据库范式的概念
设计关系数据库时,要遵从一些的规范要求,才能设计出合理的关系型数据库。这些规范要求被称为范式,越高的范式数据库冗余越小。
-
相关概念介绍
-
函数依赖:如果通过 A 属性(属性组)的值,可以确定唯一 B 属性的值,则称 B 依赖于 A,记作:A --> B
例如:学号 --> 姓名 (学号,课程号) --> 分数
-
完全函数依赖:如果 A 是一个属性组,B 属性值的确定需要依赖于 A 属性组中所有的属性值,则称 B 完全依赖于 A,记作:A --> B(箭头上加个 F)
例如:(学号,课程号) --> 分数
-
部分函数依赖:如果 A 是一个属性组,B 属性值的确定只需要依赖于 A 属性组中某一些值,则称 B 部分依赖于 A,记作:A --> B(箭头上加个 P)
例如:(学号,课程号) – > 姓名
-
传递函数依赖:如果通过 A 属性(属性组)的值,可以确定唯一 B 属性的值,再通过B属性(属性组)的值可以确定唯一 C 属性的值,则称 C 传递函数依赖于 A,记作:A --> B,B --> C
例如:学号 --> 系名,系名–>系主任
-
码:如果在一张表中,一个属性或属性组,被其他所有属性所完全依赖,则称这个属性(属性组)为该表的码。
例如:成绩表中码为:(学号,课程号)
-
主属性:码属性组中的所有属性
-
非主属性:除了码属性组的属性
-
-
范式的分类
- 第一范式(1NF):每一列都是不可分割的原子数据项
- 第二范式(2NF):在 1NF 的基础上,非码属性必须完全依赖于候选码(在 1NF 基础上消除非主属性对主码的部分函数依赖)
- 第三范式(3NF):在 2NF 的基础上,任何非主属性不依赖于其它非主属性(在 2NF 基础上消除传递依赖)
-
三大范式的应用
-
普通的表
-
满足 1NF 的表
-
满足 2NF 的表
-
满足 3NF 的表
-