Edi 104 - Chapter 3
Edi 104 - Chapter 3
3
Entity Relationship Modeling
Subtopics:-
Entity Relationship (ER) diagram is a graphical diagram used at the design stage of database system’s
development for gathering data.
ER diagram consists of three elements, which are entities, attributes and relationship, and they are each
associated to a notation or symbol for constructing a meaningful ER diagram.
However, the attribute, of which consists of another three sub-types, named as simple, multivalue,
composite and derived attribute are used as well for constructing more refined ER diagram.
Similarly, the relationship for connecting two entities comprises of description and cardinality, which
is the number of instances of one entity in relation to each instance of the related entity, to reflect the rule of
relationship to tackle business data’s requirements.
The relational keys and integrity rules, which are not notation of the ER Diagram, are studied here also
to anticipate the requirement to construct tables and setting the relationship between tables in Microsoft
Access, based on the ER diagram.
Entity Relationship Modeling is a process used to help us to understand and document the informational
requirements of a system.
For example, a database system of your university, college, or school will consist of information, such as
students and the relevant particulars that include their names, addresses, birth dates, courses, enrolments,
grades and etc.
To build the above mentioned database system, it consists of three steps, which are:
1. To gather the requirements and document the requirement using ER diagram.
2. Design the database system by transforming the developed ER model to the database design that
consists of tables, relationship and constraints.
3. Implement the system by capturing data for the system and put the system in good use.
Entity Relationship Diagram is a graphical diagram that depicts entity relationship model that comprises
entities, attributes and relationship. It also displays connectivity and cardinality.
Before looking into the Entity Relationship Diagram, let look into the example below to learn the association
of data and the three key concepts of Entity Relationship Diagram, that is, entities, attributes and
relationship.
2|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example: A Car Purchasing Model
The diagram above shows a car purchasing model that involves the relationship between the factory, car and
customer. Here, we can see clearly that the factory produces a particular car that has been purchased by a
customer.
- Entities
- Attributes (which include: simple, multivalued, composite, and derived)
- Relationship
3|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.1.3.1 Notations of ER Model
4|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.1.3.2 Entity
Definition
- Entity is a person, place, object, event or concept, which you want to store the information in a table.
3
Characteristic of entities:
- Must have an attribute and unique key.
- Will become a table in a database.
5|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Classification of Entities:
Entity Examples
1. code: P01
Name: Access 2000 Processing
Publisher Code: M-G1
Group Type: Database
Price: 76.00
In Store: Yes
2. Code: P02
Name: Concepts of Database Management
Publisher Code: P-H1
Group Type: Database
Price: 100.00
In Store: No
3.1.3.3 Attribute
6|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Relationship
7|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.1.3.4 Cardinality Constraints
The diagram above shows the hierarchy of En. Azhar's family. En. Azhar and Pn. Zainab have a daughter, 2
sons and 4 grandchildren. The relationship within his family members can be viewed clearly in this diagram.
The relationship between Azhar and Azlee is that Azlee is Azhar's son.
Cardinality Constraint
Example :
An EMPLOYEE (who is the manager) manages only one BRANCH at a time and each branch only has
one manager.
8|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
b) One to Many (1:M)
Definition: Maximum one X for each Y and possibly many Y’s for each X.
Example :
One FACULTY of a university offers many COURSES for students but every course must be
offered by one faculty. (menwarkn byk course)
Definition: Possibly many X’s for each Y and maximum one Y for each X.
Example:
Every day, many STUDENTS study in one CLASS ROOM at a time, but one class room is used by
many students at a time.
[Employee -> work in -> office]
d) Many to Many (M:N)
Possibly many X’s for each Y and many Y’s for each X.
9|P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Figure 3-12 Combination of ER Diagram (i) and (ii): A BOOK has one or more AUTHORS and an
AUTHOR writes one or more BOOKS.
Both Primary Keys attributes from BOOK & AUTHOR (ISDN & Author_ID) will become the attributes
for Composite Entity / Conceptual Entity (AUTHORING).
Examples of Relationship
Example:
One department has many employees.
10 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example:
The employee handles project.
Solution:
11 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example:
The publisher publishes books.
Solution:
12 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example:
A lecturer teaches one or more subjects and many subjects are taught by lecturers.
Solution:
13 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example:
Students register for at least a course. Attributes for student are Student_ID and Student_Name, while the
attributes for course are Course_Code and Course_Name.
Solution:
14 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.1.4 ER Diagrams
Books at MyBook bookstore are recorded. Information of the publishers and the authors is recorded to make
sure that the searching process for the book is easier.
Complete ER diagram
15 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example 2: ER Model of Borrowing Book
The picture above shows a student borrowing a book. The book was written by an author.
Complete ER diagram
16 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example: ER Model of Department, Employee and Project
The picture above show one department that has many workers and each worker is responsible for one
project.
Complete ER diagram
17 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.2 Relational Keys and Integrity Rules
Relational key enables us to link the tables together in a database for easier data access storage.
Example:
b) Below is the example of Primary key referring to the table in MyBook database.
18 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Figure 3-35 Primary Key
Publisher_Code is a PRIMARY KEY for table PUBLISHER and Book_Code is the PRIMARY
KEY for table BOOK. Both primary keys can uniquely identify a row in a relational table.
19 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
c) Below is the example of foreign key referring to the table in MyBook database.
Publisher_Code is a PRIMARY KEY for PUBLISHER table but it is a FOREIGN KEY for
BOOK table.
For a table that have a M :1 or 1:M relationship, PRIMARY KEY for table (1) must be added
in table (M) as a FOREIGN KEY. *
20 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
d) Below is the example of Composite key referring to the table in MyBook database.
Combination of Book_Code and Author_No can determine the books that are written by the author.
Many to Many Relationship (M:N) will create a new table that inherits PRIMARY KEY from both
tables related to it.*
21 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.2.2 Types of Integrity Rules
Two types of integrity constraint are Entity Integrity and Referential Integrity.
a) Entity Integrity
Requirement:
All entries are unique and no part of a primary key may be null.
Purpose:
Guarantees that each entity will have a unique identity and ensures that foreign key values can properly
refer to primary key values.
Example :
Publisher_Code is the PRIMARY KEY in PUBLISHER table, so this field cannot have a null value and all
entries are unique.
Example:
Publisher_Code is the PRIMARY KEY in PUBLISHER table as shown below, so this field cannot have a
null value.
22 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
b) Referential Integrity
Referential integrity is a set of rules that prevents you from changing or deleting a record if matching
records exist in a related table.
Requirement :
If foreign key exist in one table then the foreign key value must reference an existing primary key value in
a table to which it is related and it can be null.
Purpose :
i) Possibility for non-corresponding value, but impossible for invalid entity.
ii) Referential integrity rule disables row deletion of a table consisting primary key matching key
values in another table.
Example :
Publisher_Code is a PRIMARY KEY for PUBLISHER table, but it is also a FOREIGN KEY for BOOK
table. Please note that FOREIGN KEY value in table BOOK must match the PRIMARY KEY value in
PUBLISHER table.
23 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
3.3 Steps to Develop ER Diagram
24 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example :
Example :
A company has several departments. Each department has a supervisor and at least one employee.
Employees must be assigned to at least one department. Project will be given to the employee and it will be
done in a group. At least one employee is assigned to one project or more. The important data fields are the
names of the departments, projects, supervisors and employees, as well as the supervisor and employee
number, department code and a unique project number.
a) Department
b) Supervisor
c) Employee
d) Project
25 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
b) Each department has at least one employee
26 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 4 : State cardinality constraint
b) Each department has at least one employee. Employees must be assigned to at least one department.
c) Project will be given to the employee and it will be done in a group. At least one employee is assigned to
a project or more.
27 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 5 : Identify primary key for each entity
Entity Attribute
DEPARTMENT Department_Name
EMPLOYEE Employee_Name
SUPERVISOR Supervisor_Name
PROJECT Project_Name
28 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example :
Create an ER diagram for the following requirements with the appropriate attributes for each entity type and
structural constraints for each relationship type.
(a) An INVOICE is written by a SALESREP. Each sales representative can write many invoices, but each
invoice is written by a single sales representative.
(b) The INVOICE is written for a single CUSTOMER. However, each customer can have many invoices.
(c) An INVOICE can include many details (DETAIL-LINES), which describe the products bought by the
customer.
a) Invoice
b) Sales Representative
c) Customer
d) Detail-Lines
e) Product
29 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 3 : ER diagram
a) Each sales representative can write many invoices, but each invoice is written by a single sales
representative.
30 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
b) The INVOICE is written for a single CUSTOMER. However, each customer can have many invoices.
c) An INVOICE can include many details (DETAIL), which describe the products bought by the customer.
31 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 5 : Identify primary key for each entity
33 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Example :
You have been requested by a travel agency, EXEL Travels, to build a database for storing information on
customers and vacation package that have been booked. Create an ER Diagram based on the information
below:
Each customer has information like Customer Number, Name, Address and Telephone number.
Customers may have many addresses and telephone numbers.
A customer may book more than one holiday package and one holiday package may also be booked
by many customers.
Examples of payments according to booked holiday package :
Package A : New Zealand (3 days) - RM 3500
Package B : New Zealand (7 days) - RM 5000
Package C : New Zealand (10 days) - RM5990
a) Customer
b) Holiday Package
Step 3 : ER diagram
34 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 5 : Identify primary key for each entity
Entity Attribute
CUSTOMER Cust_No, Cust_Name, Cust_Addr, Cust_Tel
HOLIDAY PACKAGE Package_Code, Location, Duration, Payment
Example :
Hospital Jaya is based in Kuala Lumpur. This Hospital has a few wards with several patients. This ward has
a few types, which depends on types of patients.
Patients will be recorded into file that have ID Patient, Patient name, address, phone number, IC-number,
categories either VIP or regular patient and a group of blood patient.
Each patient is treated by a doctor at one time but one doctor can treats a number of patients. The
information of doctors recorded is: Doctor ID, name, address, phone number, IC-number and specialization.
The nurses will be assigned to take care of the patients. Nurses in this hospital are graded based on their
experience, qualification and duration of working in the hospital.
35 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 1: Identify entity
a) Ward
b) Patient
c) Doctor
d) Nurse
b) Each patient is treated by a doctor at one time but one doctor can treats a number of patients.
Step 3: ER diagram
36 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 4 : State cardinality constraint
b) Each patient is treated by a doctor at one time but one doctor can treats a number of patients.
c) The nurses are assigned to a ward to take care of different patients during their stay.
37 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 5 : Identify primary key for each entity
38 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
The complete ER diagram
Beside the above method, ER diagram can also be creating based on the form or report that will be produce
by the database system that you create.
Example :
39 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 1 : List down the data (attribute) that can be the primary key
a) Customer No
b) Asset No
c) Owner No
Based on the primary key, we can determine the entity that may be appearing in the ER diagram.
Assumption:
Step 3 : ER diagram
40 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Step 4 : State cardinality constraint
41 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
c) Asset is owned by owner
Complete ER diagram
42 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
SUMMARY FOR CHAPTER 3 : ENTITY RELATIONSHIP MODEL
ER Model is used for constructing ER diagram based on the conventions of ER diagram and the user
data.
Notation of ER Model :
Entity is a person, place, object, event or concept, which you want to store the information in the
database.
Relational keys are device that relates one entity with another entity in a relationship.
43 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Types of keys :
Types Definition
Candidate Key An attribute or group of attributes that identify a unique row in a relation.
One of the candidate keys is chosen to be the primary key.
Primary Key Any of the candidates that are selected to be the key or an attribute that uniquely
identifies each row in a relational table.
Foreign Key An attribute in one table whose values must match the primary key in another
table or whose value must not be null.
Attribute that acts as a primary key in a related table.
Composite Key A primary key that consists of more than one attribute, especially in(M:N)
relationship.
Entity Integrity is used to make sure that all data entries are unique and no part of primary key can be
null.
Referential Integrity is a set of rules that prevents you from changing or deleting a record if matching
records exist in a related table.
ER diagram is a graphical diagram that depicts an entity relationship model entities, attributes, and
relations. It also displays connectivity and cardinality.
44 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
Exercise
a) is a set of rules that prevents you form changing or deleting a record if matching records exist in a
related table
b) is a diagram that depicts an entity relationship model entities, attributes, and relations
c) is used to make sure that all data entries are unique
d) is a character of an entity or object.
e) is a collection of entities, which share the same characteristic
a) Primary
b) Composite
c) Derived
d) Multivalued
e) Simple
a) Candidate key
b) Primary key
c) Foreign key
d) Secondary key
e) Composite key
4. Foreign Key is
5. “A college has many courses”. Which cardinality constrains below describe the statement?
45 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
6. Based on the table below, determine the entities that may be appearing in the ER diagram
I. House No.,
II. Owner Name
III. Owner No.
IV. Renovation
a) I, II
b) I,II,III
c) I, III
d) I, IV
e) II, III, IV
7. Fill in the box with types of attributes and keys in the ER diagram below.
46 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g
10. ______________________ is an interaction of 2 entities and represented by a verb.
12. Draw a complete ER model and cardinality constraint for the sentences below. Add the possible attribute
for each entity.
“Many staff work at one department only and one department may consist of many facilities.”
47 | P a g e C h a p t e r 3 : E n t i t y r e l a t i o n s h i p m o d e l i n g