数据库系统笔记(二)

Entity-Relationship Model

Entity Sets

使用entity和relationship给真实世界建模,entity可以理解为图中的点,relationship为图上的边

entity可以是具体事物或抽象事物,但必须具有唯一性,entity具有attribute

entity set是一系列同类型的entity

entity由一系列attribute表示

domain是attribute允许的取值

attribute分类:

  • simple and composite
  • single-valued and multi-valued
  • derived(compared with base or store)

composite attribute:name(first name, middle initial, last-name)

component attribute: street number, street name, apartment-number

Relationship Sets

relationship是两个或多个不同类entity之间的关联

relationship set是一系列同类型的relationship

attribute也可以是relationship set的一部分

degree of relationship set: 参与这个relation set的entity set

degree=2 is binary

relationship degree>2 很稀少

mapping cardinality:relationship set中,一个entity可以与另一类entity相联系的entity数目。其中数目是指最多一个还是多个

Keys

super key:一个或多个可以区分每个entity的attribute

candidate key:最小的super key

primary key:一个被选中的candidate key

参与一个relationship set的各entity set的primary key的组合,构成该relationship set的super key

选择primary key时应考虑各个candidate key的现实意义

E-R Diagrams

  • 矩形为entity set
  • 菱形为relationship set
  • 椭圆为attribute
  • 带下划线为primary key
  • 线将attribute连接到entity set再连接到relationship set

relationship set can be recursive

Cardinality Constraints

directed line:one

undirected line:many

Participation of an Entity Set in a Relationship Set

total participation(double line):每个entity都在至少一个relationship中

partial participation:一些entity不在

Mapping cardinality constraints:限定了实体与发生关联的另一端实体可能关联的数目上限

Participation:下限

上下限的替代写法:min..max(*代表无穷大)

Non-binary Relationship and binary Relationship

创造一个artificial entity set可以将Non-binary relationship转换为binary relationship

Weak Entity Sets

定义:没有primary key的entity set,只有discriminator或partial key

依赖于一个identifying entity set或owner entity set

必须和一个total,one-to-many relationship set相关,相关的relationship称为identifying relationship

strong entity set的primary key不会显式地存储在weak entity set中,而是隐含在identifying relationship里

Extended E-R Features

自上而下(Top-down)设计:从一般到特殊,低级entity set继承高级entity set同时增加新的特性。(Specialization)

自下而上(bottom-up)设计:从特殊到一般。(Generalization)

Constraint

  1. 约束哪些entity可以成为低级entity set的成员
  2. 单个entity是否可以成为多个低级entity set的成员(disjoint,overlapping)
  3. 完全性约束:高级entity是否一定要属于至少一个低级entity set

Design of an E-R Database Schema

  1. 需求分析
  2. 概念设计
  3. 逻辑设计
  4. 物理设计

若一个对象只对其名字及单值感兴趣,则可作为属性,如性别,若一个对象除名字外,本身还有其他属性需描述,则该对象应定义为实体集,如电话,部门。

一个对象不能同时作为实体和属性。

一个实体集不能与另一实体集的属性相关联,只能实体与实体相联系。

要从对象的语义独立性和减少数据冗余方面考虑。

specialization和generalization有助于模块化。

aggregation可以将E-R图的一部分视为一个整体而不考虑内部细节。

Reduction of an E-R Schema to Tables

每个entity set和relation set都对应一个table

A strong entity set -> a table with the same attributes.

composite attribute需要展开,给每个部分单列一列

一个entity的multivalued attribute需要单独建一个table

一个weak entity set转换成表要加一个primary key来找它的identifying entity set

一个relationship set转换成表列为参与的entity set的primary key和relationship自己的descriptive attribute

many to many两个primary key一起作为primary key

many to one,many的primary key作为primary key

one to one两个均可

one to many可将relationship对应的table放到many端entity set实体的表中,但如果many端是partial participation的,可能会出现NULL(one to one两端都可以)

对应identifying entity set的table是redundant

用table表示specialization:

  1. 建一个高级表,下面的表包含高级的表primary key和独有的attribute
  2. 建立拥有自己和上级所有属性的表

UML

unified modeling language 统一建模语言

UML有许多组件,可以对整个软件系统的不同方面进行图形化建模

该篇为本系列最后一篇,本来打算学期中记笔记期末周复习,结果这学期根本没学这门课……,因此只打算再更新好SQL部分(因为只有这部分我觉得我以后会用到)就开摆了

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023 J-sycamore