chapter2- database_modeling

Download Report

Transcript chapter2- database_modeling

Chapter 2
Database Modeling
Data Models
Concept Model & Data Model
Entity-Relationship diagrams
1
What are Data Model?
Computer simulation of the real world.
To abstract, denote and transact the
information in the real world.
2
Data Model should :
Simulate the real world comparatively truly
Be easy to understand
Be easy to be implemented by computer
3
Abstraction and Modeling
Two steps of the abstraction and modeling:
1. The objective objects in the real world is
abstracted to a concept model.
2. Concept model is converted to a data model that
supported by some DBMS.
The concept model is a middle layer between the real
world and the machine world.
客观对象
现实世界
概念模型
信息世界
数据模型
机器世界
4
The classification of the models
Concept model
 Also called information model.
 Modeling data from the viewpoint of users.
 a middle layer between the real world and the machine
world.
 Mainly used for a database design.
 The language in which the database designers and users
communicate with each other.
 It should be simple, clear and easy to understand by
users.
5
Data model
Modeling data from the viewpoint of computer.
Mainly used for a DBMS realization.
Traditional data models
Hierarchical Model
Network Model
Relational Model
Main factors of the data model
Data structure
Data operation
Integrity constraints
6
数据模型的组成要素
数据结构
 指所研究数据集合及数据间的联系
 是对系统静态特性的描述
数据操作
 对数据库中各类数据允许执行的操作及有关的操作规则
• 检索、更新(包括插入、删除、修改)
 是对系统动态特性的描述
数据的约束条件
 一组数据及其联系所具有的制约规则,用以限定符合数
据模型的数据库状态以及状态的变化,以保证数据的正
确、有效、相容
7
E-R Model
The concept model is represented by
Entity-Relationship(E-R)diagrams.
The E/R model
 Describes kinds of data and how they
connected.
 Can be converted to relational model later.
8
概念模型基本概念
 实体(Entities)
 客观存在并可相互区别的事物,可具体,可抽象
• 例:职工、学生、系
 属性(Attribute)
 实体具有的某一特性
• 例:学生实体有学号、姓名、性别、出生年份等属性
 码(Key)
 唯一标识实体的属性集
• 例如 学号是学生实体的码
 域 (Domain)
 某属性的取值范围
 实体集(Entity Set)
 同型实体的集合
• 如 全体学生
9
概念模型的基本概念
实体间的联系(Relationship)
 现实世界中事物内部以及事物之间的联系在信息世
界中反映为实体内部的联系和实体之间的联系
• 例 张山选修数据库课程。
• 学生实体和课程之间通过选修联系
两个实体之间的联系:




一对一联系(1:1)
一对多联系(1:n)
多对一联系(n:1)
多对多联系(m:n)
10
Entity Sets
Entity = “thing” or object.
Entity set = collection of similar entities.
 Similar to a class in object-oriented languages.
Attribute = property of (the entities of) an entity
set.
 Attributes are simple values, e.g. integers or
character strings.
11
E/R Diagrams
In an entity-relationship diagram:
 Entity set = rectangle.
 Attribute = oval, with a line to the rectangle
representing its entity set.
12
Example
name
address
Stars
Entity set Stars has two attributes, name
and address.
Each star has values for these two
attributes.
13
Relationships
A relationship connects two or more entity
sets.
It is represented by a diamond, with lines to
each of the entity sets involved.
14
Example
title
year Filmtype
Movies
Stars_in
address
name
Stars
length
The arrow pointing to entity set
studios indicates that each movie
is owned by a unique studio.
Owns
P41
name
Studios
address
15
Relationship Set
The current “value” of an entity set is the set
of entities that belong to it.
 Example: the set of all stars in our database.
The “value” of a relationship is a set of lists
of currently related entities, one from each of
the related entity sets.
16
Example
For the relationship Stars_ in, we might have a
relationship set like:
Movies
Basic Instinct
Stars
Sharon Stone
Total Recall
Arnold
Schwarzenegger
Sharon Stone
Total Recall
17
Multiway Relationships
Sometimes, we need a relationship that
connects more than two entity sets.
18
A Typical Relationship Set
Studios
Universal Studio
Movies
King Kong
stars
Jack Black
19
Many-Many Relationships
In a many-many relationship,
an entity of either set can be
connected to many entities of
the other set.
 E.g., a star can appear in many
movies; a movie can have more
than one star.
20
Many-One Relationships
Some binary relationships are many
-one from one entity set to another.
 Each entity of the first set is connected
to at most one entity of the second set.
 But an entity of the second set can be
connected to zero, one, or many
entities of the first set.
21
Example
Owns, from Movies to Studios is manyone.
 A movie is owned by at most one studio.
 But a studio owns any number of movies.
22
One-One Relationships
In a one-one relationship, each entity of either
entity set is related to at most one entity of the
other set.
Example: Relationship Runs between entity sets
Studios and Presidents.
 A president can run only one studio and a studio has
only one president.
23
Representing “Multiplicity”
Show a many-one relationship by an arrow
entering the “one” side.
Show a one-one relationship by arrows
entering both entity sets.
24
实体联系图示
一对一、一对多、多对一、多对多
25
Attributes on Relationships
Sometimes it is useful to attach an attribute
to a relationship.
Think of this attribute as a property of
tuples in the relationship set.
26
Example
studios
Movies
Contracts
stars
salary
It is appropriate to associate a salary with the (star,
movie, studio) triple in the relationship set for the
Contracts relationship.
27
Keys
A key is a set of attributes for one entity set such
that no two entities in this set agree on all the
attributes of the key.
 It is allowed for two entities to agree on some, but not
all, of the key attributes.
We must designate a key for every entity set.
Keys in E/R Diagrams: Underline the key
attribute(s)
28
Example: a Multi-attribute Key
dept
number
hours
room
Courses
• Note that hours and room could also serve as a
key, but we must select only one key.
29
Design Techniques
1. Avoid redundancy.
2. Don’t use an entity set when an attribute
will do.
30
Avoiding Redundancy
Redundancy occurs when we say the same
thing in two or more different ways.
Redundancy wastes space and (more
importantly) encourages inconsistency.
 The two instances of the same fact may become
inconsistent if we change one and forget to
change the other.
31
Example: Good
title
Movies
year
name
Owns
addr
Studios
This design gives the name of each studio exactly once.
32
Example: Bad
title
Movies
year
name
Owns
addr
Studios
StudioName
This design states the name of each studio twice: as an attribute
and as a related entity.
33
Example: Bad
title
year
Movies
StudioName
StudioAddr
This design repeats the studio’s address once for each
movie and loses the address if there are temporarily no
movie for a studio.
34
Entity Sets Versus Attributes
 An entity set should satisfy at least one of
the following conditions:
 It is more than the name of something; it has
at least one nonkey attribute.
or
 It is the “many” in a many-one or many-many
relationship.
35
Example: Good
title
Movies
name
Owns
addr
Studio
•Studio deserves to be an entity set because of
the
nonkey attribute addr.
•Movies deserves to be an entity set because it is the
“many” of the many-one relationship Owns.
36
Example: Good
title
StudioName
Movies
There is no need to make the studio an entity set,
because we record nothing about studios besides their
name.
37
Example: Bad
title
Movies
name
Owns
Studios
Since the Studios is nothing but a name, and is not at
the “many” end of any relationship, it should not be an
entity set.
38
两个实体型间的联系
班级
1
班级
m
1
班级-班长
1
课程
组成
选修
n
n
班长
学生
学生
1:1联系
1:n联系
m:n联系
39
多个实体型间的联系
 多个实体型间的一对多联系
 若实体集E1,E2,...,En存在联系,对于实体集Ej(j=1,2
,...,i-1,i+1,...,n)中的给定实体,最多只和Ei中的一个
实体相联系,则Ei与E1,E2,...,Ei-1,Ei+1,...,En之间是
一对多联系
 实例:课程、教师与参考书三个实体型
 多个实体型间的一对一联系
 多个实体型间的多对多联系
课 程
1
m
教 员
讲
授
n
参考书
40
课堂思考
三个实体间的联系
判断联系种类
供应商
m
n
项 目
供
应
p
零 件
41
同一实体集内各实体间的联系
一对多联系
 实例
• 职工实体集内部具有领导与被领导的联系
• 某一职工(干部)“领导”若干名职工,一个职
工仅被另外一个职工直接领导
• 这是一对多的联系
职 工
一对一联系
多对多联系
1
n
领导
42
Other Example





矩形:表示实体集
菱形:表示联系集
线:连接实体集与联系集或属性与实体集
椭圆:表示属性
下划线:主码属性
43
学生实体例
学生实体及属性
学生
学号
姓名
性别
出生年月
入学时间
系
44
联系及其属性例
课程
供应商
m
供应
选修
零 件
项 目
供应量
成绩
n
学生
45
实例练习
实体
 系、班级、学生、教研室、教师、课程
给出概念模型:E-R图
46
Reading Guide
Required: 2.2,2.5
Recommended: 2.3,2.8 ,《数据库系统概论》第一章
47
Exercises
To manage the materials of a factory, please give the E-R diagrams. Such
entity sets are involved as below:
 Warehouse: with attributes as Number, area and telephone
 Accessory : with attributes as number, Name, Specification, price
and description
 Supplier: with attributes as Number, name, addr, telephone and
account
 Project : with attributes as number and budget
 Employee : with attributes as number, name and age
The relationships among these entity sets are listed as below:
 A kind of accessory can be deposited in more than one warehouse. A
warehouse can accommodate more than one kind of accessory.
 There can be more than one employee in a warehouse, and a employee
can work in only one warehouse.
 Some employee can leader others.
 A supplier can supply various accessories to various projects. A
project can use accessories supplied by various suppliers. A kind of
accessory can be supplied by various supplier.
48