Transcript Document

Database Systems
DataBase System
Major Content & Grade
 Introduction
*
 The
***
Relational Model
 SQL
****
 Transaction
Management
***
 Database
Design (E-R)
***
 Database
Design (Normalization)
***
Haichang Gao , Software School , Xidian University
2
DataBase System
Ch06 Database design
 Part 1 Conceptual Design
 Part 2 Logical Design
Haichang Gao , Software School , Xidian University
3
DataBase System
Contents
 Part 1 Conceptual Design
 Introduction
 Modeling
 Constraints
 E-R
Diagram
 Design Issues
 Weak Entity Sets
 Extended E-R Features
 Design of the Bank Database
 Reduction to Relation Schemas
 Database Design
 UML
Haichang Gao , Software School , Xidian University
4
DataBase System
Introduction
 DataBase Design is a subject of research ever
since the late 1970s
 Database system typically have only a very
limited understanding of what the data in the
database means.
 Semantic model(语义模型)
Extended model managed to capture all of the
semantics of the situation under consideration
 Entity-relationship approach
Most commonly used in practice
Haichang Gao , Software School , Xidian University
5
DataBase System
Introduction
 Entity-Relationship model is a set of concepts and
graphical symbols that can be used to create
conceptual schemas.
 Versions
model — Peter Chen (1976).
 Extended E-R model — Extensions to the Chen model.
 Information Engineering (IE) — James Martin (1990);
it uses “crow’s foot” notation, is easier to understand.
 IDEF1X — A national standard developed by the
National Institute of Standards and Technology(NIST) .
 Unified Modeling Language (UML) — The Object
Management Group; it supports object-oriented
methodology.
 Original E-R
Haichang Gao , Software School , Xidian University
6
DataBase System
Modeling
 A database can be modeled as:
A
collection of entities(实体)
 Relationship(联系)
among entities.
 An entity is an object that exists and is
distinguishable from other objects.
Example: specific person, company, event, plant
 Entities have attributes(属性)
Example: people have names and addresses
Haichang Gao , Software School , Xidian University
7
DataBase System
Entity Sets
 An entity set (entity class, 实体集) is a set of entities
of the same type that share the same properties.
Example: set of all persons, companies, trees, holidays
customer_id customer_ customer_ customer_
name
street
city
loan_ amount
number
Haichang Gao , Software School , Xidian University
8
DataBase System
Entity Sets
 An entity is a collection of distinguishable realworld objects with common properties.
A
value of an entity is known as entity instance(实例), or
sometimes an entity occurrence(事件)
 Any
entity instance can be represented and distinguished
only by its own properties
Haichang Gao , Software School , Xidian University
9
DataBase System
Attributes
 Domain(域) – the set of permitted values for each
attribute
 Attribute types:
 Simple
and composite attributes(单一属性和组合属性).
 Single-valued attributes (单值属性) and
multi-valued attributes(多值属性)
 E.g.
 Derived
multivalued attribute: phone-numbers
attributes(派生属性)
 Can
be computed from other attributes
 E.g. age, given date of birth
Haichang Gao , Software School , Xidian University
10
DataBase System
Relationship Sets
 A relationship(联系) is an association among several
entities.
an ordered list of m entities, E1,E2,…,Em ( where the
same entity may occur more than once in the list), A
relationship set is a mathematical relation among n  2
entities, each taken from entity sets :
 Given
{(e1, e2, … en) | e1  E1, e2  E2, …, en  En}
where (e1, e2, …, en) is a relationship
Haichang Gao , Software School , Xidian University
11
DataBase System
Relationship Sets
Haichang Gao , Software School , Xidian University
12
DataBase System
Relationship Sets
 An attribute can also be property of a relationship set.
 For instance, the depositor relationship set between entity
sets customer and account may have the attribute accessdate
Haichang Gao , Software School , Xidian University
13
DataBase System
Relationship Sets
 Degree of a Relationship Set Refers to number of entity sets
that participate in a relationship set.
 Relationship sets that involve two entity sets are binary (or
degree two). Generally, most relationship sets are binary.
 Relationship sets may involve more than two entity sets.
 E.g.
Suppose employees of a bank may have jobs
(responsibilities) at multiple branches, with different jobs at
different branches. Then there is a ternary relationship set
between entity sets employee, job and branch.
 Relationships between more than two entity sets are rare.
Haichang Gao , Software School , Xidian University
14
DataBase System
Mapping Cardinalities
 Express the number of entities to which another
entity can be associated via a relationship set.
 Most useful in describing binary relationship sets.
 For a binary relationship set the mapping cardinality
must be one of the following types:
 One
to one(1:1)
 One
to many (1:M)
 Many
to one (M:1)
 Many
to many (M:N)
Haichang Gao , Software School , Xidian University
15
DataBase System
Mapping Cardinalities
One to one
One to many
Note: Some elements in A and B may not be mapped to
any elements in the other set
Haichang Gao , Software School , Xidian University
16
DataBase System
Mapping Cardinalities
Many to one
Many to many
Note: Some elements in A and B may not be mapped to
any elements in the other set
Haichang Gao , Software School , Xidian University
17
DataBase System
Mapping Cardinalities
 Can make access-date an attribute of account,
instead of a relationship attribute, if each account
can have only one customer.
Haichang Gao , Software School , Xidian University
18
DataBase System
Keys for Entity Sets
 A super key of an entity set is a set of one or more attributes
whose values uniquely determine each entity.
 A candidate key of an entity set is a minimal super key
 Customer_id
is candidate key of customer
 account_number
is candidate key of account
 Although several candidate keys may exist, one of the
candidate keys is selected to be the primary key.
Haichang Gao , Software School , Xidian University
19
DataBase System
Keys for Relationship Sets
 The combination of primary keys of the participating entity
sets forms a super key of a relationship set.
 (customer_id,
account_number) is the super key of depositor
 NOTE:
this means a pair of entity sets can have at most one
relationship in a particular relationship set.
 Must consider the mapping cardinality of the relationship
set when deciding what are the candidate keys
 Need to consider semantics of relationship set in selecting the
primary key in case of more than one candidate key
Haichang Gao , Software School , Xidian University
20
DataBase System
E-R Diagrams
 Rectangles represent entity sets.
 Diamonds represent relationship sets.
 Lines link attributes to entity sets and entity sets to relationship sets.
 Ellipses represent attributes
 Double ellipses represent multivalued attributes.
 Dashed ellipses denote derived attributes.
 Underline indicates primary key attributes
Haichang Gao , Software School , Xidian University
21
DataBase System
E-R Diagrams
 E-R Diagram With Composite, Multivalued, and Derived
Attributes
Haichang Gao , Software School , Xidian University
22
DataBase System
E-R Diagrams
 Relationship Sets with Attributes
Haichang Gao , Software School , Xidian University
23
DataBase System
Roles(角色)
 Entity sets of a relationship need not be distinct
labels “manager” and “worker” are called roles;
they specify how employee entities interact via the
works-for relationship set.
 The
Haichang Gao , Software School , Xidian University
24
DataBase System
Roles(角色)
 Roles are indicated in E-R diagrams by labeling the lines
that connect diamonds to rectangles.
 Role labels are optional, and are used to clarify semantics
of the relationship
Haichang Gao , Software School , Xidian University
25
DataBase System
Cardinality Constraints
 We express cardinality constraints by drawing
either a directed line (), signifying “one,” or an
undirected line (—), signifying “many,” between
the relationship set and the entity set.
Haichang Gao , Software School , Xidian University
26
DataBase System
One-To-One Relationship
 In the one-to-one relationship A customer is
associated with at most one (including 0) loan via the
relationship borrower, and A loan is associated with
at most one (including 0) customer via borrower .
Haichang Gao , Software School , Xidian University
27
DataBase System
One-To-Many Relationship
 In the one-to-many relationship A customer is
associated with several (including 0) loans via
borrower; A loan is associated with at most one
customer via borrower.
Haichang Gao , Software School , Xidian University
28
DataBase System
Many-To-One Relationships
 In a many-to-one relationship A customer is
associated with at most one loan via borrower;
A loan is associated with several (including 0)
customers via borrower.
Haichang Gao , Software School , Xidian University
29
DataBase System
Many-To-Many Relationship
 In a many-to-many relationship A customer is
associated with several (possibly 0) loans via borrower;
A loan is associated with several (possibly 0)
customers via borrower .
Haichang Gao , Software School , Xidian University
30
DataBase System
Ternary Relationship
 Suppose employees of a bank may have jobs
(responsibilities) at multiple branches, with different
jobs at different branches. Then there is a ternary
relationship set between entity sets employee, job and
branch.
Haichang Gao , Software School , Xidian University
31
DataBase System
Cardinality of Entity Participation
 Total
participation (indicated by double line): Every entity
in the entity set participates in at least one relationship in the
relationship set.

E.g. participation of loan in borrower is total, for EACH
loan must have a customer associated to it via borrower
 Partial participation:
Some entities may not participate in
any relationship in the relationship set.

E.g. participation of customer in borrower is partial
Haichang Gao , Software School , Xidian University
32
DataBase System
Cardinality of Entity Participation
 When an entity E takes part in a relationship R with min-
card(E,R)=x (x is either 0 or 1) and max-card(E,R)=y (y is either
1 or N), then in the E-R diagram the connecting line between E
and R can be labeled with the ordered cardinality pair (x,y). We
use a new notation to represent this minimum-maximum pair
(x,y): card(E,R) = (x,y).
Haichang Gao , Software School , Xidian University
33
DataBase System
Cardinality of Entity Participation
 Cardinality limits can also express participation constraints
 E.g.
participation of loan in borrower is total, for EACH loan
must have a customer associated to it via borrower ;
 participation of
customer in borrower is partial
 The
relationship borrower is one-to-many from customer to
loan .
Haichang Gao , Software School , Xidian University
34
DataBase System
Design Issues
 Use of entity sets vs. attributes
Choice mainly depends on the structure of the enterprise
being modeled, and on the semantics associated with the
attribute in question.
属性间不产生联系,当属性与其他实体有联系时,该属
性应作为实体。
职工
职工号
姓名
职工
职称
年龄
职工号
聘任
年龄
姓名
职称
职称名
工资
福利
住房
Haichang Gao , Software School , Xidian University
35
DataBase System
Design Issues
 Use of entity sets vs. attributes
当一个对象被表达为实体时,在其它实体中不能以属
性出现,只能用联系表达。
病人
住院号
姓名
病房
病房
病房号
位置
年龄
病人
住院号
姓名
年龄
住院
房位数
面积
病房
病房号
位置
房位数
面积
Haichang Gao , Software School , Xidian University
36
DataBase System
Design Issues
 Use of entity sets vs. relationship sets
Possible guideline is to designate a relationship set to
describe an action that occurs between entities
供应商
订单
供应商
供应量
供应
项目
订单
零件
项目
供应量
零件
Haichang Gao , Software School , Xidian University
37
DataBase System
Design Issues
 Binary vs. n-ary relationship sets
Although it is possible to replace any nonbinary (n-ary,
for n > 2) relationship set by a number of distinct binary
relationship sets, a n-ary relationship set shows more clearly
that several entities participate in a single relationship.
姓名
姓名
编号
学历
教师
姓名
学号
性别
年龄
编号
学历
教师
姓名
学号
学生
学生
代课
学习
学习
成绩
学分
课程名
成绩
课程
课程
课程号
性别
年龄
学分
课程号
课程名
Haichang Gao , Software School , Xidian University
38
DataBase System
Design Issues
 Placement of relationship attributes
customer_street
customer_name
account_number
access_date
customer_city
customer_id
one-to-many customer
0..N
customer_name
depositor
1..1
account
customer_street
account_number
customer_city
customer_id
customer
0..N
depositor
1..N
account
many-to-many
access_date
Haichang Gao , Software School , Xidian University
39
Database Systems
DataBase System
Contents
 Part 1 Conceptual Design
 Introduction
 Modeling
 Constraints
 E-R
Diagram
 Design Issues
 Weak Entity Sets
 Extended E-R Features
 Design of the Bank Database
 Reduction to Relation Schemas
 Database Design
 UML
Haichang Gao , Software School , Xidian University
41
DataBase System
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 (strong entity, 标识实体,强实体).
 It
must relate to the identifying entity set via a total, one-tomany relationship set from the identifying to the weak entity
set
Haichang Gao , Software School , Xidian University
42
DataBase System
Weak Entity Sets
 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.
 The primary key of a weak entity set is formed by the
primary key of the strong entity set on which the weak entity
set is existence dependent, plus the weak entity set’s
discriminator.
Haichang Gao , Software School , Xidian University
43
DataBase System
Weak Entity Sets
 We
depict a weak entity set by double rectangles.
 Identifying
relationship depicted using a double diamond.
 We
underline the discriminator of a weak entity set with
a dashed line.
 Example:
Primary key for payment : (loan_number,
payment_number)
Haichang Gao , Software School , Xidian University
44
DataBase System
Weak Entity Sets
 In a university, a course is a strong entity and a course-
offering can be modeled as a weak entity
 The discriminator of course-offering would be semester
(including year) and section-number (if there is more than
one section)
C#
Cname
Cridet
course
semester
offering
sectionnumber
classes
Courseoffering
Haichang Gao , Software School , Xidian University
45
DataBase System
Weak Entity Sets
 In a university, a course is a strong entity and a course-
offering can be modeled as a weak entity
 The discriminator of course-offering would be semester
(including year) and section-number (if there is more than
one section)
 If we model course-offering as a strong entity we would
model course-number as an attribute.
Then the relationship with course would be implicit in the
course-number attribute
sectionC#
Cname
Cridet
course
semester
number
classes
C#
offering
Courseoffering
Haichang Gao , Software School , Xidian University
46
DataBase System
Contents
 Extended E-R Features
 Specialization
 Generalization
 Aggregation
 Summary
of Symbols
Haichang Gao , Software School , Xidian University
47
DataBase System
Specialization(特殊化)
 Top-down design process - we designate subgroupings within
an entity set that are distinctive from other entities in the set.
 These subgroupings become lower-level entity sets that have
attributes or participate in relationships that do not apply to the
higher-level entity set.
 Depicted by a triangle component labeled ISA (E.g. customer
“is a” person).
person
ISA
customer
Haichang Gao , Software School , Xidian University
48
DataBase System
Specialization(特殊化)
 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.
Example of entity type hierarchy
(not E-R Diagram)
Haichang Gao , Software School , Xidian University
49
DataBase System
Specialization Example
Haichang Gao , Software School , Xidian University
50
DataBase System
Generalization(一般化,泛化)
 A bottom-up(自底向上) design process– combine a
number of entity sets that share the same features into a
higher-level entity set.
 Specialization and generalization are simple inversions of
each other; they are represented in an E-R diagram in the
same way.
 The terms specialization and generalization are used
interchangeably.
Haichang Gao , Software School , Xidian University
51
DataBase System
Specialization and Generalization
 Can have multiple specializations of an entity set
based on different features.
 E.g. permanent-employee vs. temporary-employee, in
addition to officer vs. secretary vs. teller
Haichang Gao , Software School , Xidian University
52
DataBase System
Specialization and Generalization
 Each particular employee would be
a
member of one of permanent-employee or temporaryemployee,
 and
also a member of one of officer, secretary, or teller
 The ISA relationship also referred to as superclass subclass relationship
Haichang Gao , Software School , Xidian University
53
DataBase System
Aggregation
 Relationship sets works_on and manages represent
overlapping information
 Every
manages relationship corresponds to a works_on
relationship
 However,
some works_on relationships may not correspond
to any manages relationships
Haichang Gao , Software School , Xidian University
54
DataBase System
Aggregation
 Eliminate this redundancy via aggregation:
 Treat
relationship as an abstract entity
 Allows
relationships between relationships
 Abstraction
of relationship into new entity
Haichang Gao , Software School , Xidian University
55
DataBase System
Aggregation
 Without introducing redundancy, the following diagram
represents:
 An
employee works on a particular job at a particular branch
 An
employee, branch, job combination may have an
associated manager
Haichang Gao , Software School , Xidian University
56
DataBase System
E-R Design Decisions
 The use of an attribute or entity set to represent an object.
 Whether a real-world concept is best expressed by an
entity set or a relationship set.
 The use of a ternary relationship versus a pair of binary
relationships.
 The use of a strong or weak entity set.
 The use of specialization/generalization – contributes to
modularity in the design.
 The use of aggregation – can treat the aggregate entity set
as a single unit without concern for the details of its
internal structure.
Haichang Gao , Software School , Xidian University
57
DataBase System
Haichang Gao , Software School , Xidian University
58
DataBase System
Summary of Symbols Used in ERD
Haichang Gao , Software School , Xidian University
59
DataBase System
Summary of Symbols -Cont.
Haichang Gao , Software School , Xidian University
60
DataBase System
Alternative E-R Notations
Haichang Gao , Software School , Xidian University
61
DataBase System
Contents
 UML
Haichang Gao , Software School , Xidian University
62
DataBase System
UML
 UML: Unified Modeling Language
 UML has many components to graphically model
different aspects of an entire software system
 UML Class Diagrams correspond to E-R Diagram,
but several differences.
Haichang Gao , Software School , Xidian University
63
DataBase System
UML
Haichang Gao , Software School , Xidian University
64
DataBase System
UML
 Entity sets are shown as boxes, and attributes are shown
within the box, rather than as separate ellipses in E-R diagrams.
 Binary relationship sets are represented in UML by just
drawing a line connecting the entity sets. The relationship set
name is written adjacent to the line.
 The role played by an entity set in a relationship set may also
be specified by writing the role name on the line, adjacent to
the entity set.
 The relationship set name may alternatively be written in a
box, along with attributes of the relationship set, and the box
is connected, using a dotted line, to the line depicting the
relationship set.
 Non-binary relationships drawn using diamonds, just as in
ER diagrams
Haichang Gao , Software School , Xidian University
65
DataBase System
UML
* Note reversal of position in cardinality constraint depiction
* Generalization can use merged or separate arrows independent
of disjoint/overlapping
Haichang Gao , Software School , Xidian University
66
DataBase System
UML
 Cardinality constraints are specified in the form l..h, where l
denotes the minimum and h the maximum number of
relationships an entity can participate in.
 Beware: the positioning of the constraints is exactly the reverse
of the positioning of constraints in E-R diagrams.
 The constraint 0..* on the E2 side and 0..1 on the E1 side means
that each E2 entity can participate in at most one relationship,
whereas each E1 entity can participate in many relationships; in
other words, the relationship is many to one from E2 to E1.
 Single values, such as 1 or * may be written on edges; The
single value 1 on an edge is treated as equivalent to 1..1, while *
is equivalent to 0..*.
Haichang Gao , Software School , Xidian University
67
DataBase System
Mapping E-R to Relation Schemas
 Primary keys allow entity sets and relationship sets to be
expressed uniformly as relation schemas that represent the
contents of the database.
 A database which conforms to an E-R diagram can be
represented by a collection of schemas.
 For each entity set and relationship set there is a unique
schema that is assigned the name of the corresponding entity
set or relationship set.
 Each schema has a number of columns (generally
corresponding to attributes), which have unique names
Haichang Gao , Software School , Xidian University
68
DataBase System
Representing Entity Sets as Schemas
 Each entity in an E-R diagram is mapped to a single table in a
relational database.
 The
columns of the table represent all the single-valued
attributes that are attached to the entity;
 Composite
attributes are flattened out by creating a separate
attribute for each component attribute;
A
multivalued attribute M of an entity E is represented by a
separate schema EM
 Derived
attributes are not represent in the relational shema.
Haichang Gao , Software School , Xidian University
69
DataBase System
Representing Entity Sets as Schemas
 Relational Schema
 customer(
customer-id, first-name, middle-initial, last-name,
street-number, street-name, apartment-number, city,state, zipdoce, date-of-birth )
 customer-phone( customer-id, phone-number)
Haichang Gao , Software School , Xidian University
70
DataBase System
Representing Entity Sets as Schemas
 A strong entity set reduces to a schema with the same attributes.
 A weak entity set becomes a table that includes a column for the
primary key of the identifying strong entity set
 Relational Schema
 loan(loan_number,
amount)
 payment(loan_number, payment_number, payment_date,
payment_amount ) 注:依赖联系已体现!
Haichang Gao , Software School , Xidian University
71
DataBase System
Representing Relationship Sets as Schemas
 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.
 Relational Schema
 customer(…),
loan(…)
 borrower (customer_id, loan_number )
Haichang Gao , Software School , Xidian University
72
DataBase System
Representing Relationship Sets as Schemas
 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.
 Relational Schema
 branch
(branch-name, branch-city, assets)
 account (account-number, balance, branch-name)
Haichang Gao , Software School , Xidian University
73
DataBase System
Representing Relationship Sets as Schemas
 For one-to-one relationship sets, either side can be chosen to
act as the “many” side
 If participation is partial on the “many” side, replacing a
schema by an extra attribute in the schema corresponding to
the “many” side could result in null values.
 Relational Schema
 ……
Haichang Gao , Software School , Xidian University
74
DataBase System
Representing Specialization via Schemas
Haichang Gao , Software School , Xidian University
75
DataBase System
Representing Specialization via Schemas
 Method 1:
 Form
a schema for the higher-level entity
 Form
a schema for each lower-level entity set, include
primary key of higher-level entity set and local attributes
Haichang Gao , Software School , Xidian University
76
DataBase System
Representing Specialization via Schemas
 Method 1:
 Relational Schema
 person(
name, street, city )
 customer(
name, credit_rating )
 employee(
name, salary )
 ......
Haichang Gao , Software School , Xidian University
77
DataBase System
Representing Specialization via Schemas
 Method 2:
 Form
a schema for each entity set with all local and
inherited attributes
Haichang Gao , Software School , Xidian University
78
DataBase System
Representing Specialization via Schemas
 Method 2:
 Relational Schema
 person(
name, street, city )
 customer(
name, street, city, credit_rating )
 employee(
name, street, city, salary )
 ......
Haichang Gao , Software School , Xidian University
79
DataBase System
Schemas Corresponding to Aggregation
 To represent aggregation, create a schema containing
 primary
key of the aggregated relationship,
 the
primary key of the associated entity set
 any
descriptive attributes
Haichang Gao , Software School , Xidian University
80
DataBase System
Schemas Corresponding to Aggregation
 Relational Schema
 ......(略去相关实体转换的关系模式)
 manages
(employee_id, branch_name, title, manager_name)
 NOTE: Schema works_on is redundant provided, we willing to
store null values for attribute manager_name in manages.
Haichang Gao , Software School , Xidian University
81
DataBase System
Design of the Bank Database
Haichang Gao , Software School , Xidian University
82
DataBase System
作业
 某医院为了规范人员、财务、药品等的日常管理,决定采用数据
库技术实现医院信息系统。现负责人提出要求如下:
1. 医院有若干个科室,科室信息有科室编号,科室名称,楼
层等;
2. 医院药品信息有药品名称,厂商,售价等;
3. 医生有统一的编号、姓名、性别、职称等信息,并属于某
一个科室,门诊时间由医院统一安排;
4. 病人看病时选择科室、医生进行挂号,医生根据病情开具
处方,处方具体包含病人姓名,年龄,性别,日期,药品
清单。
根据以上需求,完成该医院信息系统的设计及编程(要求使用上
文中的名称,如需引入新名称,必需使用与实际意义相符的中文
名称):
a) 画出系统的完整E-R图;
b) 将你的E-R图转换为关系模式
 P256 6.2 , 6.21
Haichang Gao , Software School , Xidian University
83