数据库实体关系图 --- The Entity-Relationship Model: ER Model
ER模型的作用
- 数据库设计的第一步是需求分析阶段,也是最重要的一步
- 需求分析阶段的主要工作是概念建模也就是ER建模, 将需求以ER图的方式进行可视化
- ER图可以描述一个数据库的逻辑结构
ER模型的基本组成
- 实体(entity): 最基本的数据成员,An entity is an object that exists and is distinguishable from other objects
- For instance, Michelle Lee with S.S.N.890-12-3456 is an entity
- 一个entity可以是concrete(一个人,一本书等) 也可以是abstract (比如假期或者疾病)
- entity set:一组同一类型的entities组成了entity set
- 比如银行的所有用户组成了一组entity set
- entity set可以有重叠,比如Student 和 Professor这两个entity set可以有重复的entity,因为一个人可以是老师也可以是另外一节课的学生
- 属性(attributes): 一个entity用一组attributes来表示,每一个attribute有一个对应的值,比如一个人可以有name,SSN,gender,phone number等attributes
- relationship sets: the relationship between two entity sets.
- domain: 一个attribute的domain是这个attribute值得有效范围,比如电话号码是一个十一位整数
E-R 图
ER图的基本组成
- ER 图的基本组成结构有
- 正方形 — 代表entity sets
- 椭圆形 — 表示attributes
- 菱形 — 表示relationship sets
- 线条 — 链接attributes和entity sets,entity sets和relationship sets
不同的键(Key)
超码: superkey
- 能够用来定位关系表中entity的一个或多个属性 Example: {name,S.S.N} 就是super key
候选码: candidate key
- 最小长度的超码
主键: primary key
- DB设计者指定的candidate key用来区分不同的entity 用下划线表示
外键 foreign key
- 一个关系表(relation)的一组字段指向另外一个关系表(relation)的一行 (有点类似pointer)
- 通过包含另外一个关系表(relation)的primary key实现