Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

The Entity Relationship Model

„ How do we design a database?


„ How is the ER model used in database design?
„ What are the main concepts of the ER model?
„ How to use the ER model effectively?

Intro. to Dababase Management Systems 2.1 Lecture notes by Nikos Mamoulis


Steps in Database Design

„ Given a collection or interrelated data to be stored in the DBMS, the


first role of the DBA is to design the logical schema of the database
„ Requirements Analysis
¾ Interact with users to find out what data will be stored in the DB, any
constraints for these data, what operations will access these data, etc.
„ Conceptual Design of the Database
¾ High-level description of the data, their correlations, their constraints,
etc.
¾ The Entity-Relationship Model is a semantic data model used for this
purpose
„ Logical Design of the Database
¾ Conversion of the ER diagram to a relational database schema (next
week)

Intro. to Dababase Management Systems 2.2 Lecture notes by Nikos Mamoulis


About the Logical DB Design

„ It is only an initial step in overall DB design


„ Steps that follow:
¾ Schema refinement (Chapter 7)
¾ Application design (Chapter 8)

„ In the remainder of this lecture we will discuss in detail the Entity


Relationship Model for Conceptual Database Design

Intro. to Dababase Management Systems 2.3 Lecture notes by Nikos Mamoulis


Entities and Entity Sets
„ Entity: An object of the real world distinguishable from other objects
¾ Examples: (from an enterprise database)
ƒ The LCD TV with S/N 290300200283
ƒ The Electronics department
ƒ The manager of the Electronics department
„ Entity set: A collection of similar entities
¾ Examples:
ƒ The Employees that work in any department
ƒ The Departments of the enterprise
„ Attributes: Used to describe an entity. Entities in the same entity set have the
same attributes
¾ Example:
ƒ Each Employee has a name, a SSN, and a parking lot.
„ Domain: The set of possible values that an attribute can take
¾ Example:
ƒ Employee.name attribute takes 20-character strings as values
„ Key: A set of attributes that uniquely identify an entity in the entity set
¾ Example:
ƒ Employee.ssn attribute distinguishes an employee from another
„ Primary key: One designated candidate key of an entity

Intro. to Dababase Management Systems 2.4 Lecture notes by Nikos Mamoulis


Entities and Entity Sets

„ Notation for representing entity sets

ssn name lot

Employees

„ Entity sets in rectangles


„ Attributes in ovals
„ Primary key is underlined
„ Attribute domains NOT specified in ER notation (but specified in
the next step of design)

Intro. to Dababase Management Systems 2.5 Lecture notes by Nikos Mamoulis


Relationships and Relationship Sets

„ Relationship: An association between two or more entities


¾ Example:
ƒ Employee John Smith works in the Electronics department
„ Relationship set: A collection of similar relationships
¾ Example:
ƒ The set of works-in relationships that employees have with the
departments they work in.
„ A relationship may have a descriptive attribute.
¾ Example:
ƒ Employee John Smith works in the Electronics department
since 1/1/01

Intro. to Dababase Management Systems 2.6 Lecture notes by Nikos Mamoulis


Relationships and Relationship Sets

„ Notation for representing relationship sets (with their associated


entity sets)

ssn name lot since did dname budget

Employees Works_in Departments

„ Relationship sets in diamonds


„ Connectors to associated entities
„ Attributes may characterize relationships

Intro. to Dababase Management Systems 2.7 Lecture notes by Nikos Mamoulis


Instance of a Relationship Set

„ An instance of Works_in relationship set

1/1/01
13-324
1/4/02 34
13-322
2/2/05 12
12-824
1/1/03 76
21-397
1/1/05

Employees (ssn) Works_In Departments (did)

Intro. to Dababase Management Systems 2.8 Lecture notes by Nikos Mamoulis


More on Relationship Sets

„ Example of a ternary relationship-set

ssn name lot since did dname budget

Employees Works_in2 Departments

address Locations capacity

Intro. to Dababase Management Systems 2.9 Lecture notes by Nikos Mamoulis


More on Relationship Sets

„ Example of a relationship-set associating entities in the same


entity set

ssn name lot

Employees

supervisor sub-ordinate

Reports_to

roles

Intro. to Dababase Management Systems 2.10 Lecture notes by Nikos Mamoulis


Key Constraints
„ In practice, the participation of entities in the relationships may
be imposed by constraints (as specified by the application)
„ Key constraint: restriction of an entity to participate in at most
one relationship
¾ Example: A department can have at most one manager
ssn name lot since did dname budget

Employees Manages Departments

11-324 1/1/01
34
13-122 1/4/02
12
12-824
76
21-397

Employees (ssn) Manages Departments (did)


Intro. to Dababase Management Systems 2.11 Lecture notes by Nikos Mamoulis
Key Constraints
„ Key constraints impose three different types of relationships
between two entities

many-to-many one-to-many one-to-one

• an employee can work in • an employee can manage • an employee can manage


many departments many departments at most one department
• a department can have • a department can be managed • a department can be managed
many employees by at most one employee by at most one employee

Intro. to Dababase Management Systems 2.12 Lecture notes by Nikos Mamoulis


Participation Constraints
„ Total participation constraint: each member of an entity set
restriction must participate in a relationship Thick line
¾ Example: Each department must have a manager denotes
total
„ Otherwise participation is partial participation

ssn name lot since did dname budget

Employees Manages Departments

Arrow
denotes
11-324 1/1/01
key
34
13-122 constraint
1/4/02
12
12-824
2/2/05
76
21-397

Employees (ssn) Manages Departments (did)


Intro. to Dababase Management Systems 2.13 Lecture notes by Nikos Mamoulis
Example: what are the constraints here?

„ Exercise: Explain the key and participation constraints you see in


the following ER diagram

ssn name lot since did dname budget

Employees Manages Departments

Works_in

since

Intro. to Dababase Management Systems 2.14 Lecture notes by Nikos Mamoulis


Weak Entities
„ In practice, the existence of one entity may depend on the existence of
another entity
¾ Example: Employees purchase insurance policies that cover their
dependents
¾ If the employee is deleted from the database we want to delete the
dependents information, as well
¾ Dependents are identified not only by their own attributes, but also by the
attributes of the employee. Dependents is an example of a weak entity set.
There is a partial key (pname), used in conjunction with the primary key of
the owner entity to form a unique identifier (ssn, pname) for a weak entity.
ssn name lot cost pname age

Employees Policy Dependents

¾ Relationship between owner entity and weak entity must be one-to-many


¾ The weak entity set must have a total participation in the identifying
relationship
Intro. to Dababase Management Systems 2.15 Lecture notes by Nikos Mamoulis
Class Hierarchies
„ Similar to OO modeling, there might be
hierarchies between entity sets name lot
ssn
„ Attributes of a general entity set are
inherited by the more special entity set
„ A query on a general class must consider Employees
the entities in the special classes as well
„ Special entities may have special
attributes or may participate to special
relationships (e.g., only contract ISA
employees can be managers)
„ Overlap constraints: can an entity
participate in more than one
specialization? Hourly_Emps Contract_Emps
¾ An emp. can be both contract and hourly
emp. hours_worked contractid
„ Covering constraints: is the set of entities
in the special classes equal to the set of
entities in the general class? hour_wages

¾ The set of employees contains only


contract and hourly emps.

Intro. to Dababase Management Systems 2.16 Lecture notes by Nikos Mamoulis


Aggregation
„ Sometimes, we may want to define a relationship between
an entity and another relationship (aggregation).
„ Example:
¾ Projects Sponsored by Departments
¾ Sponsorships are Monitored by Employees
ssn name lot

Employees Monitors until

pid started_on pbudget since did dname budget

Projects Sponsors Departments

Intro. to Dababase Management Systems 2.17 Lecture notes by Nikos Mamoulis


Issues when Designing an ER Diagram

„ Should a concept be modeled as an attribute or by an


entity set?
„ Should a concept be modeled as an entity set or as a
relationship set?
„ What relationship sets should we choose? What
attributes should we choose for them? Should we use
binary or ternary relationships?
„ Should we use aggregations?

Intro. to Dababase Management Systems 2.18 Lecture notes by Nikos Mamoulis


Attribute or Entity?

„ Example 1:
¾ Should address be an attribute of an employee or another
entity, such that employees are related to addresses via a
relationship?
ssn name lot
ssn name lot

Employees
Employees address
Has_addr street
Option A

¾ When to use option B: Address number

ƒ Complex structure of address


Option B zip
ƒ Employees may have many addresses
ƒ Few addresses shared by many employees

Intro. to Dababase Management Systems 2.19 Lecture notes by Nikos Mamoulis


Attribute or Entity?

„ Example 2:
¾ Should employment duration be a set of attributes in works_in
relationship, or a different entity?

ssn name lot did dname budget


from to

Option A Employees Works_in Departments

ssn name lot did dname budget

Option B
Employees Works_in Departments

from Duration to

¾ Use option B, if you want to allow employees work at the same


department during different periods
Intro. to Dababase Management Systems 2.20 Lecture notes by Nikos Mamoulis
Entity or Relationship?

„ Example 3:
¾ Manager has a discretionary budget for each department he
manages or a total discretionary budget for all departments

ssn name lot did dname budget


since dbudget

Option A Employees Manages Departments

ssn name lot did dname budget


since

Option B Employees Manages Departments

ISA

Managers dbudget

¾ Use option B, if there is a total budget for all depts, otherwise there is
redundancy in option A (budget is repeated for every instance of the same
manager in the relationship set)
Intro. to Dababase Management Systems 2.21 Lecture notes by Nikos Mamoulis
Binary or Ternary Relationship?
ssn name lot pname age

Option A Employees Covers Dependants

policyid Policies cost

What are the differences between the two diagrams?

ssn name lot pname age

Employees Dependants
Purchaser beneficiary B is accurate if
dependants rely
Option B
on policies and
policyid Policies cost a policy is held
by one employee
Intro. to Dababase Management Systems 2.22 Lecture notes by Nikos Mamoulis
More examples in your textbook

„ Study them carefully to deeply understand the


differences between the design concepts.
„ Practice with your textbook exercises for the design of
ER diagrams for a given database specification.
„ In the tutorial we will solve such an exercise and you’ll
find one in your first assignment.

Intro. to Dababase Management Systems 2.23 Lecture notes by Nikos Mamoulis


A case study (to be solved here if time permits)

„ You are asked to design a database for a university that keeps


track of student records. For each student you need to store the
student's name, university number, HKID, address, phone, major
department. A student has exactly one major department. Either
the university number or the HKID can uniquely identify a
student. For each department in the university, you should store
a name, code, and office number. The department code has
unique value for a given department. Each course has a name,
code, number of credits, a set of zero or more prerequisite
courses, and exactly one offering department. The course code
is unique for each course. Each course offering has an instructor,
semester, year, course, and section code. The section code
distinguishes offerings of the same course that are taught during
the same semester and year. A grade report has a student,
course offering, and a grade `A', `B', `C', `D', or `F'. There may
not be a grade report for a student s and a course c if s has not
passed (grade report <>`F') all prerequisites of c.

Intro. to Dababase Management Systems 2.24 Lecture notes by Nikos Mamoulis


A case study (to be solved here if time permits)

address
HKID phone dcode
dname officeno

unumber student major department

sname

report grade offdept


credits
instructor
ccode
offering offered course
semester
cname
year section required_by
requires
prereq

Intro. to Dababase Management Systems 2.25 Lecture notes by Nikos Mamoulis


Summary

„ The Entity-Relationship model is used for conceptual database


design.
¾ A high-level description for the database is yielded.
„ Given a database specification, we identify the entities,
relationships, and their attributes that should be stored in a
database.
„ More advanced constructs include weak entities, ISA hierarchies,
and aggregation.
„ We also identify several constraints that can be expressed in the
model (key, participation, overlap/covering constraints in ISA
hierarchies).
„ There could be many alternative diagrams to represent the same
scenario. Make sure you design one that captures all concepts
and constraints.

Intro. to Dababase Management Systems 2.26 Lecture notes by Nikos Mamoulis

You might also like