数据库设计

1. 多表之间的关系

  1. 多表之间的关系的分类

    1. 一对一
    2. 一对多
    3. 多对多
  2. 多表之间的关系的实现

    1. 一对一的实现(了解)

      可以在任意一方添加外键指向另一方的主键,再将添加外键列设为唯一。

    2. 一对多的实现

      在 “多” 的一方建立外键,指向 “一” 的一方的主键。

    3. 多对多的实现

      多对多关系实现需要借助第三张中间表。中间表至少包含两个字段,这两个字段作为第三张表的外键,分别指向两张表的主键。

  3. 多表之间的关系的练习

    1. 需求分析

      1. 创建旅游线路分类表 tab_category
      2. 创建旅游线路表 tab_route
      3. 创建用户表 tab_user
      4. 建立三个表之间的关系
    2. 概念模型

      在这里插入图片描述

    3. 物理模型

      在这里插入图片描述

    4. 生成数据库

      /*==============================================================*/
      /* 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. 数据库设计范式

  1. 数据库范式的概念

    设计关系数据库时,要遵从一些的规范要求,才能设计出合理的关系型数据库。这些规范要求被称为范式,越高的范式数据库冗余越小。

  2. 相关概念介绍

    1. 函数依赖:如果通过 A 属性(属性组)的值,可以确定唯一 B 属性的值,则称 B 依赖于 A,记作:A --> B

      例如:学号 --> 姓名 (学号,课程号) --> 分数

    2. 完全函数依赖:如果 A 是一个属性组,B 属性值的确定需要依赖于 A 属性组中所有的属性值,则称 B 完全依赖于 A,记作:A --> B(箭头上加个 F)

      例如:(学号,课程号) --> 分数

    3. 部分函数依赖:如果 A 是一个属性组,B 属性值的确定只需要依赖于 A 属性组中某一些值,则称 B 部分依赖于 A,记作:A --> B(箭头上加个 P)

      例如:(学号,课程号) – > 姓名

    4. 传递函数依赖:如果通过 A 属性(属性组)的值,可以确定唯一 B 属性的值,再通过B属性(属性组)的值可以确定唯一 C 属性的值,则称 C 传递函数依赖于 A,记作:A --> B,B --> C

      例如:学号 --> 系名,系名–>系主任

    5. 码:如果在一张表中,一个属性或属性组,被其他所有属性所完全依赖,则称这个属性(属性组)为该表的码。

      例如:成绩表中码为:(学号,课程号)

    6. 主属性:码属性组中的所有属性

    7. 非主属性:除了码属性组的属性

  3. 范式的分类

    1. 第一范式(1NF):每一列都是不可分割的原子数据项
    2. 第二范式(2NF):在 1NF 的基础上,非码属性必须完全依赖于候选码(在 1NF 基础上消除非主属性对主码的部分函数依赖
    3. 第三范式(3NF):在 2NF 的基础上,任何非主属性不依赖于其它非主属性(在 2NF 基础上消除传递依赖
  4. 三大范式的应用

    1. 普通的表

      在这里插入图片描述

    2. 满足 1NF 的表

      在这里插入图片描述

    3. 满足 2NF 的表

      在这里插入图片描述

    4. 满足 3NF 的表

      在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bm1998

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值