E-R Diagrams Tutorial
E-R Diagrams Tutorial
\
\ ' *.
\ \ N
._ . \'¢\
. i v ;
# J
i i
W
" ! f »
I
I
IH
Yf
I
»'; f ; 1,} ¢
'
1
E-R Diagrams
I'/$':'9
,f .
Keypoints
An E~R diagram is a graphical E-R diagrams are frequently used by database designers to build data
representation of data for an models in the process of database development (see Conceptual Data Modeling
organization at conceptual in 5.2). A well-drawn E-R diagram can be transformed into relational database
level.
tables easily: Entities will be mapped into tables and attributes will be mapped
into fields.
There are some software tools that help people produce E-R diagrams
and generate database schema (see Chapter 7) automatically.
'Meaning _ |
Symbol
Entity
Entity
Attribute
. s
Attrlbute
Key Attribute
Key Attribute I
Relationship
Relationship
1. 1
Recall that an entity is a person, place, device, event OI' concept and an Keypoints
attribute is a property of an entity class. The requirements for an entity are
An entity has one or more
1. one or more attributes attributes, and many possible
2. many possible distinct instances. distinct instances.
A relationship is an
association between two
A relationship is an association between two entities based on a key attribute. entities based on a key
attribute.
As you can see, identifying entities and relationships for a data model is between two
a process of filtering and integrating. It requires certain common sense, based on a
14 Part B Dotobose Design
7. Bear in mind that a true entity will have many possible instances,
each with a distinguishing characteristic.
In identifying relationships, 6. Ignore business activities like transfer, send, summarize. In the 7th
ignore process which example, "sending report cards to parents" should be ignored,
involves 7 because this activity is irrelevant to the design of a database
and business activities, like system. Remember that you should choose a relationship which is a
7 and true association between two entities.
Chapter 6 E-R Diagrams 15
There are three basic types of relationships with the maximum number
of instances specified: one-to-one (1:1), one-fo-many (1:M) and many-to-many
(M:N) . Note
The typical example of one-to-one relationship is the relationship A many-to-one relationship
between HKID (HKID card) and PERSON (permanent resident in Hong Kong). is an one-to-many
Each HKID card is issued to one and only one permanent resident. Each relationship in opposite
permanent resident is issued one and only one HKID card. direction
One-to-many 1 M
Relationship or
I \ Relationship <
Many-to-one M 1
Relationship or > \ Relationship 4
Many-to-many M M
Quick Check 6.5
Relationship or > \ Relationship <
1. AUTHOR and BOOK is
an relationship.
Fig.6.3 Symbols for maximum cardinality
2. BOOK and ISBN is an
relationship.
3. BOOK and ORDER is an
relationship.
76 Part B Database Design
2. Mandatory Cardinality
3. Optional Cardinality
I
Meaning Symbol for Relationship Cardinality
Mandatory One 1
L
One and only one I
I
or
II
----------.-w
Mandatory Many M
One Of" many
or
'Quinn-
Optional One 1
Zero, or one f'\
\J
or
}o
Ill Ll lIIII.| I I'll I I JI
Optional Many M
Quick Check 6.6
0, 1, 2, . o or >o
I
1. In the relationship
between AUTHOR and
Fig.6.5 All possible cardinalities BOOK, both sides are
2. In the relationship
between BOOK and
ORDER, the mandatory
side is
and the optional side is
78 Port B Dotobcxse Design
I C. Sample relationships
lagram~ Remark
1. 1 1 One-to-one relationship; Optional instances on
STUDENT C Buys A
\J LUNCH BOX both sides.
l Student may buy at most one lunch box. Each
-ill IIII III .J I
lunch box may or may not be sold to a student.
2. P
1 1 One-to-one relationship; Optional instance on
TEACHER Is_in_charge O CLASS one side, mandatory instance on the other side.
: I
I II It II I I J ' I
Each student must have a student card and each
student card belongs to exactly one student.
4. 1 M One-to-many relationship; Optional instances
STUDENT C BOITOWS O BOOK
on both sides.
A student may borrow some books from the
library. A book in the library may be borrowed
by at most a student.
5. l M One-to-many relationship; Optional instance on
SUMMER one side.
COURSE
C __by : STUDENT
There must be at least one student for a summer
mu---»-J
course to exist. Enrollment by students is
optional. But, a student may apply for at most
one summer course.
6. 1 M One-to-many relationship; Optional instance on
STUDENT + Assigned_to O POST one side.
M 1
STUDENT I l Is_allocated I CLASS
I I
1 _to
I I
1 1
Is_managed
by
M
1
Is assigned O CLASS_POST
_to Fig.6.7
I
Example 2 Wing Lee Inc. is a construction company with over 1000 employees.
A customer can hire the company for more than one project, and
employees sometimes work on more than one project at a time.
Equipment is assigned to only one project. Draw an E-R diagram for
Wing Lee Inc., indicating cardinality.
Solution It is reasonable to assume that a construction project involves more than
one employee and requires more than one equipment.
EQUIPMENT
M T'
i
Assignedo 4
4-
1
1 M M M
CUSTOMER II Hires I
I PROJECT I Is_worked . I
I EMPLOYEE
by
pi
Fig.6.8
80 Port B Database Design
Solution (a)
M M
Is_checked_
BOOK O O USER
Fig.6.9a I I 4 1
out_by
I II J
The relationship between BOOK and USER is optional because a
book may never be borrowed out, and a user may never borrow any
book.
(b) Since each check-out has attributes, like date, borrower, etc.,
check-out is an entity. Another entity is librarian.
1 M M 1
Is checked Checked
BOOK ii out
C CHECK OUT o out_by
: USER
M r ¢~
Is handled
LIBRARIAN
Fig.6.9b
The degree of a relationship is the number of entities participating in The degree of a relationship
that relationship. A11 the relationships mentioned above involve two different is the number of entity
entities. Therefore, they are of degree 2 and called binary relationships. Binary classes participating in that
relationship.
relationships are the most CO1'I]IT1OI1.
Binary relationships involve
Unary (degree 1) relationship involves one entity. The following are two two entities and are the most
examples of unary relationship: common. Unary relationships
involve one entity.
1 1 I
PERSON \ Is married to EMPLOYEE Supervises
I I l -
1 M
(a) (b)
I
. | '
.|
and respectively.
82 Part B Database Design
E. Multiple Relationships
In some situations, there are more than one relationship between two
entities. Consider the following example:
1 M
STUDENT I
I Gets C SCHOLARSHIP
(a)
M M
STUDENT -C Applies_for O SCHOLARSHIP
...-.. I
(b)
Refer to Fig.6.11a. A student may get more than one scholarship (or
may not get any scholarship). A scholarship must be awarded to exactly one
student. Refer to Fig.6.1 lb. A student may apply for more than one scholarship.
A scholarship may be applied for by more than one student.
The two relationships can be drawn on the same E-R diagram as shown in
Fig.6.12.
Gets
1 M
STUDENT SCHOLARSHIP I
lI I
M M
App1ies_for
v
Fig.6.12 Multiple relationships
Chapler 6 E-R Diagrams 83
@@ ( Post_1D
Post ID I (Post_Name)
j
/ Quick Check 6.8
1 M r
M
\ /
*Il**.
1 Yea
s
STUDENT Assigned_to o POST
"*.
\
Fig.6.14 Stud_ID in POST
is a foreign key; Year_Birth in Address ( Date_birth )
STUDENT is a derived attribute
(Teacher_ID) ( Name
Name I ) I C1ass_ID ) (Class_teacherII)1
\ / M M
\ /
Note TEACHER Teaches I
CLASS
: I
Double-oval is used to J
indicate multi-valued /
attribute. Fig.6.15a I( Qualification ))
I C1ass_ID )
(Teacher_9 I Name > M M
\ l I Teaches
r
I
I
I l
TEACHER CLASS
1 1
Leads J
/ Ill-
: (as classteacher
I( Qualification
Fig.6.15b
M M
I
Stores I
WAREHOUSE I I PRODUCT
M II I l II Il 1 la II I
I 1 M M 1
1
WAREHOUSE II Is for l STORAGE I Records 4% PRODUCT
.I I
I
I I I I I
Fig.6.17 A relationship
Quantity m
converted into an entity
I 1 M M 1
I An associative entity
is converted from a
WAREHOUSE : : STORAGE I
I : PRODUCT . The
\/ I Illllll
J primary key of an associative
entity is
(b) Re-draw the E-R diagram with a new entity converted from the
relationship in (a).
Solution (a)
\ / M M
I A
STUDENT I Works u SERVICE
I
I Hours_worked 3
Fig.6.19
(b)
1 M M 1 \ /
STUDENT : \ Works .o ASSIGNMENT I I
I Is for : SERVICE
Fig.6.20
Chapter 6 E-R Diogroms 87
/
M M
A
CUSTOMER v Orders l PRODUCT
IIII II II
1. Multi-valued attributes
Fig.6.21 Many-to-many relationship cannot be represented in a
2. An M:N relationship
must be resolved into
two or more
relationships.
88 Port B Database Design
Assume that in each sales order, a customer will purchase one and only
product. We will add an entity ORDER to show the details of the purchase. The
E-R diagram is redrawn as follows:
1 /
1 M M
I I
CUSTOMER I ORDER O : PRODUCT
I I llllll I'll sly I IIII
Date
1 M M MI
CUSTOMER I I ORDER O I PRODUCT
In resolving an M:N
relationship, a new
is created, where the primary
key is formed by
Chapl'er 6 E-R Diagrams 89
\
1 M M_ I
CUSTOMER E ORDER I ORDER LINE
l ,1 II
M
Date
.1 ( PrOd ID
)
PRODUCT
Description
I I
Notice that ORDER_LINE does not need an extra primary key because the
combination of Invoice _No and Prod_ID forms a primary key. The attribute
Date is placed in ORDER because all the products in the same sales order are
sold on the same day. On the other hand, the attribute Quantity is placed in
ORDER_LINE because customers may require different quantities for different
products.
M
\ 1
PHYSICIAN PATIENT
M M
Treats
Phy_Name (°at_Name )
Phy_ID Admits
f
Pat_ID >
1
PHYSICIAN PATIENT
II I
l 1 1
Phy_Name
Handles .4 i TREATMENT t Is for
('at_Name J
M M
I
I
COMPANY (( Contact_Name ))
Fig-
6.27 -Entity with a multi-valued attribute
(Tea¢het_1D)
( Name ) ( Ouali ID
) lDescr*iption
\ I
TEACHER 4 Qualification 9 QUALIFICATION
Fig.6.29 Multi-valued
qualification
(a) (b)
Note
The subsequent steps will be similar to converting an M:N relationship It is possible to add some extra
into two l:M relationships. Fig.6.31 shows a new entity CERTIFICATE added. It information, like Cert_ID
is an entity converted from the relationship Is_certified. This entity does not and Year, to CERTIFICATE
require a new primary key, because it includes both foreign keys Teacher_ID in Fig.6.31 to make it more
and Quali_ID. The combination of these two foreign keys is sufficient to comprehensive. But, they are
uniquely identify records in CERTIFICATE. not a must.
_1 M M 1
\
TEACHER : Obtains o CERTIFICATE re Is for ml QUALIFICATION
I I J I .|
Fig.6.31 Resolved
relationship
The second example above is more complicated than the first one
because qualification is a general entity that can e>dst independently.
However, the contact persons are dependent on the company. While the same
qualification may be owned by different teachers, a contact person belongs to
exactly one company.
92 Port B Database Design
EMPLOYEE l Cooperates
< Name
Name I
I
M
Fig.6.32 An unary M:N
relationship
M 1
EMPLOYEE I Cooperates I GROUP
M M
I
I
EMPLOYEE I Cooperates I GROUP
l M M 1
I I I
Quick Check 6.13 EMPLOYEEIl I
COOPERATION I I
GROUP
J
In resolving an urinary M:N
(b) Final resolution
relationship, a new
is created Fig.6.34 Resolved relationship
Chapter 6 E-R Diagrams 93
Solution (a)
M
Course _ID 1
COURSE I
Requires 4
< Course_Name y
MI
(b)
( Required_Course_ID J
Course_ID J
\ 1 M
COURSE l Requires o PREREQUISITE
| I
/
I Course_Name )
tables, Holds 1, 2, 3
Quick Check 6.2 Quick Check 6.8
4. An associative entity
A. has a primary key.
B. is converted from a relationship.
c. represents a fundamental object.
D. has an association with other entities.
7. After resolution, which of the following should disappear from an E-R diagram?
A. N:M relationship
B. l:l unary relationship
c. l:l binary relationship
D. l:M binary relationship
A doctor owns a clinic located in Causeway Bay. There are approximately 100 clients. The
doctor records the name, address and phone number of each client on a paper card. For
each interview, the doctor will retrieve the paper card of the client and record the date,
symptoms, diagnostic results and medications on the paper card. Assume that there is a
set of well-known symptoms used by doctors. An E-R diagram is drawn to rind out the
overall structure of data during the phase of conceptual data modeling. In the diagram, a
relationship is set up between CLIENT and INTERVIEW.
Chap1*er 6 E-R Diagrams 97
11. The cardinalities on the side of CLIENT and INTERVIEW are and
respectively.
A. optional, optional
B. optional, mandatory
c. mandatory, optional
D. mandatory, mandatory
Conventional Questions
1. A credit-card center cooperates with some banks to offer credit cards to users. Fig.Q6.l is
an incomplete E-R diagram shown three entities: banks, credit cards and users. Each bank
issues a number of credit cards, each with a unique identification code and expiration
date. A credit card must be issued by one and only one bank and belong to one and only
one user. Each user must own at least one credit card. Users are identified by HKID and
banks are identified by a unique code. Complete the E-R diagram. Attributes like user
name, address, bank name should be included.
l
BANK
I l lll1lllll llll Ill
CREDIT CARD
LJ USER
Fig.Q6.1
3. Peter is a database designer, He is going to develop an E-R diagram for a concert hall in
Tung Chung. Three entities have been identified as follows:
Fig.Q6.2
(a) Complete the E-R diagram to show the attributes and cardinalities.
(b) Resolve the E-R diagram to eliminate any N:M relationship.
4. A private tennis club has ten tennis courts that allow members to use. Booking from
members is accepted within one week before the tennis court is used. In each booking,
each member can reserve at most 3 tennis courts and the duration is a multiple of
half-hour. Given that two entities are identified:
(H) Sketch an initial E-R diagram to show the relationship and cardinality between
MEMBER and COURT.
(b) Redraw the E-R diagram to include an entity BOOKING with attributes including
date and time of use and duration of booking.
5. In a country, all children must receive one vaccination against VXl. In Kinsman Hospital,
the job of vaccination is handled by a group of five nurses. Children who have received
the vaccination must be recorded so that they would not receive vaccination more than
once. The record includes the HKID of the parent. It also includes date and the name of
the nurse in charge of the vaccination.
(a) Explain why the HKID of parent cannot be a key attribute for entity children.
Hence, suggest a key attribute for the children.
(b) Draw an E-R diagram to represent the structure of data about vaccination in the
hospital.
7. In a post secondary institute, each student takes at least four courses. A course must
have at least one student and is taught by one and only one lecturer. Each student will be
assigned a supervisor who is the lecturer of one of the subjects taken by the student. The
diagram below shows three entities identified.
lLecturer_ID 3( Lecturer_Name )
\ /
LECTURER I
/
lCourse_ID) ( Course_Name H
( SMd_ID Stud_Name ,I ( Marks 3 Fig.Q6.3
(a) Complete the diagram to build a data model that represents the institute.
(b) Does your E-R diagram need resolution? If yes, state how resolution is carried out,
but do not draw a new E-R diagram.
(a) Draw an E-R diagram using the above entities to represent a data model for the Hrm.
(b) Does your E-R diagram needs resolution? If yes, state how resolution is carried out,
but do not draw a new E-R diagram.
9. Wing Kea Inc., is an engineering Hrm with approximately 300 employees. A database is
required to keep track of all employees, projects assigned and departments worked in.
O Attributes of each employee include a unique employee code, name, address and
job title (like engineer, secretary, and so on).
4 There are la different departments each with a unique name and a phone number.
An employee can report to only one department.
O Each department may deal with several vendors. A vendor typically supplies
equipment to more than one department. It is necessary to store the name and
address of each vendor and the date of last meeting between a department and a
vendor.
O Many employees can work on a project. Arr employee can only be assigned to at
most one project at a time. Projects are distinguished by project numbers and the
estimated cost of each project must be stored.
Using only four entities, draw an E-R diagram that models the data of the firm.