Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COMP 430
Intro. to Database Systems
Entity-Relationship Diagram Basics
Slides use ideas from Chris Ré.
Goal: Agree on DB structure to
implement.
• What entities to model
• How entities related
• What constraints exist in
domain
• How to achieve good design
(Logical) Entity-Relationship Diagrams
name
name
category
price
Product
Makes
Company
“The Entity-Relationship model
– toward a unified view of data”
Peter Chen, 1976
Precise enough for technical decisions
Abstracted enough to omit some implementation details
Abstracted enough for non-technical people
Primary key – minimal set of
attributes that uniquely
identifies entity
Attributes of
entity sets
name
name
category
price
Product
Entity sets –
classes/types
of objects
Entities –
individual objects
Makes
Company
Relationship –
between
entity sets
name
category
name
price
Makes
Company
Product
stockprice
Buys
Employs
Person
address
name
ssn
Mathematical definitions
• Sets A, B:
A={1,2,3}, B={a,b,c,d}
A
1
B
a
2
b
3
c
d
Mathematical definitions
• Sets A, B:
A={1,2,3}, B={a,b,c,d}
• Cross-product A B:
The set of all (a,b) pairs
A B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d),
(3,a), (3,b), (3,c), (3,d)}
A
1
B
a
2
b
3
c
d
Mathematical definitions
• Sets A, B:
A={1,2,3}, B={a,b,c,d}
• Cross-product A B:
The set of all (a,b) pairs
A B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d),
(3,a), (3,b), (3,c), (3,d)}
• Relationship R:
• A subset of A B
• R = {(1,a), (2,c), (2,d), (3,b)}
A
1
B
a
2
b
3
c
d
name
name
category
price
Product
Makes
Company
Product and Company are sets of entities.
•
Entities uniquely identified by primary keys.
Makes is a relationship – a subset of Product Company.
•
Tuples uniquely identified by pairs of Product & Company’s keys.
Company
Product
name
price
category
name
Product p × Company c
p.name
p.price
c.category
c.name
Gizmo
19.99 Electronics
GizmoWorks
Gizmo
19.99 Electronics
GizmoWorks
Powergizmo
39.99 Electronics
GadgetCorp
Powergizmo
39.99 Electronics
GizmoWorks
Gadget
19.99 Toys
Gadget
19.99 Toys
GizmoWorks
Gizmo
19.99 Electronics
GadgetCorp
Powergizmo
39.99 Electronics
GadgetCorp
Gadget
19.99 Toys
GadgetCorp
name
name
category
price
Product
Makes
Company
Company
Product
name
price
category
name
Product p × Company c
p.name
p.price
c.category
c.name
Gizmo
19.99 Electronics
GizmoWorks
Gizmo
19.99 Electronics
GizmoWorks
Powergizmo
39.99 Electronics
GadgetCorp
Powergizmo
39.99 Electronics
GizmoWorks
Gadget
19.99 Toys
Gadget
19.99 Toys
GizmoWorks
Gizmo
19.99 Electronics
GadgetCorp
Powergizmo
39.99 Electronics
GadgetCorp
Gadget
19.99 Toys
GadgetCorp
Makes
name
name
category
price
Product
Makes
Company
p.name
p.price
c.category
c.name
Gizmo
19.99 Electronics
GizmoWorks
Powergizmo
39.99 Electronics
GizmoWorks
Gadget
19.99 Toys
GadgetCorp
Likes
ssn
name
Person
name
WorksFor
address
Company
stockprice
OwnsStock
Can have multiple relationships between same entity sets.
Each is a subset of Person Company with primary key (ssn, name).
ssn
Person
name
Friend
address
Can have relationship between single entity set.
A subset of Person Person with primary key (ssn, ssn).
Relationships can have attributes
since
name
name
category
price
Product
Makes
Company
Relationship attributes are implicitly unique per (p.name, c.name).
Relationship vs. entity
date
name
p_id
category
price
Product
Purchased
Person
Relationship attributes are implicitly unique per (name, p_id).
What if we don’t want uniqueness?
Relationship vs. entity
date
purch_id
quantity
p_id
name
category
price
Purchase
ProductOf
Product
BuyerOf
Person
Representing as an entity allows multiple Purchases for each Product-Person combination.
Activity – Draw ER diagram
Authors have IDs and
names. They write books.
Books have ISBNs and
titles. The library keeps
track of how many copies it
has of the book. Each book
is written by authors and
published by a publisher.
We want to know every
time it is checked out by a
borrower.
Borrowers have a library
card number, name, and
address. They can check
out a book on a particular
date.
Publishers have an ID,
name and address. They
publish books.