跳转至

6. Entity-Relationship Model

约 4272 个字 4 行代码 预计阅读时间 21 分钟

实体关系模型

Database Design Process

Example of Entity-Relationship Model

image-20240401133652021

  • 一个方形框就是一个实体的集合,下面列出其属性。
  • 实体与实体之间有关系,一个菱形框表示关系relationship

    • 至多一个(\(\leftarrow, \rightarrow\)) 单向箭头,一个学生一定会有instructor,但是instructor不一定有学生被指的有多个指的
    • 一对一(双向箭头)
    • 多对多(直线)

      • course 这里的 prereq 是多对多,表示一门课可以有多门预修课,一门课也可以是多门课的预修课。
      • instructor可以教多门课,同时一个课可以有多个intructor教
    • 双横线表示每个对象都必须参与关系,而单横线则表示对象可以不参与关系。

      • inst_dept 中如果 department-inst_dept 为双横线,则表示每一个系都要有老师。 而instructor - inst_dept之间为双横线,灭一个老师都是系。
      • instructor可以没有课,但是每一个课section都有instructor
      • 每一个course都需要有course_dept,都需要所属的系。同时一个系又可以有多个course
      • 每一个section都有对应的sec_course,但是一个course能有多个section,也就是能有多个班级
      • 每一个section都有上课的时间,sec_time_slot,但是一个sec_time_slot能有多个section上课
      • 每一个section都有上课的班级,但是一个sec_class能有多个section
  • 这里 instructor 实体里不需要 dept_name 属性,因为在 department 实体里有了,否则会冗余。section表中原本有course_id, building, room_number, time_slot_id,但是都没有在section实体中展示出来,原因是course_id 已经出现在course entity中,其他同理,多次出现会冗余,一般保留以primary key形式存在的

  • section 不足以唯一确定元组,称为弱实体,依赖于另一个实体(如 OOP、DB 都可以有同样年份学期的 1 班)。因为课程号 course_id 放在 section 会有冗余,因此没有这个属性,导致形成了一个弱实体。sec_course 表示联系的是弱实体(双框)section 不能离开 course 存在。 只有将弱实体连接的实体的primary key 和 自己的key(带下划虚线)的组合在一起才能唯一确定一个元组

  • 每个实体直接转换为关系模式。关系转换为元组,元素为两个表的 primary key. 对于一对多的情况(如 instructordepartment)转换后 primary key 仍为 ID 和 dept_name.

  • relationship 上也可以带属性,如 takes 上的 grade.

  • 关系双方可以是相同的实体集合course 这里的 prereq 是多对多,表示一门课可以有多门预修课,一门课也可以是多门课的预修课。

  • {} 里面是多个值,表示复合属性。这里表示 time_slot_id 实际上可以由这三个属性复合而成。

img

ER图中的实体是table(矩形框)

菱形框表示relationship,是entity之间的联系

img

6.1 Database Modeling

A database can be modeled as:

  • a collection of entities 实体的集合
  • relationship among entities. 实体之间的关系

6.1.1 Entities

An entity is an object that exists and is distinguishable from other objects.

实体是存在且可与其他对象区分开来的对象。

e.g. specific person, company, event, plant

  • Entities have attributes

    实体具有属性

    e.g. people have names and addresses

  • An entity set is a set of entities of the same type that share the same properties.

    实体集是一组共享相同属性的相同类型的实体。

    e.g. set of all persons, companies, trees, holidays

Entity sets can be represented graphically as follows:

  • Rectangles represent entity sets.
  • Attributes listed inside entity rectangle
  • Underline indicates primary key attributes

矩形表示实体集。实体矩形内列出的属性。下划线表示主键属性

6.1.2 Relationship Sets

A relationship is an association among several entities.

A relationship set is a mathematical relation among n entities, each taken from entity \(\{(e_1, e_2, ... e_n) | e_1 \in E_1, e_2 \in E_2, ..., e_n \in E_n\}\) where \((e_1, e_2, ..., e_n)\) is a relationship.

本质也是一个集合。最开始的例子中均为是二元联系,即是两个实体集合的关系,但是关系可以是多元的,即多个实体。如老师,同学,SRTP 项目可以共同形成一个关系。

此处student与instructor之间存在多对多的关系,一个老师指导多名学生,一个学生由多位老师培养

6.1.2.1 Relationship Sets with Attributes

An attribute can also be property of a relationship set.

e.g. The advisor relationship set between entity sets instructor and student may have the attribute date which tracks when the student started being associated with the advisor.

例如,实体集教师和学生之间的顾问关系集可能具有属性日期,用于跟踪学生何时开始与顾问关联。

advisor中存在属性,student的ID,instructor的ID,以及结对的时间

6.1.2.2 Roles

Entity sets of a relationship need not be distinct. 关系的实体集可以相同也可以不同。

Each occurrence of an entity set plays a “role” in the relationship

The labels course_id and prereq_id are called roles.

实体集的每次出现都扮演着“角色”关系中的“角色”,标签 course_idprereq_id 称为角色

不用双横线——一门course没有预修课程。横线——多对多

6.1.2.3 Degree(度) of a Relationship Set

binary relationship(二元联系)

  • involve two entity sets (or degree two). 涉及两个实体集
  • most relationship sets in a database system are binary.

尽量不用多元联系,因为二元联系比较清晰。而且任何的多元联系都可以通过引入中介实体转化为二元联系。

转化为二元联系:把多元联系实体化。如 proj_guide 里有老师、学生、工程的 id. 随后这个实体又和另外三个实体各有一个二元联系。

6.1.3 Attributes

An entity is represented by a set of attributes, that is descriptive properties possessed by all members of an entity set.

实体由一组属性表示,这些属性是实体集的所有成员所拥有的描述性属性

Attribute types 属性类型:

  • Simple(简单) and composite(复合) attributes.

  • Single-valued(单值) and multivalued(多值) attributes

    e.g. multivalued attribute: phone_numbers

  • Derived(派生) attributes

    • Can be computed from other attributes 通过其他属性计算出来

      e.g. age, given date_of_birth

    image-20240401141926733

Representing Complex Attributes in ER Diagram

  1. 复合属性由多个属性组成,相当于C语言中的sturct
  2. 多值属性,用“{ }”包含
  3. Derived attribute

6.1.4 Mapping Cardinality Constraints

映射基数约束

Express the number of entities to which another entity can be associated via a relationship set.

可通过关系集将另一个实体关联到的实体

  • One to one
  • One to many
  • Many to one
  • Many to many

We express cardinality constraints by drawing either a directed line (\(\rightarrow\)), signifying “one,” or an undirected line (\(—\)), signifying “many,” between the relationship set and the entity set.

我们通过在关系集和实体集之间绘制一条表示“一”的有向线 ( → ) 或表示“许多”的无向线 ( — ) 来表示基数约束。

  1. 一对一

一个学生最多和一个老师关联,一个老师最多和一个学生关联

  1. 一对多

    image-20240407194947160

    一个学生最多有一个老师指导,一个老师可以指导多名学生

  2. 多对多

    image-20240407195026252

    教师可以与多个(可能是 0 个)学生相关联 , 一个学生可以与多个(可能是 0 个)教师相关联

6.1.5 Total and Partial Participation

  • Total participation (indicated by double line): every entity in the entity set participates in at least one relationship in the relationship set

    所有元素都要参与关系

  • Partial participation: some entities may not participate in any relationship in the relationship set

    部分参与:某些实体可能不参与关系集中的任何关系

image-20240407195233870

双横线和单横线的区别

6.1.6 Notation for Expressing More Complex Constraints

用于表达更复杂约束的表示法

A line may have an associated minimum and maximum cardinality, shown in the form l..h, where l is the minimum and h the maximum cardinality

一条线可能具有关联的最小基数和最大基数,如 l. 形式所示。h,其中 l 是最小基数,h 是最大基数

  • A minimum value of 1 indicates total participation.
  • A maximum value of 1 indicates that the entity participates in at most one relationship
  • A maximum value of * indicates no limit.

最小值为1,意味着必须有参与,说明是total participation,最大值是1,意味着实体最多参与一个关系。最大值为*表示没有限制

Instructor can advise 0 or more students. A student must have 1 advisor (instructor); cannot have multiple advisors(instructors).

多元关系里,我们只允许最多有一个箭头。

6.1.7 Primary Key for Entity Sets

A key for an entity is a set of attributes that suffice to distinguish entities from each other

实体的键是一组足以将实体彼此区分开来的属性.实体集中不允许两个实体在所有属性中具有完全相同的值

  • Let R be a relationship set involving entity sets E1, E2, .. En

  • The primary key for R is consists of the union of the primary keys of entity sets E1, E2, ..En

关系集R的主键是由实体集E1,E2,…. En的主键组成的

  • If the relationship set R has attributes a1, a2, .., am associated with it, then the primary key of R also includes the attributes a1, a2, .., am

关系集的主键取决于关系集的映射基数(mapping cardinality)

  1. 多对多关系。就是两个实体集的主键的组合
  2. 一对多关系。主键选择many侧的主键
  3. 多对一关系。The primary key of the “Many” side is a minimal superkey and is used as the primary key
  4. 一对一关系。任何一个参与实体集的主键形成一个最小的超级键,并且可以选择任何一个作为主键。

image-20240407200920591

多元联系的 primary key 是两端 key 的组合。如果是一对一的联系,那么单个实体的 key 也可以作为 primary key. 一对多的联系(如导师和学生就是一对多,那么主键应该为学生)

6.1.8 Weak Entity Sets

弱实体集

An entity set that does not have a primary key is referred to as a weak entity set.

没有主键的实体集称为弱实体集

The existence of a weak entity set depends on the existence of a identifying entity set(标识性实体集)

  • It must relate to the identifying entity set via a total, one-to-many relationship set from the identifying to the weak entity set

  • Identifying relationship(标识性联系) depicted using a double diamond

使用双菱形描绘的识别关系(标识性联系)

The discriminator(分辨符,or partial key) of a weak entity set is the set of attributes that distinguishes among all the entities of a weak entity set when the identifying entity they depend is known.

partial key 和 identifying entity set的primary key 结合在一起才能唯一识别元组

我们用虚线在弱实体集的partial key下划线。(虚线) 我们将弱实体的识别关系放在双菱形中。(双框)

image-20240407201520949

强实体集的主键不显式存储在弱实体集中,因为它是隐式的。

如果显式存储course_id,则 section 可以成为一个强实体,但 section 和 course 之间的会因为course_id而冗余

6.1.9 Redundant Attributes 冗余属性

既有关系 student_dept 又有 属性dept_name,两者重复

foreign key 会因为 冗余而去掉

6.2 Reduction to Relational Schemas

一个 ER 图可以转换成多种模式(图数据库、面向对象、关系模式等)

  • A strong entity set reduces to a schema with the same attributes course(course_id, title, credits)

    普通的entity直接转化为关系模式即可。强实体集简化为具有相同属性的架构

  • A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set.

    Primary key of the table is the union of the discriminator of the weak entity set and the primary key of the identifying strong entity set.

    表的主键是弱实体集的discriminator(partial key)和标识强实体集的主键的并集

    section(course_id, sec_id, semester, year)

  • A many-to-many relationship set is represented as a schema with attributes for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. 即两个集合的主键拼起来,加上关系的附带属性。

    advisor = (s_id, i_id)

  • Many-to-one and one-to-many relationship sets that are total on the many-side can be represented by adding an extra attribute to the “many” side, containing the primary key of the “one” side.

    多对一可以不转换为单独的关系模式,直接在“多“那个表上添加”一“的主键即可

    inst_dept 本质就是将 instructordepartment 合并,在 instrutor 的属性上添加 dept_name.

    image-20240401145224840

    primary key (含义)相同的表可以合并, instructor 和 inst_dept可以合并

    各有利弊,第一种写法表可能会太多,第二种写法合在一起表可能太大,不利于管理。

  • 一对一关系,任选一方的primary key 作为主键

6.2.1 Composite and Multivalued Attributes

image-20240407203744172

Composite attributes are flattened out by creating a separate attribute for each component attribute.

就像在 C 语言里定义一个结构。但是关系数据库里每个属性都必须是简单数据类型,就必须把这些复合属性铺平

e.g.

instructor(ID, 
        first_name, middle_initial, last_name,      
        street_number, street_name, apt_number, 
        city, state, zip_code, date_of_birth, age)

A multivalued attribute M of an entity E is represented by a separate schema EM.

实体 E 的多值属性 M 由单独的schema EM 表示。

Schema EM has attributes corresponding to the primary key of E and an attribute corresponding to multivalued attribute M.

schema EM 具有与 E 的主键相对应的属性和与多值属性 M 相对应的属性。

例如此处,建立新的schema inst_phone, 其中的属性是 instructor的主键 ID,和多值属性phone_number。多值属性的每个值都映射到架构 EM 上关系的单独元组

特殊情况:实体time_slot除了主键属性外只有一个属性,并且该属性是多值的

会创建两个schema, 分别是

  • time_slot(time_slot_id)
  • time_slot_detail(time_slot_id, day, start_time, end_time)

缺点:优化去掉time_slot这个表后,就无法使用外键time_slot_id

利用一对多的性质,把many的主键拿过来,最终的section(course_id, sec_id, semester, year, building, room_number, time_slot_id)

  1. 在把ER图转化为关系模式时,entity的哪种属性必须单独转化为一个关系模式?

    • 组合属性:平铺即可
    • 多值属性:必须转化
    • 导出属性(derived attribute):未知
  2. 在把ER图转化为关系模式时,哪种relationship set必须单独转化为一个关系模式

    • 一对一:特殊版的一对多
    • 一对多/多对一:把主键添加到另一个entity即可,非强制
    • 多对多:强制,两个entity的主键的组合

6.3 Design Issues

6.3.1 Common Mistakes in E-R Diagrams

  • 信息冗余

    student 的 dept_name 应该去掉

    image-20240401152055792

    不然转化为关系模型之后,dept_name会出现两次

  • 关系属性使用不当

    这里一门课可能有很多次作业,不能只用一个实体。

    image-20240401152200340

    解决方法:

    image-20240401152242599

    把assignment新拉出来,section有作业assignment,作业对每个学生来说有成绩mark_in

6.3.2 Use of entity sets vs. attributes

image-20240401152517850

phone_number可以作为一个属性,也可以扩充作为一个实体entity

  • 第一种方法,明确放一个电话号码。
  • 第二种方法,电话号码可以附属更多属性,一个电话号码可以由多人共享。(如办公室的公共电话)
    • 好处:信息更多,可以共享; 坏处:表变多了

6.3.3 Use of entity sets vs. relationship sets

image-20240401152852386

此处一个registration对应一个学生,对应一个section,采用双横线。但是一个学生可以有多个registration(选多门课),一个section可以有多个registration(被多个学生选)

image-20240407210820600

原本student entity与section之间是通过relationship set联系,但是也可以通过新添加一个entity进行联系

实体可以便于与其他实体建立联系。

如电商,我们可以简单的把客户和商品用 buy 联系起来,但后续还会有付款、物流等情况,我们最好把 buy 实体化为订单。

6.3.4 Placement of relationship attributes

image-20240401153024689

relationship的属性是放在relationship set中还是放在实体中?

  • 第一种方法,可以记录每次访问的访问日期。
  • 第二种方法,只能记录用户最近一次访问日期,不完整。

6.3.5 Binary Vs. Non-Binary Relationships

Example "Converting Non-Binary Relationships "

多元联系到二元联系的转化

新建一个实体E,建立新实体与旧实体之间的联系

6.4 Extended ER Features

扩展

  • Specialization(特化)

    • Top-down design process; we designate subgroupings within an entity set that are distinctive from other entities in the set.

    自上而下的设计流程;我们指定实体集中的子分组,这些子分组与集合中的其他实体不同。

    • Attribute inheritance – a lower-level entity set inherits all the attributes and relationship participation of the higher-level entity set to which it is linked.

    属性继承 – 较低级别的实体集继承与其链接的高级实体集的所有属性和关系参与

  • Generalization(概化)

A bottom-up design process – combine a number of entity sets that share the same features into a higher-level entity set.

自下而上的设计过程 – 将共享相同特征的多个实体集组合到更高级别的实体集中。

image-20240401154301636

两种箭头模式 total / partial

**disjoint 不相交 / overlapping 重叠 **

image-20240401154808503

从上到下,specialization,属性的继承,继承higher-level entity的主键

缺点:获取信息时,员工需要访问两个关系,一个对应于低级架,就比如需要获取student的name

image-20240401155036907

为每个实体集形成一个架构,其中包含所有本地和继承属性

缺点:对于既是学生又是员工的人来说,姓名、街道和城市可能会被冗余存储(overlapping情况)

image-20240401155042643

一个表包含所有的属性

缺点:表太长,null值太多