Entity Relationship Diagram
Entity Relationship Diagram
Entity Relationship Diagram
Today’s Lecture
2
Lecture 4 > Section 1
3
Lecture 4 > Section 1
2. Entities
3. Relations
4
Lecture 4 > Section 1 > Motivation
Database Design
• Database design: Why do we need it?
• Agree on structure of the database before deciding on a particular implementation
5
Lecture 4 > Section 1 > Motivation
2. Conceptual Design
3. More:
• Logical Database Design
• Security Design
8
Lecture 4 > Section 1 > Motivation
This process is
E/R Model & Diagrams used
iterated many
name times
name category
price
Product Makes Company
10
Lecture 4 > Section 1 > Entities
11
Lecture 4 > Section 1 > Entities
12
Lecture 4 > Section 1 > Entities
Entity
name category
Name: Xbox
price
Category: Total Name: My Little Pony Doll Entity
Product
Multimedia System
Price: $250
Category: Toy
Price: $25
Attribute
Product
Entity Set
13
Lecture 4 > Section 1 > Entities
Keys
• A key is a minimal set of attributes that uniquely identifies an entity.
The E/R model forces us to designate a single primary key, though there
may be multiple candidate keys
14
Lecture 4 > Section 1 > Relationships
name name
category
price
Product Makes Company
15
Lecture 4 > Section 1 > Relationships
name category
name
price
makes Company
Product
stockprice
buys
employs
Person
What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d} b
2
c
3
d
17
Lecture 4 > Section 1 > Relationships
What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d} b
2
18
Lecture 4 > Section 1 > Relationships
What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d}, b
2
19
Lecture 4 > Section 1 > Relationships
What is a Relationship?
• A mathematical definition:
• Let A, B be sets B=
• A x B (the cross-product) is the set of all pairs A= 1 a
• A relationship is a subset of A x B 2 b
c
• Makes is relationship- it is a subset of 3
Product ´ Company: d
makes Company
Product
20
Lecture 4 > Section 1 > Relationships
What is a Relationship?
name name
category
price
Product Makes Company
21
Lecture 4 > Section 1 > Relationships
What is a Relationship?
Company Product
name name category price
GizmoWorks Gizmo Electronics $9.99
GadgetCorp GizmoLite Electronics $7.50
Gadget Toys $5.50
What is a Relationship?
Product Company C × Product P
Company
name name category price C.name P.name P.category P.price
What is a Relationship?
Product Company C × Product P
Company
name name category price C.name P.name P.category P.price
What is a Relationship?
• There can only be one relationship for every This follows from our
unique combination of entities mathematical
definition of a
relationship- it’s a SET!
since
name
name category
price
Product Makes Company
date
price
• A: A person can only buy a specific product once (on one date)
name
name category Purchase
price
ProductOf BuyerOf
Product Person
29
Lecture 4 > Section 1 > ACTIVITY
Teams play each Players belong to A Game is made up A Play will contain
other in Games. Teams (assume of Plays that result either a Pass from
Each pair of teams no trades / in a yardage one player to
can play each other changes). gain/loss, and another, or a Run by
multiple times potentially a one player
touchdown
30
Lecture 4 > Section 2
31
Lecture 4 > Section 2
2. Design considerations
3. Conversion to SQL
32
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
33
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
name category
name
price
makes Company
Product
stockprice
What does
buys this say?
employs
Person
Multi-way Relationships
How do we model a purchase relationship between buyers,
products and stores?
Product
Purchase Store
Person
35
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
Product
Purchase Store
Person
36
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
Product
Purchase Store
Person
37
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
Product
Purchase Store
Person
BuyerOf Person
39
Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way
Are these
correct?
Purchase StoreOf Store
BuyerOf Person
40
Lecture 4 > Section 2 > Design decisions
Purchase Store
Purchase StoreOf Store
BuyerOf Person
Person
41
Lecture 4 > Section 2 > Design decisions
Purchase Store
Purchase StoreOf Store
BuyerOf Person
Person
Multiple purchases per
(product, store, person)
combo possible here!
42
Lecture 4 > Section 2 > Design decisions
Purchase Store
Purchase StoreOf Store
BuyerOf Person
Person
We can add more-fine-
grained constraints here!
• (B) is also useful when we want to add details (constraints or attributes) to the
relationship
- “A person who shops in only one store”
- “How long a person has been shopping at a store”
43
Lecture 4 > Section 2 > Design decisions
Purchase Store
Purchase StoreOf Store
BuyerOf Person
Person
44
Lecture 4 > Section 2 > Design decisions
3. Design Principles
What’s wrong with these examples?
45
Lecture 4 > Section 2 > Design decisions
Design Principles:
What’s Wrong?
date
Product
Purchase Store
personAddr
personName
46
Lecture 4 > Section 2 > Design decisions
Design Principles:
What’s Wrong?
Dates date
Product
Purchase Store
Person
47
Lecture 4 > Section 2 > Design decisions
Address
AddrOf
Employee
Employee
48
Lecture 4 > Section 2 > Design decisions
Addr 1 Addr 2
How do we handle addresses
where internal structure of the
address (e.g. zip code, state) is
Employee
useful?
49
Lecture 4 > Section 2 > Design decisions
AddrOf
Employee
Employee
51
Lecture 4 > Section 2 > Conversion to SQL
52
Lecture 4 > Section 2 > Conversion to SQL
name
Product
CREATE TABLE Product(
name CHAR(50) PRIMARY KEY,
price DOUBLE,
category VARCHAR(30)
)
Product
name price category
Gizmo1 99.99 Camera
Gizmo2 19.99 Edible
53
Lecture 4 > Section 2 > Conversion to SQL
Purchased
– Each row/tuple is name firstname lastname date
• composed of the union of the entity sets’ keys
Gizmo1 Bob Joe 01/01/15
• has the entities’ primary keys as foreign keys
Gizmo2 Joe Bob 01/03/15
• has the union of the entity sets’ keys as primary
Gizmo1 JoeBob Smith 01/05/15
key
54
Lecture 4 > Section 2 > Conversion to SQL
55
Lecture 4 > Section 2 > Conversion to SQL
date
firstname lastname
name category
price
name address
Store
56
Lecture 4 > Section 2 > ACTIVITY
57
Lecture 4 > Section 2 > ACTIVITY
59
Lecture 4 > Section 3
60
Lecture 4 > Section 3
2. Constraints
61
Lecture 4 > Section 3 > Subclasses & OO
Modeling Subclasses
• Some objects in a class may be special, i.e. worthy of their own class
Software Educational
products products
Modeling Subclasses
name
platforms ageGroup
63
Lecture 4 > Section 3 > Subclasses & OO
Understanding Subclasses
• Think in terms of records; ex:
name Child subclasses contain
name
• Product all the attributes of all of
price price their parent classes plus
Product the new attributes
name shown attached to them
isA • SoftwareProduct in the E/R diagram
Software Product Educational Product
price
platforms
platforms ageGroup
name
• EducationalProduct
price
ageGroup
64
Lecture 4 > Section 3 > Subclasses & OO
Product
Think like tables…
name price category
Gizmo 99 gadget
name Camera 49 photo
Product
Sw.Product name platforms
p1 p2
p3 ep1 EducationalProduct
sp1
ep2
SoftwareProduct
sp2 ep3
66
Lecture 4 > Section 3 > Subclasses & OO
Product
p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2 ep3
67
Lecture 4 > Section 3 > Subclasses & OO
p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2
esp1 esp2 ep3
IsA Review
• If we declare A IsA B then every A is a B
• We use IsA to
69
Lecture 4 > Section 3 > Subclasses & OO
ownedByPerson ownedByComp
71
Lecture 4 > Section 3 > Subclasses & OO
FurniturePiece
What is happening here?
ownedBy
isa 72
Lecture 4 > Section 3 > Constraints
• Single-value constraints:
• Ex: a person can have only one father
Bold line indicates total participation (i.e. here: all products are made by a company)
74
Lecture 4 > Section 3 > Constraints
price
Note: no formal
way to specify
Product multiple keys in
E/R diagrams…
Person
makes
v. s.
makes
76
Lecture 4 > Section 3 > Constraints
E/R Summary
• E/R diagrams are a visual syntax that allows
technical and non-technical people to talk
• For conceptual design