0% found this document useful (0 votes)
18 views55 pages

Lecture 2 Design Database ER

PDM - Design Database ER
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)
18 views55 pages

Lecture 2 Design Database ER

PDM - Design Database ER
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/ 55

International University, VNU-HCMC

School of Computer Science and Engineering

Lecture 2: Relational Database


Design Entity-Relationship

Instructor: Nguyen Thi Thuy Loan


[email protected], [email protected]
https://nttloan.wordpress.com/

International University, VNU-HCMC

Acknowledgement

The following slides are referenced from


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Northeastern University and Duke University.

Duke CS, Winter 2023 2


International University, VNU-HCMC

Today’s topics
• Design ERD
o Notation
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

o Entity/ Weak entity


o Attributes
o Relationship

• Reading material
[GUW] Chapter 3

Duke CS, Winter 2023 3

International University, VNU-HCMC

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.

Duke CS, Winter 2023 4


International University, VNU-HCMC

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

allows you to define your database structure using a simple


textual syntax.

• Website: MySQL Workbench


• Description: MySQL Workbench is an integrated
development environment for MySQL databases. It includes
a visual database design tool that enables you to create
ERDs. It's particularly useful if you're working with MySQL
databases.

Duke CS, Winter 2023 5

International University, VNU-HCMC

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

includes support for ERDs and provides a wide range of


shapes and symbols.

• Website: Pencil Project


• Description: Pencil is an open-source GUI prototyping tool
that also supports various types of diagrams, including ERDs.
It's available as a standalone application or a Firefox add-on.

Duke CS, Winter 2023 6


International University, VNU-HCMC

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)

Duke CS, Winter 2023 7

International University, VNU-HCMC

Schema vs. instance


Drinker
name address
Amy 100 W. Main Street
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Ben 101 W. Main Street


Dan 300 N. Duke Street

• Is name a key of Drinker?


– Yes? Seems reasonable for this instance
– No? Drinkers names are not unique in general
• Key declarations are part of the schema

Duke CS, Winter 2023 8


International University, VNU-HCMC

More examples of keys


• Enrolled (Sid, Cid)
– {Sid, Cid}: a key
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– A key can contain multiple attributes


• Address(street_address, city, state, zip)
– {street_address, city, state}
– {street_address, zip}
– A relation can have multiple keys!
– We typically pick one as the “primary” key, and underline
all its attributes, e.g., Address(street_address, city, state,
zip)

Duke CS, Winter 2023 9

International University, VNU-HCMC

Use of keys
• More constraints on data, fewer mistakes
• Look up a row by its key value
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Many selection conditions are “key = value”


“Pointers” to other rows (often across tables)
• Example: Enrolled(Sid, Cid)
– Sid: is a key of Students
– Cid: is a key of Courses
– An Enrolled row “links” a Students row with a Courses
row.
• Many join conditions are “key = key value stored in
another table”
Duke CS, Winter 2023 10
International University, VNU-HCMC

Entity-relationship (E/R) model


• Historically and still very popular
• Concepts applicable to other design models as well
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Can think of as a “watered-down” object-oriented


design model
• Primarily a design model-not directed implemented
by DBMS
• Designs represented by E/R diagarms
– We use the style of E/R diagram covered by the GMUW
book; there are other styles/extensions
– Very similar to UML diagrams

Duke CS, Winter 2023 11

International University, VNU-HCMC

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

like a relation of tuples or a class objects


– Represented as a rectangle
• Relationship: an association among entities
• Relationship set: a set of relationships of the same
type (among same entity sets)
– Represented as a diamond
• Attributes: properties of entities or relationships,
like attributes of tupes or objects.
– Represented as ovals
Duke CS, Winter 2023 12
International University, VNU-HCMC 5

Example: Users, Groups, Members


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

International University, VNU-HCMC

The famous “Beers” database


Drinkers Frequent Bars Bars
“X” times a week Each has an address
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Bars Serve Beers


At price “Y”

Drinkers Drinkers Likes Beers


Each has an address Beers
Each has a brewer
(Later in ER diagram – how to
design a relational database)
Duke CS, Winter 2023 14
International University, VNU-HCMC

Purpose of E/R Model

• The E/R model allows us to sketch


database schema designs.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Includes some constraints, but not


operations.
• Designs are pictures called entity-
relationship diagrams.
• Later: convert E/R designs to relational DB
designs.

Duke CS, Winter 2023 15

International University, VNU-HCMC

Framework for E/R


• Design is a serious business.
• The “boss” knows they want a database, but
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

they don’t know what they want in it.


• Sketching the key components is an efficient
way to develop a working database.

Duke CS, Winter 2023 16


International University, VNU-HCMC

Summary of the notation for ER diagrams


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 17

International University, VNU-HCMC

Summary of the notation for ER diagrams


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 18


E entity set
A1
a!ributes:
A2
simple (A1),
A2.1 composite (A2) and
R relationship set A2.2 multivalued (A3)
derived (A4)
{A3}
A4()
identifying
R relationship set E
for weak entity set primary key
A1
International University, VNU-HCMC
total participation E discriminating
R E of entity set in a!ribute of
Summary of the notation for
A1 ER diagrams
relationship weak entity set

many-to-many many-to-one
R R
relationship relationship
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

R one-to-one l..h cardinality


R E
relationship limits

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

International University, VNU-HCMC

Summary of the notation for ER diagrams

entity set E with A2.1 A2.2


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

simple a!ribute A1,


composite a!ribute A2, A2
multivalued a!ribute A3, A1 A3
derived a!ribute A4,
E A4
and primary key A1

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)

ISA total ISA


weak entity set
entity set E with A2.1 A2.2
simple a!ribute
International A1, VNU-HCMC
University,
composite a!ribute A2, A2
A3
Alternative ER Notations
multivalued a!ribute A3,
derived a!ribute A4,
A1
A4
and primary key A1 E

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)

ISA total ISA


weak entity set generalization generalization
Duke CS, Winter 2023 21

International University, VNU-HCMC

ERD design steps


• Step 1: Entity Identification (attributes or entities)
• Step 2: Relationship Identification
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Step 3: Identifying and mapping attributes to


entities, relationships.
• Step 4: Determine the value range for each
attribute.
• Step 5: Determine key attribute for each entity.
• Step 6: Cardinality identification.
• Step 7: Hierarchical design (generalized/specialized)
constraints.

Duke CS, Winter 2023 22


International University, VNU-HCMC

Mapping Cardinality Constraints


• Express the number of entities to which another
entity can be associated via a relationship set.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Most useful in describing binary relationship sets.


• For a binary relationship set the mapping cardinality
must be one of the following types:
– One to one
– One to many
– Many to one
– Many to many

Duke CS, Winter 2023 23

International University, VNU-HCMC

Mapping Cardinalities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

One to one One to many

Note: Some elements in A and B may not be mapped


to any elements in the other set

Duke CS, Winter 2023 24


International University, VNU-HCMC

Mapping Cardinalities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Many to one Many to many


Note: Some elements in A and B may not be mapped
to any elements in the other set

Duke CS, Winter 2023 25

International University, VNU-HCMC

Entity Sets

• Entity = “thing” or object.


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Entity set = collection of similar entities.


– Similar to a class in object-oriented languages.
• Attribute = property of (the entities of) an
entity set.
– Attributes are simple values, e.g. integers or
character strings, not structs, sets, etc.

Duke CS, Winter 2023 26


International University, VNU-HCMC

E/R Diagrams

• In an entity-relationship diagram:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Entity set = rectangle.


– Attribute = oval, with a line to the rectangle
representing its entity set.

Duke CS, Winter 2023 27

International University, VNU-HCMC

Example:
name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers

• Entity set Beers has two attributes, name and manf


(manufacturer).
• Each Beers entity has values for these two
attributes, e.g. (Bud, Anheuser-Busch)

Duke CS, Winter 2023 28


International University, VNU-HCMC

Relationships

• A relationship connects two or more entity


sets.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• It is represented by a diamond, with lines to


each of the entity sets involved.

Duke CS, Winter 2023 29

International University, VNU-HCMC

Example: Relationships
name addr Bars sell some name manf
beers.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Bars Sells Beers

license

Drinkers frequent Frequents Likes Drinkers like


some bars. some beers.

Drinkers
name addr

Note: license = beer, full, none


Duke CS, Winter 2023 30
International University, VNU-HCMC

Relationship Set

• The current “value” of an entity set is the set of


entities that belong to it.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Example: the set of all bars in our database.


• The “value” of a relationship is a relationship set,
a set of tuples with one component for each
related entity set.

Duke CS, Winter 2023 31

International University, VNU-HCMC

Example: Relationship Set

• For the relationship Sells, we might have a


relationship set like:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

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

Duke CS, Winter 2023 32


International University, VNU-HCMC

Multiway Relationships
• Sometimes, we need a relationship that connects
more than two entity sets.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Suppose that drinkers will only drink certain beers


at certain bars.
– Our three binary relationships Likes, Sells, and
Frequents do not allow us to make this
distinction.
– But a 3-way relationship would.

Duke CS, Winter 2023 33

International University, VNU-HCMC

Example: 3-Way Relationship

name addr name manf


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

license Bars Beers

Preferences

Drinkers

name addr

Duke CS, Winter 2023 34


International University, VNU-HCMC

Ex. Ternary relationship type


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 35

International University, VNU-HCMC

A Typical Relationship Set


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Bar Drinker Beer


Joe’s Bar Ann Miller
Sue’s Bar Ann Bud
Sue’s Bar Ann Pete’s Ale
Joe’s Bar Bob Bud
Joe’s Bar Bob Miller
Joe’s Bar Cal Miller
Sue’s Bar Cal Bud Lite

Duke CS, Winter 2023 36


International University, VNU-HCMC

Many-Many Relationships
• Focus: binary relationships, such as Sells between
Bars and Beers.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• In a many-many relationship, an entity of either set


can be connected to many entities of the other set.
– E.g., a bar sells many beers; a beer is sold by many
bars.

Duke CS, Winter 2023 37

International University, VNU-HCMC

In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

many-many

Duke CS, Winter 2023 38


International University, VNU-HCMC

Many-One Relationships

• Some binary relationships are many-one from


one entity set to another.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Each entity of the first set is connected to at


most one entity of the second set.
• But an entity of the second set can be
connected to zero, one, or many entities of
the first set.

Duke CS, Winter 2023 39

International University, VNU-HCMC

In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

many-one

Duke CS, Winter 2023 40


International University, VNU-HCMC

Example: Many-One Relationship


• Favorite, from Drinkers to Beers is many-one.
• A drinker has at most one favorite beer.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• But a beer can be the favorite of any number


of drinkers, including zero.

Duke CS, Winter 2023 41

International University, VNU-HCMC

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

entity of the other set.


• Example: Relationship Best-seller between
entity sets Manfs (manufacturer) and Beers.
– A beer cannot be made by more than one
manufacturer, and no manufacturer can have
more than one best-seller (assume no ties).

Duke CS, Winter 2023 42


International University, VNU-HCMC

In Pictures:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

one-one

Duke CS, Winter 2023 43

International University, VNU-HCMC

Representing “Multiplicity”

• Show a many-one relationship by an


arrow entering the “one” side.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Remember: Like a functional dependency.


• Show a one-one relationship by arrows
entering both entity sets.
• Rounded arrow = “exactly one,” i.e., each
entity of the first set is related to exactly
one entity of the target set.

Duke CS, Winter 2023 44


International University, VNU-HCMC

Example: Many-One Relationship

Drinkers Likes Beers


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Favorite

Notice: two relationships connect the same entity


sets, but are different.

Duke CS, Winter 2023 45

International University, VNU-HCMC

Example: One-One Relationship


• Consider Best-seller between Manfs and
Beers.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Some beers are not the best-seller of any


manufacturer, so a rounded arrow to Manfs
would be inappropriate.
• But a beer manufacturer has to have a best-
seller.

Duke CS, Winter 2023 46


International University, VNU-HCMC

In the E/R Diagram


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Manfs Best- Beers


seller

A beer is the best- A manufacturer


seller for 0 or 1 has exactly one
manufacturer. best seller.

Duke CS, Winter 2023 47

International University, VNU-HCMC

Attributes on Relationships
• Sometimes it is useful to attach an attribute
to a relationship.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Think of this attribute as a property of tuples


in the relationship set.

Duke CS, Winter 2023 48


International University, VNU-HCMC

Example: Attribute on Relationship


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Bars Sells Beers

price

Price is a function of both the bar and the beer,


not of one alone.

Duke CS, Winter 2023 49

International University, VNU-HCMC


Equivalent Diagrams Without Attributes on
Relationships

• Create an entity set representing values


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

of the attribute.
• Make that entity set participate in the
relationship.

Duke CS, Winter 2023 50


International University, VNU-HCMC

Ex. Removing an Attribute from a Relationship

Bars Sells Beers


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Prices

price

Note convention: arrow from multiway relationship = “all other


entity sets together determine a unique one of these.”

Duke CS, Winter 2023 51

International University, VNU-HCMC

Roles
• Sometimes an entity set appears more than
once in a relationship.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Label the edges between the relationship and


the entity set with names called roles.

Duke CS, Winter 2023 52


International University, VNU-HCMC

Example: Roles

Relationship Set
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Married
Husband wife
husband Bob Ann
wife
Joe Sue
Drinkers … …

Duke CS, Winter 2023 53

International University, VNU-HCMC

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
… …

Duke CS, Winter 2023 54


International University, VNU-HCMC

Subclasses
• Subclass = special case = fewer entities = more
properties.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Example: Ales are a kind of beer.


– Not every beer is an ale, but some are.
– Let us suppose that in addition to all the
properties (attributes and relationships) of beers,
ales also have the attribute color.

Duke CS, Winter 2023 55

International University, VNU-HCMC

Subclasses in E/R Diagrams


• Assume subclasses form a tree.
– I.e., no multiple inheritance.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Isa triangles indicate the subclass


relationship.
– Point to the superclass.

Duke CS, Winter 2023 56


International University, VNU-HCMC

Example: Subclasses

name Beers manf


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

isa

color Ales

Duke CS, Winter 2023 57

International University, VNU-HCMC

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

Duke CS, Winter 2023 58


International University, VNU-HCMC

Multiple Subtypes: Disjointedness


(o)verlap: may be more than one
(d)isjoint: entities may only be one subtype
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

A person can be an
SSN
employee, an
alumnus, and/or a
PERSON
student
o

U
U

U
EMPLOYEE ALUMNUS STUDENT

Duke CS, Winter 2023 59

International University, VNU-HCMC

Multiple Subtypes: Disjointedness


(o)verlap: may be more than one
(d)isjoint: entities may only be one subtype
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SSN
A person can be
either an employee,
PERSON
an alumnus, or a
student
d

U
U

EMPLOYEE ALUMNUS STUDENT

Duke CS, Winter 2023 60


International University, VNU-HCMC

Multiple Subtypes: Completeness


Similar to relationships; can be total (must
belong to subtypes) or partial (can belong)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SSN A person must be


exactly one: an
employee, an
PERSON
alumnus, or a student
d

U
U

U
EMPLOYEE ALUMNUS STUDENT

Duke CS, Winter 2023 61

International University, VNU-HCMC

E/R Vs. Object-Oriented Subclasses


• In OO, objects are in one class only.
– Subclasses inherit from superclasses.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• In contrast, E/R entities have


representatives in all subclasses to which
they belong.
– Rule: if entity e is represented in a subclass,
then e is represented in the superclass (and
recursively up the tree).

Duke CS, Winter 2023 62


International University, VNU-HCMC

Example: Representatives of Entities

name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers

Pete’s Ale
isa

color Ales

Duke CS, Winter 2023 63

International University, VNU-HCMC

Keys in E/R Diagrams


• Underline the key attribute(s).
• In an isa hierarchy, only the root entity set has
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

a key, and it must serve as the key for all


entities in the hierarchy.

Duke CS, Winter 2023 64


International University, VNU-HCMC

Example: name is Key for Beers

name Beers manf


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

isa

color Ales

Duke CS, Winter 2023 65

International University, VNU-HCMC

Example: a Multi-attribute Key


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

dept number hours room

Courses

• Note that hours and room could also serve as a


key, but we must select only one key.

Duke CS, Winter 2023 66


International University, VNU-HCMC

Weak Entity Sets


• Occasionally, entities of an entity set need
“help” to identify them uniquely.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Entity set E is said to be weak if in order to


identify entities of E uniquely, we need to
follow one or more many-one relationships
from E and include the key of the related
entities from the connected entity sets.

Duke CS, Winter 2023 67

International University, VNU-HCMC

Example: Weak Entity Set


• name is almost a key for football players, but
there might be two with the same name.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• number is certainly not a key, since players on


two teams could have the same number.
• But number, together with the team name
related to the player by Plays-on should be
unique.

Duke CS, Winter 2023 68


International University, VNU-HCMC

In E/R Diagrams

name number name


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Players Plays- Teams


on

Note: must be rounded because


each player needs a team to help
with the key.

• Double diamond for supporting many-one relationship.


• Double rectangle for the weak entity set.

Duke CS, Winter 2023 69

International University, VNU-HCMC

Weak Entity-Set Rules


• A weak entity set has one or more many-one
relationships to other (supporting) entity
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

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

Duke CS, Winter 2023 70


International University, VNU-HCMC

Weak Entity-Set Rules – (Cont.)


• The key for a weak entity set is its own
underlined attributes and the keys for the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

supporting entity sets.


– E.g., (player) number and (team) name is a key for
Players in the previous example.

Duke CS, Winter 2023 71

International University, VNU-HCMC

Design Techniques
1. Avoid redundancy.
2. Limit the use of weak entity sets.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

3. Don’t use an entity set when an attribute


will do.

Duke CS, Winter 2023 72


International University, VNU-HCMC

Avoiding Redundancy
• Redundancy = saying the same thing in two
(or more) different ways.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Wastes space and (more importantly)


encourages inconsistency.
– Two representations of the same fact become
inconsistent if we change one and forget to
change the other.
– Recall anomalies due to FD’s.

Duke CS, Winter 2023 73

International University, VNU-HCMC

Example: Good

name name addr


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers ManfBy Manfs

This design gives the address of each manufacturer


exactly once.

Duke CS, Winter 2023 74


International University, VNU-HCMC

Example: Bad

name name addr


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers ManfBy Manfs

manf

This design states the manufacturer of a beer twice:


as an attribute and as a related entity.

Duke CS, Winter 2023 75

International University, VNU-HCMC

Example: Bad

name manf manfAddr


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers

This design repeats the manufacturer’s address once


for each beer and loses the address if there are
temporarily no beers for a manufacturer.

Duke CS, Winter 2023 76


International University, VNU-HCMC

Entity Sets Versus Attributes


• An entity set should satisfy at least one of
the following conditions:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– It is more than the name of something; it has


at least one non-key attribute.
or
– It is the “many” in a many-one or many-many
relationship.

Duke CS, Winter 2023 77

International University, VNU-HCMC

Example: Good

name name addr


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers ManfBy Manfs

• Manfs deserves to be an entity set because of the


non-key attribute addr.
• Beers deserves to be an entity set because it is
the “many” of the many-one relationship ManfBy.

Duke CS, Winter 2023 78


International University, VNU-HCMC

Example: Good

name manf
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers

There is no need to make the manufacturer an entity


set, because we record nothing about manufacturers
besides their name.

Duke CS, Winter 2023 79

International University, VNU-HCMC

Example: Bad

name name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Beers ManfBy Manfs

Since the manufacturer is nothing but a name, and is


not at the “many” end of any relationship, it should
not be an entity set.

Duke CS, Winter 2023 80


International University, VNU-HCMC

Don’t Overuse Weak Entity Sets


• Beginning database designers often doubt that
anything could be a key by itself.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– They make all entity sets weak, supported by all


other entity sets to which they are linked.
• In reality, we usually create unique ID’s for entity
sets.
– Examples include social-security numbers,
automobile license’s etc.

Duke CS, Winter 2023 81

International University, VNU-HCMC

When do we need weak entity sets?


• The usual reason is that there is no global authority
capable of creating unique ID’s.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Example: it is unlikely that there could be an


agreement to assign unique player numbers across
all football teams in the world.

Duke CS, Winter 2023 82


International University, VNU-HCMC

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.

Duke CS, Winter 2023 83

International University, VNU-HCMC 31

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

• For counties, record name, area, and location (state)


• For cities, record name, population, and location
(county and state)
• Assume the following:
• Names of states are unique
• Names of counties are only unique within a state
• Names of cities are only unique within a county
• A city is always located in a single county
• A county is always located in a single state

Duke CS, Winter 2023 84


International University, VNU-HCMC 34

Case study 1: first design


name name
Cities In States
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

population capital

county_name

county_area

• County area information is repeated for every city


in the county
Redundancy is bad (why?)
• State capital should really be a city
Should “reference” entities through explicit
relationships
Duke CS, Winter 2023 85

International University, VNU-HCMC 35

Case study 1: second design


name
Cities
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

population

In IsCapitalOf

name
Counties States
In name
area

• Technically, nothing in this design prevents a city in


state 𝑋 from being the capital of another state 𝑌 …

Duke CS, Winter 2023 86


International University, VNU-HCMC

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

- A train has a unique number and an engineer, and is


either an express train or a local train
- A local train can stop at any station
- An express train only stops at express stations
- A train can stop at a station for any number of times
during a day
- Train schedules are the same everyday

Duke CS, Winter 2023 87

International University, VNU-HCMC

Case study 2: first design


number name

engineer Trains StopsAt Stations


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

address

E/L? time E/L?

• Nothing in this design prevents express trains from


stopping at local stations
=> We should capture as many constraints as possible
• A train can stop at a station only once during a day
=> We should not introduce unintended constraints

Duke CS, Winter 2023 88


International University, VNU-HCMC

Case study 2: second design


time
number name
Trains LocalTrainStops Stations
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

engineer address

ISA ISA
LocalTrains
LocalStations

ExpressTrains
ExpressStations
time No double-diamonds here
because train number + time
ExpressTrainStops uniquely determine a stop

Is the extra complexity worth it?

Duke CS, Winter 2023 89

International University, VNU-HCMC

Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Each department has a unique name, a unique


number, and a particular employee who manages
the department. We keep track of the start date
when that employee began managing the
department. A department may have several
locations.

Duke CS, Winter 2023 90


International University, VNU-HCMC

Exercise
Or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

An entity type DEPARTMENT with attributes Name,


Number, Locations, Manager, and
Manager_start_date. Locations is the only
multivalued attribute. We can specify that both
Name and Number are (separate) key attributes
because each was specified to be unique.

Duke CS, Winter 2023 91

International University, VNU-HCMC

Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 92


International University, VNU-HCMC

Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

A department controls a number of projects, each


of which has a unique name, a unique number, and
a single location.
Or
An entity type PROJECT with attributes Name,
Number, Location, and Controlling_department. Both
Name and Number are (separate) key attributes.

Duke CS, Winter 2023 93

International University, VNU-HCMC

Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 94


International University, VNU-HCMC

Exercise

Draw an ERD for the following description:


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

We store each employee’s name (first, last, MI), Social


Security number (SSN), street address, salary, sex
(gender), and birth date. An employee is assigned to
one department, but may work on several projects,
which are not necessarily controlled by the same
department. We keep track of the current number of
hours per week that an employee works on each
project. We also keep track of the direct supervisor of
each employee (who is another employee).

Duke CS, Winter 2023 95

International University, VNU-HCMC

Exercise
Or
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

An entity type EMPLOYEE with attributes Name, Ssn,


Sex, Address, Salary, Birth_date, Department, and
Supervisor. Both Name and Address may be composite
attributes; however, this was not specified in the
requirements. We must go back to the users to see if
any of them will refer to the individual components of
Name First_name, Middle_initial, Last_name or of
Address. In this example Name is modeled as a
composite attribute, whereas Address is not,
presumably after consultation with the users.

Duke CS, Winter 2023 96


International University, VNU-HCMC

Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 97

International University, VNU-HCMC

Exercise
Draw an ERD for the following description:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

We want to keep track of the dependents of each


employee for insurance purposes. We keep each
dependent’s first name, sex, birth date, and
relationship to the employee.
Or
An entity type DEPENDENT with attributes
Employee, Dependent_name, Sex, Birth_date, and
Relationship (to the employee).

Duke CS, Winter 2023 98


International University, VNU-HCMC

Answer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 99

International University, VNU-HCMC

Revise!
We store each employee’s name
(first, last, MI), Social Security
number (SSN), street address,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

salary, sex (gender), and birth date. Salary

An employee is assigned to one SSN Sex

department, but may work on


Department Birthdate
several projects, which are not EMPLOYEE

necessarily controlled by the same Supervisor Address

department. We keep track of the FName Name Works_On Project

current number of hours per week MI LName Hours


that an employee works on each
project. We also keep track of the
direct supervisor of each employee
(who is another employee).

Duke CS, Winter 2023 100


International University, VNU-HCMC

Answer

Salary
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

SSN Sex Department

1
Supervisor Birthdate
Supervision

EMPLOYEE
Address
Supervisee
N
FName Name Works_On Project

MI LName Hours

Duke CS, Winter 2023 101

International University, VNU-HCMC

Revise!
Salary

We want to keep SSN Sex Department


1
track of the Supervisor
Supervision

Birthdate
EMPLOYEE
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

dependents of each Supervisee Address


N
employee for FName Name Works_On Project

insurance purposes. MI LName Hours

We keep each
dependent’s first
Sex
name, sex, birth
date, and relationship Employee
DEPENDENT
DBirthdate
to the employee Relationship

DName

Duke CS, Winter 2023 102


Assoc. Prof. Nguyen Thi Thuy Loan, PhD
International University, VNU-HCMC

Revise!
Salary

SSN Sex Department


1
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Supervisor
Supervision

Birthdate
EMPLOYEE
Address
1
N

Supervisee FName Name Works_On Project

Sex
MI LName Hours

DEPENDENT N
_OF
DEPENDENT DBirthdate

Relationship

DName

Duke CS, Winter 2023 103

International University, VNU-HCMC

Revise!
Number
A department controls Name

a number of projects, PROJECT


Location
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

each of which has a


unique name, a unique Controlling_
Department
number, and a single
location.
Name Number

DEPARTMENT
Location Manager

Manager_
Start_Date

Duke CS, Winter 2023 104


International University, VNU-HCMC

Answer
Name Number
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

PROJECT
Location
N Name Number

1 DEPARTMENT
CONTROLS Manager

Manager_
Location Start_Date

Duke CS, Winter 2023 105

International University, VNU-HCMC

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

to one department, but Location Manager_


Start_Date

may work on several Supervisor


SSN
Salary
Sex Department

projects, which are not 1


Supervision

Birthdate
EMPLOYEE
Address
necessarily controlled by N 1
Supervisee Name W orks_On
the same department. We Project

FName LName Sex


keep track of the current MI
Hours

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

Location Name Number

Duke CS, Winter 2023 107

International University, VNU-HCMC

All Together Now!


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 108


International University, VNU-HCMC

The UML class diagram notation


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Winter 2023 109

International University, VNU-HCMC


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Thank you for your attention!

Duke CS, Winter 2023 110

You might also like