1 chapter 2 database modeling data models concept model & data model entity-relationship...

48
1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

Upload: shon-george

Post on 13-Dec-2015

262 views

Category:

Documents


18 download

TRANSCRIPT

Page 1: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

1

Chapter 2

Database Modeling

Data Models

Concept Model & Data Model

Entity-Relationship diagrams

Page 2: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

2

What are Data Model?

Computer simulation of the real world. To abstract, denote and transact the

information in the real world.

Page 3: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

3

Data Model should :

Simulate the real world comparatively truly Be easy to understand Be easy to be implemented by computer

Page 4: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

4

Abstraction and ModelingTwo 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.

现实世界 机器世界

概念模型 数据模型客观对象

信息世界

Page 5: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

5

The classification of the models

Concept modelAlso 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.

Page 6: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

6

Data modelModeling data from the viewpoint of computer.Mainly used for a DBMS realization.Traditional data models

Hierarchical ModelNetwork ModelRelational Model

Main factors of the data modelData structureData operationIntegrity constraints

Page 7: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

7

数据模型的组成要素 数据结构

指所研究数据集合及数据间的联系 是对系统静态特性的描述

数据操作 对数据库中各类数据允许执行的操作及有关的操作规则

• 检索、更新(包括插入、删除、修改) 是对系统动态特性的描述

数据的约束条件 一组数据及其联系所具有的制约规则,用以限定符合数

据模型的数据库状态以及状态的变化,以保证数据的正确、有效、相容

Page 8: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

8

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.

Page 9: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

9

概念模型基本概念 实体( Entities)

客观存在并可相互区别的事物,可具体,可抽象• 例:职工、学生、系

属性( Attribute ) 实体具有的某一特性

• 例:学生实体有学号、姓名、性别、出生年份等属性 码( Key )

唯一标识实体的属性集• 例如 学号是学生实体的码

域 ( Domain ) 某属性的取值范围

实体集( Entity Set ) 同型实体的集合

• 如 全体学生

Page 10: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

10

概念模型的基本概念 实体间的联系( Relationship )

现实世界中事物内部以及事物之间的联系在信息世界中反映为实体内部的联系和实体之间的联系

• 例 张山选修数据库课程。• 学生实体和课程之间通过选修联系

两个实体之间的联系: 一对一联系 (1:1) 一对多联系 (1:n) 多对一联系 (n:1) 多对多联系 (m:n)

Page 11: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

11

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.

Page 12: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

12

E/R Diagrams

In an entity-relationship diagram: Entity set = rectangle. Attribute = oval, with a line to the rectangle

representing its entity set.

Page 13: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

13

Example

Entity set Stars has two attributes, name and address.

Each star has values for these two attributes.

Stars

name address

Page 14: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

14

Relationships

A relationship connects two or more entity sets.

It is represented by a diamond, with lines to each of the entity sets involved.

Page 15: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

15

Example

Filmtype

P41

The arrow pointing to entity set studios indicates that each movie

is owned by a unique studio.

Studiosaddressname

Stars

addressname

Movies

title

length

year

Stars_in

Owns

Page 16: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

16

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.

Page 17: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

17

Example

For the relationship Stars_ in, we might have a relationship set like:

Movies Stars

Basic Instinct Sharon Stone

Total Recall Arnold Schwarzenegger

Total Recall Sharon Stone

Page 18: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

18

Multiway Relationships

Sometimes, we need a relationship that connects more than two entity sets.

Page 19: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

19

A Typical Relationship Set

Studios Movies stars

Universal Studio King Kong Jack Black

Page 20: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

20

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.

Page 21: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

21

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.

Page 22: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

22

Example

Owns, from Movies to Studios is many-one. A movie is owned by at most one studio. But a studio owns any number of movies.

Page 23: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

23

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.

Page 24: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

24

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.

Page 25: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

25

实体联系图示一对一、一对多、多对一、多对多

Page 26: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

26

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.

Page 27: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

27

Example

Movies starsContracts

salary

It is appropriate to associate a salary with the (star, movie, studio) triple in the relationship set for the Contracts relationship.

studios

Page 28: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

28

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)

Page 29: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

29

Example: a Multi-attribute Key

Courses

dept number hours room

• Note that hours and room could also serve as a key, but we must select only one key.

Page 30: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

30

Design Techniques

1. Avoid redundancy.

2. Don’t use an entity set when an attribute will do.

Page 31: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

31

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.

Page 32: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

32

Example: Good

This design gives the name of each studio exactly once.

Movies StudiosOwns

title name addryear

Page 33: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

33

Example: Bad

Movies StudiosOwns

title

This design states the name of each studio twice: as an attribute and as a related entity.

name addryear

StudioName

Page 34: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

34

Example: Bad

This design repeats the studio’s address once for each movie and loses the address if there are temporarily no movie for a studio.

Movies

title year StudioName

StudioAddr

Page 35: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

35

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.

Page 36: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

36

Example: Good

Movies StudioOwns

title

•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.

name addr

Page 37: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

37

Example: Good

Movies

title

There is no need to make the studio an entity set, because we record nothing about studios besides their name.

StudioName

Page 38: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

38

Example: Bad

Movies StudiosOwns

title

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.

name

Page 39: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

39

两个实体型间的联系 班级

班级 -班长

班长

1

1

1:1联系

班级

组成

学生

1

n

1:n联系

课程

选修

学生

m

n

m:n联系

Page 40: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

40

多个实体型间的联系 多个实体型间的一对多联系

若实体集 E1 , E2 , ... , En 存在联系,对于实体集Ej ( j=1 , 2 , ... , i-1 , i+1 , ... , n )中的给定实体,最多只和 Ei 中的一个实体相联系,则 Ei 与 E1 , E2 , ... , Ei-1 , Ei+1 , ... , En 之间是一对多联系

实例:课程、教师与参考书三个实体型 多个实体型间的一对一联系 多个实体型间的多对多联系

课 程

教 员 参考书

讲 授1

m n

Page 41: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

41

课堂思考 三个实体间的联系 判断联系种类

供应商

项 目 零 件

供 应

m

n p

Page 42: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

42

同一实体集内各实体间的联系 一对多联系

实例• 职工实体集内部具有领导与被领导的联系• 某一职工(干部)“领导”若干名职工,一个职

工仅被另外一个职工直接领导• 这是一对多的联系

一对一联系 多对多联系

职 工

领导

1 n

Page 43: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

43

Other Example 矩形:表示实体集 菱形:表示联系集 线:连接实体集与联系集或属性与实体集 椭圆:表示属性 下划线:主码属性

Page 44: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

44

学生实体例 学生实体及属性

学生

学号 姓名 性别 出生年月 入学时间 系

Page 45: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

45

联系及其属性例

供应商

供应

项 目 零 件

供应量

课程

选修

学生

m

n

成绩

Page 46: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

46

实例练习 实体

系、班级、学生、教研室、教师、课程 给出概念模型: E - R 图

Page 47: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

47

Reading Guide

Required: 2.2,2.5 Recommended: 2.3,2.8 , 《数据库系统概论》第一章

Page 48: 1 Chapter 2 Database Modeling Data Models Concept Model & Data Model Entity-Relationship diagrams

48

ExercisesTo 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 ageThe 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.