0% found this document useful (0 votes)
331 views28 pages

E-R Diagrams Tutorial

An entity-relationship diagram (E-R diagram) is a graphical representation of data for an organization at the conceptual level. E-R diagrams are used by database designers to build conceptual data models. Entities map to tables in a database and attributes map to fields. Relationships link entities and are based on key attributes. The basic components of an E-R diagram are entities, attributes, and relationships.

Uploaded by

Robin Rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
331 views28 pages

E-R Diagrams Tutorial

An entity-relationship diagram (E-R diagram) is a graphical representation of data for an organization at the conceptual level. E-R diagrams are used by database designers to build conceptual data models. Entities map to tables in a database and attributes map to fields. Relationships link entities and are based on key attributes. The basic components of an E-R diagram are entities, attributes, and relationships.

Uploaded by

Robin Rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CHAPTER

\
\ ' *.
\ \ N

._ . \'¢\

. i v ;

# J
i i
W
" ! f »
I
I
IH
Yf
I
»'; f ; 1,} ¢

'
1

E-R Diagrams
I'/$':'9
,f .

An entity-relationship diagram (E-R diagram) is a graphical representation


of data for an organization at conceptual level.

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.

The basic constructs of an E-R diagram are entities, attributes and


relationship, with symbols shown in Fig.6.l.

'Meaning _ |
Symbol
Entity
Entity

Attribute
. s
Attrlbute

Key Attribute
Key Attribute I

Relationship
Relationship
1. 1

Fig,6_1 Symbols used in E-R diagrams

Quick Check 6.1

When converted into logical database,


entities are mapped into 7

attributes are mapped into


Chapter 6 E-R Diagrams 73

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.

Relationship is a glue that holds together the various components of an


E-R diagram.
Note
Do not confuse relationship

I A. Identifying Entities and Relationships with relation: A relation is


a table. A relationship links
up two tables.
In this book, an entity wll'l be represented by single noun (e.g. STUDENT,
CLUB, CERTIFICATE, etc.) and a relationship will be represented by a verb in
present tense (e.g. Buys, Attends, Borrows etc.). Fig.6.2 shows the entities and
relationships identified for some daily life examples. In these examples, some
entities and relationships are not obvious, and you need to filter out unwanted
information.

Example Entities Relationship


1. An employee works in a EMPLOYEE, Works _in
department. DEPARTMENT
2. A student studies English, Chinese, STUDENT, SUBJECT Studies
Mathematics, Computer or
Economics.
3. An employee has three children. EMPLOYEE, CHILDREN Has

4. Some teachers are assigned a car TEACHER, CAR PARK Is_assigned


park.
5. An event in the sports day of a STUDENT, EVENT Participates
school will be cancelled if there are
no participants.
6. A company sells ten products. CUSTOMER, PRODUCT Orders
Customers can order products by
phone.
Quick Check 6.2
7. A teacher calculates the average STUDENT, REPORT CARD Has
mark of each student. The marks 1. An entity should
are recorded in report cards which have one or more
are sent to the parents.
and many possible
Fig.6.2 - Some daily-life examples of entities and relationship
2. A relationship is an

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

1. Guides to Identifying Entities

The following hints may be helpful to you in identifying entities:

1. Do not include specific data instances, like English, Chinese, etc. in


the 2nd example. Use an entity class instead.
2. Defer dealing with numbers, like "three children" in the 3rd example,
"some teachers" in the 4'** example and so on.
3. Ignore those data that do not have instances. In the 5111 example,
you will not record the sports day, but you will record events in the
sports day.
4. Ignore facts that can be derived. In the 6th example, "ten products
" can be derived using function COUNT. But, "product" should be
recorded.
5. Ignore calculated (derived) result. The average marks in the 7th
example will be calculated by the computer.
6. Ignore equipment needed to carry out a process. The "phone" in the
6th example can be ignored.

7. Bear in mind that a true entity will have many possible instances,
each with a distinguishing characteristic.

2. Guides to Naming Relationships

Defining a name for a relationship is the perhaps the most challenging


task for students. The following are some guidelines:

1. A relationship represents an action being taken. Therefore, a


relationship name is a verb phrase (see Fig.6.2) in present tense.

2. Relationship in E-R diagram should he read from left to right or


from up to down. Use passive voice if the action is not carried out
by the entity on the left. e.g. If SUBJECT is on the left and STUDENT
on the right, the name of the relationship should be Is_studied_by,
or simply Studied_by.
Quick Check 6.3
3. A relationship name states the action taken, not the result of
In identifying entities, ignore the action. e.g. use Worl<s_in, not Staff_of, use Studies, not
data without , data
Student_of, use Is_Assigned, not Assignment: use Participates not
that can be and
A_participant_of, use Orders not business etc.
equipment used to carry out a
4. Avoid using vague terms, like Is_related_to.
5. Ignore calculating processes. In the 7th example, calculating average
Quick Check 6A marks will be done by a computer.

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

181 Relationship Cardinality


Cardinality specifies the maximum and minimum numbers of instances of an Keypoinn
entity participating in an association with another entity.
Cardinality specifies the
maximum and minimum
numbers of instances of an
entity participating in an
1. Maximum Cardinality association with another
entity.

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

The typical example of one-to-many relationship is the relationship


between PUBLISHER and BOOK. A publisher may publish more than one book.
But, each book must be published by exactly one publisher.
The typical example of many-to-many relationship is the relationship
between CUSTOMER and PRODUCT. A customer may purchase more than
one type of product. A type of product may be purchased by more than one
customer.
Fig.6.3 shows the symbols used for specifying maximum cardinality.
Fig.6.4 shows some more examples.

mbol for Relationship


One-to-one 1 1
Relationship or I
I
Relationship
*al

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

nsnlp sample Relationships illustrated


One-to-one 1. A student may buy a lunch box. A lunch box Student : Lunch Box = 1:1
may be bought by one student.
2. A teacher may be in charge of a class. Each Teacher : Class 1:1
class must be in charge of by one teacher.
3. A student has one and only one student card. Student : Student Card = 1:1
Each student card is owned by one and one
only student.
One-to-many 1. A student may borrow some books from Student : Book = I LM
the library. A book in the library may be
borrowed by a student.
2. A summer course is attended by one or more Summary Course : Student
students. Each student may apply for at most = l:M
one summer course.
3. A student may be assigned to some Student : Functional Post
functional posts (monitor, perfect or club l :M
chairman). Each functional post is assigned
to one and only one student.
4. A class is formed by a group of at least one Class : Student = 1:M
student. A student is allocated to one and
only one class.
Many-to-many 1. A student may apply for some scholarships. Student : Scholarship = M:N
Each scholarship may be applied for by
some students.
2. An event in the sports day is participated by Event : Student = M:N
at least one student (Otherwise the event will
be cancelled). Each student may participate
in some event.
3. A student takes at least at least one course. A Student 2 Course = M:N
course is taken by at least one student.

Fig.6.4 Examples of three basic cardinality

Besides the maximum number of instances in a relationship, cardinality


also specifies the minimum number of instances, using terms: mandatory and
optional.

2. Mandatory Cardinality

An instance is mandatory (symbol I ) if there exists at least one


instance in the relationship. Terms like must, at least one and one and only one
are used to describe mandatory cardinality.
For example, a class must have a class teacher. Therefore, TEACHER is
mandatory M the relationship between CLASS and TEACHER. However, CLASS
is not mandatory because some teachers may not be a class teacher.
Another example is the relationship between HKID (HKID card) and
PERSON (permanent resident in HK). The relationship is mandatory in both
directions because every HKID card must have an owner and a permanent
resident must have an HKID card.
Chapter 6 E-R Diagrams 17

The above two examples illustrate mandatory cardinality in one-to-one


relationships. The relationship between CUSTOMER and ORDER is a mandatory Keypoints
one-to-many relationship. CUSTOMER is mandatory because a sale order must
There are four possible
be submitted by a customer. However, ORDER may or may not be mandatory, cardinalities: mandatory one,
depending on the company policy, which is called business mle. Some mandatory many, optional
companies treat potential customers who have asked for price quotation as one and optional many.
customers. But, some do not.

3. Optional Cardinality

An instance is optional (symbol \_//`\ ) if the instance may or may not


e>dst. Terms like may, or none and at most one are used to describe optional
cardinality.
Consider a school music concert in which students may buy any number
of tickets. The relationship between STUDENT and TICKET in both directions
are optional. A student may not buy any ticket. Also, a ticket may not be sold
out at all.
In conclusion, there are four possible cardinalities: mandatory one,
mandatory many, optional one and optional many, as shown in Fig.6.5. See
more examples in Fig.6.6.

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

of A teacher may or may not be in charge of a


la III llllll I1 J class. But, each class must be in charge of by
one teacher.
3. r 1 1 One-to-one relationship; Mandatory instances
STUDENT I
I Has STUDENT CARD on both sides.

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.

_ I A student may be assigned to some posts, like


monitor, perfect or club chairman. Each post
must be responsible by one student,
7. One-to-many relationship; Mandatory instances
CLASS
l » A r I
I STUDENT
on both sides.
A class is formed by a group of at least one
v*
I III II I I
student. Each student is allocated to one and
only one class.
8. M M Many-to-many relationship; Optional instances
STUDENT C on both sides.
App1ies_for
K C SCHOLARSHIP
I

A student may apply for more than one


I I I 'll
scholarship. Each scholarship may receive some
applications from students, or none.
9.
i
M M Many-to-many relationship; Optional instance
EVENT A
\./
Participated I
I STUDENT on one side.
_by An event in the sports day is participated by
I I la Ill ...J I II l I II II IIUI
at least one student (Otherwise the event will
be cancelled). Each student may participate in
some event.
10. M M Many-to-many relationship," Optional instance
STUDENT
I
l Takes I
l COURSE on one side.
.4 I A student takes at least one course. A course is
-__.-- .. I
/r I'll II l I I'll I II I I
taken by at least one student.

Fig.6.6 Ten examples of relationships


Chapters E-R Diagrams 79

Example 1 In a school, students are allocated to different classes. Each student


must be allocated to exactly one class, and a class is formed by at least
30 students. Each class must be managed by several different students,
namely, prefect, 1st monitor, 2nd monitor and 3rd monitor. Draw an E-R
diagram for the school, indicating cardinality.
Solution The first step is to classify the prefect and monitors into an entity
class, CLASS_POST. The relationship between CLASS and CLASS_POST is
mandatory in both directions because each class must have several posts
and these posts cannot e>dst without a class. The relationship between
STUDENT and CLASS is also mandatory in both directions, because
student must be allocated to one class and a class must consist of at least
one student. A student may or may not be assigned to a post. Therefore,
it is optional on the side of CLASS_POST. A post must be assigned to a
student. Therefore, it is mandatory on the side of STUDENT.

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

Example3 A library is going to use a computer system to handle the circulation.


Each user may borrow several books at each time. Each book is recorded
separately by a librarian into a check-out record, which contains data
about the book, borrower librarian, and dates (borrow and return).
There are three librarians working on three shifts.
(a) Draw an E-R diagram to show the relationships between BOOK and
USER, indicating cardinality.
(b) Identify two other entities for the library information system and
draw another E-R diagram to represent the relationships between
these entities, showing the cardinality of each relationship.

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 direction from LIBRARIAN to CHECK_OUT is optional because


a librarian may never handle any check-out (e.g. newly employed or
the system is just started). However, the direction from CHECK_OUT
to LIBRARIAN is mandatory because each check-out must involve
one librarian. Similarly, each check-out must involve one book and
one user.
Chapter 6 E-R Diagrams 81

IP- lDegree of Relationship Keypoints 1

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)

Fig.6.10 Unary relationship

In Fig.6.10a, a person may (or may not) be married to another person. If


they are married, they have exactly one spouse. Therefore, the relationship is
one-to-one and optional in both directions.
In Fig.6.lob (Read the relationship from top to bottom through the
relationship name), an employee may supervise more than one staff. The term
"may" means that an employee may or may not supervise other employee at all.
Therefore, optional-many is used below EMPLOYEE. When the statement is read
in the reverse order, an employee must be supervised by another employee. In
our example, each staff belongs to one department each of which must have a
department head. Therefore, mandatory-one is used above EMPLOYEE.
Ternary (degree 3) relationship and higher degree relationships are
beyond the scope of this book. Fig.6.1l shows the typical shape of ternary
relationship.

I
. | '

.|

Quick Check 6.7


Fig.6.11 Ternary relationship
The numbers of entities
involved in unary, binary and
ternary relationships are 7

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

..| I I I'll Illlll I ill lllllll I ...J

(a)

M M
STUDENT -C Applies_for O SCHOLARSHIP
...-.. I
(b)

Fig.6.11 Two relationships between two entities

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

An attribute is shown using an oval connected to an entity. Key attributes


are underlined. When an entity is transformed into a database table, the key
Keypoints
attribute will be the primary key of the table. Foreign keys are usually not
drawn in E-R diagrams.
A relationship is formed by linking the foreign key of a table to the
primary key of another table. In general, if a relationship has been drawn
to link two entities and the first entity has the key attribute, then it is not
necessary to draw the foreign key for the second entity. In other words, the
second entity is assumed to have an attribute with values matching the key
attribute of the first entity.

The following are some guidelines in drawing attributes:

1. Use noun to represent attributes.


2. Attributes must be unique.
3. Every basic entity must have a key attribute. Basic entities are
entities originally identified and represent fundamental objects in
an organization, like STUDENT, BOOK, LIBRARIAN etc.
4. An attribute that represents the foreign keys of a relationship need
not be drawn (see Example 4 below).
5. Attributes derivable from other attributes need not be drawn. For
example, if Date_birth has been recorded, Age or Year_birth are
derived attributes that can be ignored (see Example 4 below).
6. Clearly indicate multi-valued attributes. For example, Qualification
is a multi-valued attribute for a teacher because a teacher
may obtain several qualifications. Use double-oval to indicate
multi-valued attributes (See Example 5 below).

Example 4 In a school, a student may be assigned with one or more functional


posts, like prefect, monitor or chairman. A post must be assigned to
exactly one student. Draw a complete E-R diagram to represent the
relationship between STUDENT and POST, indicating cardinality.
Solution

@@ ( Post_1D
Post ID I (Post_Name)
j
/ Quick Check 6.8
1 M r

STUDENT : Assigned to O POST 1. Foreign keys need/need

/ \ al-ull J not* be drawn in E-R


diagrams.
Fig.6.13 I Address) lDate_birth ) 2. Derived attributes
need/need not* be drawn
in E-R diagrams.
3.
The entity POST has foreign key stud_ID. But, we don't need to draw attributes are represented
foreign key in E-R diagram, because the relationship has associated by double-oval.
stud _ID with Post_ID. Also, we don't need to include the derived 4. entities
attributes Year _birth in STUDENT (see Fig.6.14). must have a key attribute.
*delete inappropriate
84 Part B Dotobcse Design

Stud ID Name Post_ID ) (Post_Name)

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

Example 5 In a school, a teacher teaches in one or more classes, each of which


is taught by one or more teachers. A teacher may have more than
one qualification. On the other hand, each class must be responsible
(lead) by one class teacher. Sketch an E-R diagram to show the
relationship between TEACHER and CLASS, indicating cardinality.
Solution
Alternative 1 Single relationship

(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 ))

Alternative 2 Multiple relationships

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

The difference between the above diagrams is that Class teacher_ID


is added to CLASS in Fig.6.l5a. This is necessary because the
relationship "Teaches" does not represent the class teacher.
In Fig.6.15b, Class_teacher_ID needs not be drawn explicitly because
a new relationship is added to associate ClassID with Teacl'1er_ID
primarily to describe class teacher.
Chapter 6 E-R Diagrams 85

< Warehouse_ID ) I District > Product_ID J I Product_Name )

M M
I
Stores I
WAREHOUSE I I PRODUCT
M II I l II Il 1 la II I

Quantity J Fig.6.16 A relationship with


an attribute

Some relationships may have attributes. This commonly occurs in


many-to-many relationships. Let's consider the M:N relationship between Keypoinls
WAREHOUSE and PRODUCT shown in Fig.6.16. A warehouse may store
Relationships with attributes
several lands of products, and each kind of product may be stored in several should be converted into
warehouses. Attribute Quantity is added to the relationship to record the entities.
quantity of a kind of product in a warehouse. Entity converted from
a relationship is called
As a general rule, relationships with attributes should be converted associative entity.
into entities. The newly created entity is known as associative entity. The
The primary key of an
entity is associative because it is converted from a relationship which is an associative entity is formed
associationbetween two entities. It is an entity because it has instances each by a combination of the
with distinguishing attribute values (see 6.1 for requirements for an entity). foreign keys.

A feature of an associative entity is that the primary key is formed by


a combination of two foreign keys. As mentioned in 6.2, foreign keys are not
shown in E-R diagrams. Therefore, associative entities are drawn without any
key attribute.
Fig.6.l7 and 6.18 show two equivalent E-R diagrams in which a new entity
STORAGE is created to replace the original relationship Stores.

< Warehouse__ID ) District J Product_ID n < Product_Name 3

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

lWarehouse_ID ) District n Product__ID > I Product_Name ) Quick Check 6.9

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

Quantity > Fig.6.18 Equivalent E-R


diagram
86 Pqr|- B Database Design

Example 6 A joint project is hold by a school and a voluntary service


organization. The organization offers three types of voluntary
services to students in the school. Students may participate in more
than one such services. But, the teachers will adjust the services
chosen by students so that the number of participants in each service
is more or less the same. Score will be calculated for each student
based on the number of hours worked and the type of services at the
end of the school year.
(a) Draw an E-R diagram to show the relationship between
STUDENT and SERVICE and include the necessary attribute(s)
that enable(s) the score to be calculated, indicating cardinality.

(b) Re-draw the E-R diagram with a new entity converted from the
relationship in (a).
Solution (a)

( Stud_ID J ( Name ) (Sewice_ID) (Se1vice_Name )

\ / M M
I A
STUDENT I Works u SERVICE

I
I Hours_worked 3

Fig.6.19

(b)

Stud ID Name I ( Hours_worked 3 lService_ID) lService_Name)

1 M M 1 \ /
STUDENT : \ Works .o ASSIGNMENT I I
I Is for : SERVICE

Fig.6.20
Chapter 6 E-R Diogroms 87

The ultimate purpose of drawing E-R diagrams is to design a database.


Some entities can be transformed into tables directly, while others need some
conversions.
For instance, consider an entity with a multi-valued attribute. Since we do
not know the number of attribute values in each instance (record), we have to
use one row for each attribute value. As a result, an instance will have multiple
rows. Or, a row will have multiple rows. This is not allowed M relational
databases.
Keypoints
Resolution is to convert
Similarly, many-to-many (M:N) relationships cannot be implemented an E-R diagram into a
directly without conversion. In an M:N relationship, we cannot tell the number form transformable into a
of rows for a foreign key field in each record. This ends up with multiple rows relational database.
in a row, similar to the case of multi-valued attributes above. An M:N relationship must be
converted into multiple l:M
The process of converting an E-R diagram into a form that makes it relationships, by creating a
possible to transform into a relational database is called resolution. You will new entity.
see that all resolutions involve creating new entities. In fact, the discussion
in 6.3 is a resolution that converts an M:N relationship into multiple 1:M
relationships by creating an associative entity.
We shall discuss the resolutions of three types of relationships: binary
M:N relationship, multi-valued attribute and unary M:N relationship.

IA. Resolving Binary M:N Relationship

As a general rule, an M:N relationship must be converted into multiple


1:M relationships. The technique is to create a new entity to replace the
original M:N relationship.
Fig.6.2l shows a binary M:N relationship between CUSTOMER and
PRODUCT: A customer may have purchased more than one product. On the
other hand, a product may be purchased by more than one customer.

I Cust_ID) lCus_\amc ) Date Quantity Prod_ID J escription)

/
M M
A
CUSTOMER v Orders l PRODUCT
IIII II II

Quick Check 6.10

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

Case 1 Each sales order includes one product only

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:

I Cust _ID lCus_IIamc ) (Invoice-No0 I Quantity Prod_ID ) (Description

1 /
1 M M
I I
CUSTOMER I ORDER O : PRODUCT
I I llllll I'll sly I IIII

Date

Fig.6.22 Two One-to-many relationships

In Fig.6.22, ORDER is converted from the relationship Orders in Fig.6.21.


You need not create a new primary key for ORDER because you can make use
of the unique combination of foreign keys, cust_ID and Pr0d_ID, to form a
primary key. Yet, in our example, Invoice_No is included just to make it more
close to real business situations.
Note: The many-relationship of CUSTOMER in Fig.6.21 has been changed into
an one-relationship in Fig.6.22. The same happens to PRODUCT too. This
is a rule of thumb which you can use to verify that your resolution is
correct.

Case 2 Each sales order includes more than one product


If a customer can purchase more than one product in each sales order,
the E-R diagram will still contain M:N relationship as shown in Fig.6.23. Details
are removed for clarity purpose.

1 M M MI
CUSTOMER I I ORDER O I PRODUCT

Fig.6.23 Three basic entities

It is necessary to further resolve the relationship between ORDER and


PRODUCT. Since an invoice often contains several lines each describing one
product, we add an entity called ORDER_LINE as shown in Fig.6.24.
Quick Check 6.1 I

In resolving an M:N
relationship, a new
is created, where the primary
key is formed by
Chapl'er 6 E-R Diagrams 89

( Cust_ID ) (Cus Name Invoice No Quantity

\
1 M M_ I
CUSTOMER E ORDER I ORDER LINE

l ,1 II
M
Date

.1 ( PrOd ID
)
PRODUCT
Description
I I

Fig.6.24 Resolved E-R diagram

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.

Example 7 In a hospital, a group of physicians are responsible for admitting


new patients, while all the physicians will treat patients. Each
new patient is admitted by exactly one admitting physician. Once
admitted, a patient must be treated by at least one physician, who
may or may not be the admitting physician. Whenever a patient
is treated by a physician, the hospital wishes to record the details
of the treatment, including Date, Time, and Results. Draw an E-R
diagram without M:N relationships for the hospital, indicating
cardinality.
Solution
Step 1: Sketching two relationships between PHYSICIAN and PATIENT

( Phy_ID ) Admits Pat ID

M
\ 1

PHYSICIAN PATIENT

M M
Treats
Phy_Name (°at_Name )

Fig.6.2? Relationships between physicians and patients


90 Part B Database Design

Step 2: Resolving the M:N relationship into multiple 1:M relationships

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

Date ( Time >( Result II


Fig.6.26 Resolved relationships between physicians and patients

1§11 Resolving Multi-valued Attribute


Keypoints
As explained before, multi-valued fields are not allowed in relational
Multi-valued fields are database. The technique is to create a new entity to represent the multi-
not allowed in relational
valued attribute.
database.
A new entity is created to For example, a company may have several contact persons. The attribute
represent the multi-valued Contact _Name is multi-valued as shown in Fig.6.27.
attribute.

(Company_ID) I Name >

COMPANY (( Contact_Name ))

Fig-
6.27 -Entity with a multi-valued attribute

To resolve multi-valued attribute for this situation, create a new entity


CONTACT_PERSON as shown below:

lCompany_IDJ ( Name > < Contact _Name m

Quick Check 6.12 \ /1 I M


I
In resolving multi-valued COMPANY i Contacts i CONTACT PERSON
attributes, a new I
1 ll.ll .I
is created, where the primary
key is formed by Fig.6.28Resolved relationship
Chapter 6 E-R Diogrcxms 91

Another example of multi-valued attribute is the qua1ification(s) (e.g.


Bachelor degree, master degree, teacher training) owned by a teacher (see
Fig.6.29a). The attribute Qualification should be stored inside a new entity
QUALIFICATION which describes the qualifications in general. Quali_ID is a
unique code that represents a general qualification, like "B.Sc (Hons)".

(Tea¢het_1D)
( Name ) ( Ouali ID
) lDescr*iption

\ I
TEACHER 4 Qualification 9 QUALIFICATION
Fig.6.29 Multi-valued
qualification
(a) (b)

The relationship Is_certified between TEACHER and QUALIFICATION is


many-to-many (see Fig.6.30).

(Teacher_ID) < Name ) ( Ouali ID


3 (Description
I
M M
\ /
TEACHER C Is_certified1% QUALIFICATION
J Fig.6.30M:N relationship

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.

Teacher ID Name ( Qua1i_ID ) (Description)

_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

c. Resolving Unary M:N relationship

In a company, an employee always co-operates with other employees.


Keypoims Therefore, we have the unary M:N relationship as shown in Fig.6.32.
Unary M:N relationship must
be resolved by creating a new
entity. M
( Emp_ID )

EMPLOYEE l Cooperates
< Name
Name I
I

M
Fig.6.32 An unary M:N
relationship

As employees are worldng in groups while they co-operate with one


another, we can create a new entity GROUP to resolve the relationship .
Case 1 Each employee is assigned to one group only

Emp_ID Name > I Group_ID )

M 1
EMPLOYEE I Cooperates I GROUP

Fig.6.33 Resolved relationship

Case 2 Each employee may be assigned to more than one group


Since the relationship Cooperate5_in is an M:N relationship as
shown in Fig.6.34a, we need to resolve it further into two l:M
relationships, as shown in Fig.6.34b.

Emp_ID >( Name > I Group_ID )

M M
I
I
EMPLOYEE I Cooperates I GROUP

(a) M:N relationship

Emp_ID Name I Group_ID )

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

Example 8 A university has a large number of courses in its catalog. Each


course may have one or more different courses as prerequisites, or
may have no prerequisites. Similarly, a particular course may be a
prerequisite of any number of courses, or may not be prerequisite of
any other course.
(a) Sketch an E-R diagram that includes COURSE as the single
entity, indicating cardinality.
(b) Sketch another E-R diagram that includes COURSE and
PREREQUISITE as entities. State the necessary attribute in
PREREQUISITE.

Solution (a)

M
Course _ID 1
COURSE I
Requires 4

< Course_Name y
MI

Fig.6.35 E-R diagram with one single entity

(b)

( Required_Course_ID J
Course_ID J
\ 1 M
COURSE l Requires o PREREQUISITE
| I
/
I Course_Name )

Fig.6.36 Resolved relationship


94 Part B Dotcbose Design

Answers to Quick Check 6


Quick Check 6.1 Quick Check 6.7

tables, Holds 1, 2, 3
Quick Check 6.2 Quick Check 6.8

1. attributes, instances 1. need not


2. association, entities, key attribute 2. need not
3. Multi-valued
Quick Check 6.3 4. basic

instances, derived, process Quick Check 6.9

Quick Check 6A relationship, a combination of the foreign keys

calculation, transfer, send, summarize Quick Check 6.10

Quick Check 6.5 1. relational table


2. 1 :M
1. l:M (An author could write more than one
book, each book is written by one author) Quick Check 6.1 I
2. 1:1 (Each book has an ISBN, each ISBN
identities a book) entity, combining the foreign keys of two tables
3. MIN (An order may involve more than one
book, a book may appear in more than one Quick Check 6.12
order)
entity, combining the foreign key with its own key
attribute
Quick Check 6.6

1. mandatory (Books must have an author, An Quick Check 6.13


author must have written a book)
2. BOOK (An order cannot have no book, A entity
book may never appear in any order)
Chapter 6 E-R Diagrams 95

4 An entity-relationship (E-R) diagram is a graphical representation of data


for an organization at conceptual level.
4 The requirements for an entity are
1. one or more attributes
2. many possible distinct instances.
4 A relationship is an association between two entities based on a key
attribute.
0 Cardinality specifies the maximum and minimum numbers of instances of
an entity participating in an association with another entity.
O There are three basic types of relationships that specify the maydmum
number of instances: one-to-one (111), one-to-many(l:M) and many-to-many
(M:N) .
4 There are four possible cardinalities: mandatory one, mandatory many,
optional one and optional many.
4 The degree of a relationship is the number of entity classes participating in
that relationship.
o Binary relationships (degree 2) involve two entities and are the most
common. Unary relationships (degree 1) involve one entity.
4 If a relationship has been drawn to link two entities and the first entity has
the primary key, then it is not necessary to draw the foreign key for the
second entity.
O Relationships with attributes should be converted into entities.
o Entity converted from a relationship is called associative entity.
4 For an associative entity, the primary key is usually formed by a
combination of the foreign keys.
0 Resolution is a process of converting an E-R diagram into a form that makes
it possible to transform into a relational database.
0 An M:N relationship must be converted into multiple 1:M relationships. The
technique is to create a new entity to replace the original M:N relationship.
Q Multi-valued fields are not allowed in relational database. The technique is
to create a new entity to represent the multi-valued attribute.
0 Unary M:N relationship must be resolved by creating a new entity.
96 ParT B Database Design

Multiple Choice Questions

1. An E-R diagram represents


A. a data model.
B. a database schema.
C. an entity.
D. a relationship.

2. A binary relationship involves


A. two relationships.
B. two different entities.
c. two attributes.
D. more than two entities.

3. When a resolution takes place,


A. a new entity is created.
B. a new attribute is created.
c. a relationship is converted into an attribute.
D. an entity is removed.

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.

5. Which of the following types of relationships should be resolved?


A. one-to-one binary
B. one-to-many binary
c. many-to-many binary
D. relationship without any attribute

6. Which of the following types of attributes should be resolved?


A. key attribute
B. non-key attribute
C. single-valued attribute
D. multi-valued attribute

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

Study the paragraph below to answer the followirlgfive questions:

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

8. Which of the following can be included as an entity?


A. date of interview
B. paper card
c. symptoms
D. the clinic

9. Which of the following should NOT be included as an attribute?


A. name of client
B. date of interview
c. diagnostic results
D. Causeway Bay

10. The type of relationship in the direction from CLIENT to INTERVIEW is


A. one-to-one
B. one-to-many
c. many-to-one
D. many-to-many

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

12. Which of the following should be an attribute included in INTERVIEW?


A. name of client
B. diagnostic results
C. name of the doctor
D. number of visits by a client

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

2. In an auction of paintings, all participants must be registered, recording their names,


HKID, addresses and contact phone numbers. The paintings come from several famous
painters. Each painting has a detailed description. Each painter has one or more paintings
to be auctioned. Participants may purchase one or more paintings, or may not purchase
any painting.
Sketch an E-R diagram to represent auction, resolving all the N:M relationships.
98 Port B Database Design

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:

CONCERT Each concert represents a performance of one or more compositions


on a given date. Identifier is Concer"t_ID. Each concert must be
conducted by one conductor.
COMPOSITION Compositions to be performed at each concert. Identifier is
Composition_ID. Other attributes include name of composer, name
of composition.
CONDUCTOR Person who will conduct the concert. Identifier is Conductor_ID.
Other attributes include name of conductor. A conductor may conduct
more than one concert, or may not conduct any concert.

An initial E-R diagram is drawn as follows:

CONDUCTOR Conducts CONCERT Includes COMPOSITION


lllll ll lllll l J

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:

MEMBER A member of the tennis club. Identifier is Member _ID. Other


attributes are name of member and contact phone.
COURT A tennis court to be used by members. Identifier is Court_ID.

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

6. The principal of a school wants to record the siblings (brothers/sisters) of students


studying in the same school currently. Siblings of students who are not studying in the
same school currently need not be recorded.
(a) Draw an initial E-R diagram with one entity to represent the data model about
siblings of students.
(b) Resolve your E-R diagram so that it can be implemented in a relational database.
Chapter 6 E-R Diagrams 99

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

STUDENT Takes COURSE


I l 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.

8. The following describes a real estate Finn:


* The firm has a number of sales offices in several districts. Attributes of sales office
include Office _ID and Location.
O Each sales office is assigned one or more employees. Attributes of employee include
Employee_ID and Employee_Name. An employee must be assigned to only one
sales office.
4 For each sales office, there is always one employee assigned to manage that office.
O The firm lists property for sale. Attributes of property include Property_ID and
Location.
O Each unit of property may be listed in one or more sales offices.

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

You might also like