Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R...
Transcript of Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R...
![Page 1: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/1.jpg)
Introduction to Data Management CSE 344
Lecture 15: E/R Diagrams
Guest Lecturer: Sudeepa Roy
CSE 344 - Fall 2013 1
![Page 2: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/2.jpg)
Announcements
• WQ6 due Thursday (there is no WQ5…)
• Homework 4 posted, due Friday
• Midterm: Monday, November 4th, in class
Today: E/R diagrams (4.1-4.6)
![Page 3: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/3.jpg)
Today: E/R Diagrams
Motivating scenario: your boss asks you to setup a DBMS about:
• Companies. Each company has: – A name, an address, and a CEO – A list of employees, with ssn, name, and address
• Products manufactured by these companies – Each product has a name and a price – The same product may be manufactured by several companies
• Buyers of these products – Each buyer has an ssn, name, and address – Some employees may be buyers too
![Page 4: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/4.jpg)
Database Design
• Why do we need it? – Need a way to model real world entities in terms of relations – Not easy to go from real-world entities to a database schema
• Consider issues such as: – What entities to model – How entities are related – What constraints exist in the domain – How to achieve good designs
• Several formalisms exists – We discuss E/R diagrams
CSE 344 - Fall 2013 4
![Page 5: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/5.jpg)
Database Design Process company makes product
name
price name address
Conceptual Model:
Relational Model: Tables + constraints And also functional dep.
Normalization: Eliminates anomalies
Conceptual Schema
Physical Schema Physical storage details
![Page 6: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/6.jpg)
Entity / Relationship Diagrams
• Entity set = a class – An entity = an object
• Attribute
• Relationship
CSE 344 - Fall 2013 6
Product
city
makes
![Page 7: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/7.jpg)
Person
Company Product
buys
makes
employs
name CEO
price
address name ssn
address
name
7
![Page 8: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/8.jpg)
Keys in E/R Diagrams
• Every entity set must have a key
Product
name
price
CSE 344 - Fall 2013 8
![Page 9: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/9.jpg)
What is a Relation ?
• A mathematical definition: – if A, B are sets, then a relation R is a subset of A × B
• A={1,2,3}, B={a,b,c,d}, A × B = {(1,a),(1,b), . . ., (3,d)} R = {(1,a), (1,c), (3,b)}
• makes is a subset of Product × Company:
1
2
3
a
b
c
d
A=
B=
makes Company Product
CSE 344 - Fall 2013 9
![Page 10: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/10.jpg)
Multiplicity of E/R Relations
• one-one:
• many-one
• many-many
1 2 3
a b c d
1 2 3
a b c d
1 2 3
a b c d
CSE 344 - Fall 2013 10
![Page 11: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/11.jpg)
Person
Company Product
buys
makes
employs
name CEO
price
address name ssn
address
name
11
What does this say ?
![Page 12: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/12.jpg)
Multi-way Relationships How do we model a purchase relationship between buyers, products and stores?
Purchase
Product
Person
Store
Can still model as a mathematical set (Q. how ?)
12 A. As a set of triples ⊆ Person × Product × Store
![Page 13: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/13.jpg)
Q: What does the arrow mean ?
Arrows in Multiway Relationships
A: A given person buys a given product from at most one store
Purchase
Product
Person
Store
13
[Arrow pointing to E means that if we select one entity from each of the other entity sets in the relationship, those entities are related to at most one entity in E]
![Page 14: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/14.jpg)
Q: What does the arrow mean ?
Arrows in Multiway Relationships
A: A given person buys a given product from at most one store AND every store sells to every person at most one product
Purchase
Product
Person
Store
CSE 344 - Fall 2013 14
![Page 15: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/15.jpg)
Q: How do we say that every person shops at at most one store ?
Arrows in Multiway Relationships
A: Cannot. This is the best approximation. (Why only approximation ?)
Purchase
Product
Person
Store
CSE 344 - Fall 2013 15
![Page 16: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/16.jpg)
Converting Multi-way Relationships to Binary
Purchase
Person
Store
Product
StoreOf
ProductOf
BuyerOf
date
Arrows go in which direction? 16
![Page 17: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/17.jpg)
Converting Multi-way Relationships to Binary
Purchase
Person
Store
Product
StoreOf
ProductOf
BuyerOf
date
Make sure you understand why! 17
![Page 18: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/18.jpg)
3. Design Principles
Purchase Product Person
What’s wrong?
President Person Country
Moral: be faithful to the specifications of the app!
CSE 344 - Fall 2013 18
![Page 19: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/19.jpg)
Design Principles: What’s Wrong?
Purchase
Product
Store
date
personName personAddr
Moral: pick the right kind of entities.
CSE 344 - Fall 2013 19
![Page 20: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/20.jpg)
Design Principles: What’s Wrong?
Purchase
Product
Person
Store
date Dates
Moral: don’t complicate life more than it already is.
20
![Page 21: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/21.jpg)
From E/R Diagrams to Relational Schema
• Entity set à relation • Relationship à relation
CSE 344 - Fall 2013 21
![Page 22: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/22.jpg)
Entity Set to Relation
Product
prod-ID category
price
Product(prod-ID, category, price)
prod-ID category price Gizmo55 Camera 99.99 Pokemn19 Toy 29.99 22
![Page 23: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/23.jpg)
Create Table (SQL)
CREATE TABLE Product ( prod-ID CHAR(30) PRIMARY KEY, category VARCHAR(20),
price double)
CSE 344 - Fall 2013 23
![Page 24: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/24.jpg)
N-N Relationships to Relations
Orders
prod-ID cust-ID
date
Shipment Shipping-Co
address
name date
Represent that in relations! CSE 344 - Fall 2013 24
![Page 25: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/25.jpg)
N-N Relationships to Relations
Orders
prod-ID cust-ID
date
Shipment Shipping-Co
address
name
Orders(prod-ID,cust-ID, date) Shipment(prod-ID,cust-ID, name, date) Shipping-Co(name, address)
date
prod-ID cust-ID name date
Gizmo55 Joe12 UPS 4/10/2011
Gizmo55 Joe12 FEDEX 4/9/2011
![Page 26: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/26.jpg)
Create Table (SQL) CREATE TABLE Shipment(
name CHAR(30) REFERENCES Shipping-Co,
prod-ID CHAR(30), cust-ID VARCHAR(20), date DATETIME, PRIMARY KEY (name, prod-ID, cust-ID), FOREIGN KEY (prod-ID, cust-ID) REFERENCES Orders )
![Page 27: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/27.jpg)
N-1 Relationships to Relations
Orders
prod-ID cust-ID
date
Shipment Shipping-Co
address
name date
Represent this in relations! CSE 344 - Fall 2013 27
![Page 28: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/28.jpg)
N-1 Relationships to Relations
Orders
prod-ID cust-ID
date
Shipment Shipping-Co
address
name
Orders(prod-ID,cust-ID, date1, name, date2) Shipping-Co(name, address)
date
Remember: no separate relations for many-one relationship
![Page 29: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/29.jpg)
Multi-way Relationships to Relations
Purchase
Product
Person
Store prod-ID price
ssn name
name address
29 Purchase(prod-ID, cust-ssn, store-name)
![Page 30: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/30.jpg)
Modeling Subclasses Some objects in a class may be special
• define a new class • better: define a subclass
Products
Software products
Educational products
So --- we define subclasses in E/R CSE 344 - Fall 2013 30
![Page 31: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/31.jpg)
Product
name category
price
isa isa
Educational Product Software Product
Age Group platforms
Subclasses
CSE 344 - Fall 2013
![Page 32: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/32.jpg)
Understanding Subclasses
• Think in terms of records: – Product
– SoftwareProduct
– EducationalProduct
field1 field2
field1 field2
field1 field2
field3
field4 field5
CSE 344 - Fall 2013 32
![Page 33: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/33.jpg)
Subclasses to
Relations
Product
name category
price
isa isa
Educational Product Software Product
Age Group platforms
Name Price Category
Gizmo 99 gadget
Camera 49 photo
Toy 39 gadget
Name platforms
Gizmo unix
Name Age Group
Gizmo toddler
Toy retired
Product
Sw.Product
Ed.Product
Other ways to convert are possible
![Page 34: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/34.jpg)
Modeling UnionTypes With Subclasses
FurniturePiece
Person Company
Say: each piece of furniture is owned either by a person or by a company
CSE 344 - Fall 2013 34
![Page 35: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/35.jpg)
Modeling Union Types with Subclasses
Say: each piece of furniture is owned either by a person or by a company
Solution 1. Acceptable but imperfect (What’s wrong ?) FurniturePiece Person Company
ownedByPerson ownedByComp.
CSE 344 - Fall 2013 35
![Page 36: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/36.jpg)
Modeling Union Types with Subclasses
Solution 2: better, more laborious
isa
FurniturePiece
Person Company ownedBy
Owner
isa
CSE 344 - Fall 2013 36
![Page 37: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/37.jpg)
37
Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related.
University Team affiliation
number sport name
Team(sport, number, universityName) University(name)
CSE 344 - Fall 2013
![Page 38: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/38.jpg)
What Are the Keys of R ?
R
A
B
S
T
V
Q
U W
V
Z
C
D E G
K
H
F L
![Page 39: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/39.jpg)
Constraints in E/R Diagrams
Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Other constraints: peoples’ ages are between 0 and 150.
CSE 344 - Fall 2013 39
![Page 40: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/40.jpg)
Keys in E/R Diagrams
address name ssn
Person
Product
name category
price
No formal way to specify multiple keys in E/R diagrams
Underline:
![Page 41: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/41.jpg)
Single Value Constraints
makes
makes
v. s.
CSE 344 - Fall 2013 41
![Page 42: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/42.jpg)
Referential Integrity Constraints
Company Product makes
Company Product makes
Each product made by at most one company. Some products made by no company
Each product made by exactly one company.
CSE 344 - Fall 2013 42
Note: For weak entity sets should be replaced by (sec 4.4.2)
![Page 43: Introduction to Data Management CSE 344 · Introduction to Data Management CSE 344 Lecture 15: E/R Diagrams Guest Lecturer: Sudeepa Roy CSE 344 - Fall 2013 1](https://reader030.fdocuments.us/reader030/viewer/2022040107/5e70fdec1ce010496639eabc/html5/thumbnails/43.jpg)
Other Constraints
Company Product makes <100
Q: What does this mean ? A: A Company entity cannot be connected by relationship to more than 99 Product entities
CSE 344 - Fall 2013 43
Note: For “at least one”, you can use “≥ 1” in a many-many relationship