Lecture 2 Design Database ER
Lecture 2 Design Database ER
Acknowledgement
Today’s topics
• Design ERD
o Notation
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Reading material
[GUW] Chapter 3
Open-source tools
There are several open-source tools available for drawing Entity-
Relationship Diagrams (ERDs):
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Website: Draw.io
• Description: Draw.io is a free and open-source online diagramming
tool. It supports various diagram types, including ERDs. It has a
user-friendly interface and allows you to save diagrams locally or
on cloud storage services.
• Website: Lucidchart
• Description: Lucidchart is a web-based diagramming tool that
offers a free version with limited features. It supports ERDs and
provides collaboration features for team projects.
Open-source tools
• Website: dbdiagram.io
• Description: dbdiagram.io is an online tool specifically
designed for creating database diagrams, including ERDs. It
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Open-source tools
• Website: Dia
• Description: Dia is an open-source general-purpose
diagramming software for Linux, Windows, and macOS. It
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Keys
A key is a set of attributes K for a relation R if
• In no instance of R will two different tuples agree on
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
all attributes of K
– That is, K can serve as a “tuple identifier”
• No proper subset of K satisfies the above condition.
– That is, K is minimal
• Example: Students (Sid, name, age, pop)
– Sid is a key
– Age is not a key (not an identifier)
– {sid, name} is not a key (not minimal)
Use of keys
• More constraints on data, fewer mistakes
• Look up a row by its key value
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
E/R basics
• Entity: a “thing” like an object
• Entity set: a collection of things of the same type,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Users
Groups
Each has uid (unique id),
name, age, pop (popularity) Each has gid (unique id),
name
Member
Records fromDate
(when a user joined a group)
Duke CS, Winter 2023 13
many-to-many many-to-one
R R
relationship relationship
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
E1
role-
name ISA: generalization
R E role indicator
or specialization
E2 E3
E1 E1
total (disjoint) disjoint
total generalization generalization
E2 E3 E2 E3
Duke CS, Winter 2023 19
many-to-many * * R
E1 R E2 E1 E2
relationship
one-to-one 1 1 R
E1 R E2 E1 E2
relationship
Duke CS, Winter 2023 20
many-to-one 1 R
E1 * R E2 E1 E2
relationship
participation R
in R: total (E1) E1 R E2 E1 E2
and partial (E2)
many-to-many * * R
E1 R E2 E1 E2
relationship
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
one-to-one 1 1 R
E1 R E2 E1 E2
relationship
many-to-one 1 R
E1 * R E2 E1 E2
relationship
participation R
in R: total (E1) E1 R E2 E1 E2
and partial (E2)
Mapping Cardinalities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Mapping Cardinalities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Entity Sets
E/R Diagrams
• In an entity-relationship diagram:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example:
name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Beers
Relationships
Example: Relationships
name addr Bars sell some name manf
beers.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
license
Drinkers
name addr
Relationship Set
Bar Beer
Beers
Joe’s Bar Bud
Joe’s Bar Miller
lls Sue’s Bar Bud
Se
Sue’s Bar Pete’s Ale
Bars Sue’s Bar Bud Lite
Multiway Relationships
• Sometimes, we need a relationship that connects
more than two entity sets.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Preferences
Drinkers
name addr
Many-Many Relationships
• Focus: binary relationships, such as Sells between
Bars and Beers.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
many-many
Many-One Relationships
In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
many-one
One-One Relationships
• In a one-one relationship, each entity of
either entity set is related to at most one
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
one-one
Representing “Multiplicity”
Favorite
Attributes on Relationships
• Sometimes it is useful to attach an attribute
to a relationship.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
price
of the attribute.
• Make that entity set participate in the
relationship.
Prices
price
Roles
• Sometimes an entity set appears more than
once in a relationship.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example: Roles
Relationship Set
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Married
Husband wife
husband Bob Ann
wife
Joe Sue
Drinkers … …
Example: Roles
Relationship Set
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Buddies
Buddy1 Buddy2
1 Bob Ann
2
Joe Sue
Drinkers Ann Bob
Joe Moe
… …
Subclasses
• Subclass = special case = fewer entities = more
properties.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example: Subclasses
isa
color Ales
Specialization/Generalization
Only a subset of entities within a type have
certain attributes or participate in certain
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
relationships
ID
STUDENT
GRAD_STUDENT Degrees
A person can be an
SSN
employee, an
alumnus, and/or a
PERSON
student
o
U
U
U
EMPLOYEE ALUMNUS STUDENT
SSN
A person can be
either an employee,
PERSON
an alumnus, or a
student
d
U
U
U
U
U
EMPLOYEE ALUMNUS STUDENT
name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Beers
Pete’s Ale
isa
color Ales
isa
color Ales
Courses
In E/R Diagrams
sets.
– Not every many-one relationship from a weak
entity set need be supporting.
– But supporting relationships must have a rounded
arrow (entity at the “one” end is guaranteed).
Design Techniques
1. Avoid redundancy.
2. Limit the use of weak entity sets.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Avoiding Redundancy
• Redundancy = saying the same thing in two
(or more) different ways.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example: Good
Example: Bad
manf
Example: Bad
Beers
Example: Good
Example: Good
name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Beers
Example: Bad
name name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Exercise
Draw an ERD for the following description:
• You are in charge of managing the program committee for an important
conference and journal. The following database stores information about
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
papers submitted to the conference and journal (Papers). Each Paper has a
unique ID (PID), a title, and a type of source. Reviewers on the program
committee (Reviewers: each reviewer has a unique ID (rid), a full name
including first name last name, and middle name, an affiliation, and several
emails). Each reviewer on the program committee will have to check a set of
papers (zero or more). Each paper is assigned by zero or more reviewers,
identify the number of papers that have been checked by the amount
attribute.
Case study 1
• Design a database representing cities, counties,
and states
• For states, record name and capital (city)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
population capital
county_name
county_area
population
In IsCapitalOf
name
Counties States
In name
area
Case study 2
• Design a database consistent with the following:
- A station has a unique name and an address, and is
either an express station or a local station
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
address
engineer address
ISA ISA
LocalTrains
LocalStations
ExpressTrains
ExpressStations
time No double-diamonds here
because train number + time
ExpressTrainStops uniquely determine a stop
Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Exercise
Or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Exercise
Exercise
Or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Revise!
We store each employee’s name
(first, last, MI), Social Security
number (SSN), street address,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Answer
Salary
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
1
Supervisor Birthdate
Supervision
EMPLOYEE
Address
Supervisee
N
FName Name Works_On Project
MI LName Hours
Revise!
Salary
Birthdate
EMPLOYEE
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
We keep each
dependent’s first
Sex
name, sex, birth
date, and relationship Employee
DEPENDENT
DBirthdate
to the employee Relationship
DName
Revise!
Salary
Supervisor
Supervision
Birthdate
EMPLOYEE
Address
1
N
Sex
MI LName Hours
DEPENDENT N
_OF
DEPENDENT DBirthdate
Relationship
DName
Revise!
Number
A department controls Name
DEPARTMENT
Location Manager
Manager_
Start_Date
Answer
Name Number
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
PROJECT
Location
N Name Number
1 DEPARTMENT
CONTROLS Manager
Manager_
Location Start_Date
Revise!
Nam e Num ber
Each department has a
particular employee who Location PROJECT
Name Number
N
manages the department.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
1 DEPARTMENT
An employee is assigned CO NTRO LS M anager
Birthdate
EMPLOYEE
Address
necessarily controlled by N 1
Supervisee Name W orks_On
the same department. We Project
N DEPENDENT
DEPENDENT
number of hours per week _OF
DBirthdate
Relationship
that an employee works DName
on each project.
Duke CS, Winter 2023 106
International University, VNU-HCMC
Answer
Supervisor Supervisee
Hours 1 Supervision N
M
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
EMPLOYEE
WORKS_ON SSN
1 N
Name Start
_Date
N
PROJECT Manages
Location
N
Number
1
1 DEPARTMENT 1
CONTROLS Works_For