Download - Lecture 02: Conceptual Design, Normal Forms
![Page 1: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/1.jpg)
1
Lecture 02: Conceptual Design,
Normal Forms Tuesday, April 7, 2009
![Page 2: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/2.jpg)
2
Outline
• Chapter 2: Database design
• Chapter 19: Normal forms
Note: slides for Lecture 1 have been updated. Please reprint.
![Page 3: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/3.jpg)
Database Design
• Requirements analysis – Discussions with user groups
• Conceptual database design – E/R model
• Logical Database design – Database normalization
3
![Page 4: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/4.jpg)
Entity / Relationship Diagrams
• Entities:
• Attributes:
• Relationships:
4
Product
address
buys
![Page 5: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/5.jpg)
5
Person
Company Product
buys
makes
employs
name category
price
address name ssn
stockprice
name
![Page 6: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/6.jpg)
6
Keys in E/R Diagrams
• Every entity set must have a key
Product
name category
price
![Page 7: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/7.jpg)
7
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
![Page 8: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/8.jpg)
8
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
Note: “many-one” actually means “many-[zero-or-one]”
![Page 9: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/9.jpg)
Notation in Class v.s. the Book
9
makes Company Product
makes Company Product
In class:
In the book:
![Page 10: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/10.jpg)
10 address name ssn
Person
buys
makes
employs
Company Product
name category
stockprice
name
price
What does this say ?
![Page 11: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/11.jpg)
11
Multi-way Relationships How do we model a purchase relationship between buyers, products and stores?
Purchase
Product
Person
Store
![Page 12: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/12.jpg)
12
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
![Page 13: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/13.jpg)
13
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
![Page 14: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/14.jpg)
14
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
![Page 15: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/15.jpg)
15
Reification: Multi-way to Binary
Purchase
Person
Store
Product
StoreOf
ProductOf
BuyerOf
date
![Page 16: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/16.jpg)
16
3. Design Principles
Purchase Product Person
What’s wrong?
President Person Country
Moral: be faithful!
![Page 17: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/17.jpg)
17
Design Principles: What’s Wrong?
Purchase
Product
Store
date
personName personAddr
Moral: pick the right kind of entities.
![Page 18: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/18.jpg)
18
Design Principles: What’s Wrong?
Purchase
Product
Person
Store
date Dates
Moral: don’t complicate life more than it already is.
![Page 19: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/19.jpg)
19
From E/R Diagrams to Relational Schema
• Entity set relation • Relationship relation
![Page 20: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/20.jpg)
20
Entity Set to Relation
Product
name category
price
Product
Name Category Price Gizmo Gadgets $19.99
![Page 21: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/21.jpg)
21
Relationships to Relations
makes Company Product
name category
Stock price
name
Makes
Start Year price
(watch out for attribute name conflicts)
ProdName ProdCategory CompanyName StartYear Gizmo Gadgets gizmoWorks 1963
![Page 22: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/22.jpg)
22
Relationships to Relations
makes Company Product
name category
Stock price
name
No need for Makes. Modify Product:
Start Year price
Name Category Price CompanyName StartYear Gizmo Gadgets $19.99 gizmoWorks 1963
![Page 23: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/23.jpg)
23
Multi-way Relationships to Relations
Purchase
Product
Person
Store name price
ssn name
name address
Purchase(productName, ssn,
storeName)
![Page 24: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/24.jpg)
24
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
![Page 25: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/25.jpg)
25
Product
name category
price
isa isa
Educational Product Software Product
Age Group platforms
Subclasses
![Page 26: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/26.jpg)
26
Understanding Subclasses
• Think in terms of records: – Product
– SoftwareProduct
– EducationalProduct
field1 field2
field1 field2
field1 field2
field3
field4 field5
![Page 27: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/27.jpg)
27
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 todler
Toy retired
Product
Sw.Product
Ed.Product
![Page 28: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/28.jpg)
28
• OO: classes are disjoint (same for Java, C++)
p1 p2 p3
sp1
sp2
ep1
ep2
ep3
Difference between OO and E/R inheritance
Product
SoftwareProduct EducationalProduct
![Page 29: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/29.jpg)
29
• E/R: entity sets overlap
Difference between OO and E/R inheritance
SoftwareProduct
EducationalProduct
p1 p2 p3
sp1
sp2
ep1
ep2
ep3
Product
![Page 30: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/30.jpg)
30
No need for multiple inheritance in E/R
SoftwareProduct
EducationalProduct
p1 p2 p3
sp1
sp2
ep1
ep2
ep3
Product
esp1 esp2
We have three entity sets, but four different kinds of objects.
![Page 31: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/31.jpg)
31
Modeling UnionTypes With Subclasses
FurniturePiece
Person Company
Say: each piece of furniture is owned either by a person, or by a company
![Page 32: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/32.jpg)
32
Modeling Union Types with Subclasses
Say: each piece of furniture is owned either by a person, or by a company
Solution 1. Acceptable, imperfect (What’s wrong ?) FurniturePiece Person Company
ownedByPerson ownedByPerson
![Page 33: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/33.jpg)
33
Modeling Union Types with Subclasses
Solution 2: better, more laborious
isa
FurniturePiece
Person Company ownedBy
Owner
isa
Use THIS solution in homework 2 !
![Page 34: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/34.jpg)
Constraints in E/R Diagrams
• Key constraints
• Single value constraints
• Referential integrity constraints
• Cardinality constraints 34
![Page 35: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/35.jpg)
35
Keys in E/R Diagrams
Product
name category
price
In E/R diagrams each entity set must have exactly one key (consisting of one or more attributes)
![Page 36: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/36.jpg)
36
Single Value Constraints
makes
makes
v. s.
![Page 37: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/37.jpg)
37
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.
![Page 38: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/38.jpg)
38
Cardinality Constraints
Company Product makes <100
What does this mean ?
![Page 39: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/39.jpg)
39
Weak Entity Sets Weak entity set = entity where part of the key comes from another
number
sport
name
Convert to a relational schema (in class)
affiliation Team University
![Page 40: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/40.jpg)
40
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 41: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/41.jpg)
41
Schema Refinements = Normal Forms
• 1st Normal Form = all tables are flat • 2nd Normal Form = obsolete • Boyce Codd Normal Form = will study • 3rd Normal Form = see book
![Page 42: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/42.jpg)
42
First Normal Form (1NF) • A database schema is in First Normal Form
if all tables are flat
Name GPA Courses
Alice 3.8
Bob 3.7
Carol 3.9
Math
DB
OS
DB
OS
Math
OS
Student Name GPA
Alice 3.8
Bob 3.7
Carol 3.9
Student
Course
Math
DB
OS
Student Course
Alice Math
Carol Math
Alice DB
Bob DB
Alice OS
Carol OS
Takes Course
May need to add keys
![Page 43: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/43.jpg)
43
Relational Schema Design Person buys Product
name
price name ssn
Conceptual Model:
Relational Model: plus FD’s
Normalization: Eliminates anomalies
![Page 44: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/44.jpg)
44
Data Anomalies
When a database is poorly designed we get anomalies:
Redundancy: data is repeated
Updated anomalies: need to change in several places
Delete anomalies: may lose data when we don’t want
![Page 45: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/45.jpg)
45
Relational Schema Design
Anomalies: • Redundancy = repeat data • Update anomalies = Fred moves to “Bellevue” • Deletion anomalies = Joe deletes his phone number:
what is his city ?
Recall set attributes (persons with several phones): Name SSN PhoneNumber City Fred 123-45-6789 206-555-1234 Seattle Fred 123-45-6789 206-555-6543 Seattle Joe 987-65-4321 908-555-2121 Westfield
One person may have multiple phones, but lives in only one city
![Page 46: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/46.jpg)
46
Relation Decomposition Break the relation into two:
Name SSN City Fred 123-45-6789 Seattle Joe 987-65-4321 Westfield
SSN PhoneNumber 123-45-6789 206-555-1234 123-45-6789 206-555-6543 987-65-4321 908-555-2121 Anomalies have gone:
• No more repeated data • Easy to move Fred to “Bellevue” (how ?) • Easy to delete all Joe’s phone number (how ?)
Name SSN PhoneNumber City Fred 123-45-6789 206-555-1234 Seattle Fred 123-45-6789 206-555-6543 Seattle Joe 987-65-4321 908-555-2121 Westfield
![Page 47: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/47.jpg)
47
Relational Schema Design (or Logical Design)
Main idea: • Start with some relational schema • Find out its functional dependencies • Use them to design a better relational
schema
![Page 48: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/48.jpg)
48
Functional Dependencies
• A form of constraint – hence, part of the schema
• Finding them is part of the database design • Also used in normalizing the relations
![Page 49: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/49.jpg)
49
Functional Dependencies Definition:
If two tuples agree on the attributes
then they must also agree on the attributes
Formally:
A1, A2, …, An B1, B2, …, Bm
A1, A2, …, An
B1, B2, …, Bm
![Page 50: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/50.jpg)
50
When Does an FD Hold
Definition: A1, ..., Am B1, ..., Bn holds in R if:
∀t, t’ ∈ R, (t.A1=t’.A1 ∧ ... ∧ t.Am=t’.Am ⇒ t.B1=t’.B1 ∧ ... ∧ t.Bn=t’.Bn )
A1 ... Am B1 ... nm
if t, t’ agree here then t, t’ agree here
t
t’
R
![Page 51: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/51.jpg)
51
Examples
EmpID Name, Phone, Position Position Phone but not Phone Position
An FD holds, or does not hold on an instance:
EmpID Name Phone Position E0045 Smith 1234 Clerk E3542 Mike 9876 Salesrep E1111 Smith 9876 Salesrep E9999 Mary 1234 Lawyer
![Page 52: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/52.jpg)
52
Example
Position Phone
EmpID Name Phone Position E0045 Smith 1234 Clerk E3542 Mike 9876 ← Salesrep E1111 Smith 9876 ← Salesrep E9999 Mary 1234 Lawyer
![Page 53: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/53.jpg)
53
Example
EmpID Name Phone Position E0045 Smith 1234 → Clerk E3542 Mike 9876 Salesrep E1111 Smith 9876 Salesrep E9999 Mary 1234 → Lawyer
but not Phone Position
![Page 54: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/54.jpg)
54
Example FD’s are constraints: • On some instances they hold • On others they don’t
name category color department price
Gizmo Gadget Green Toys 49
Tweaker Gadget Green Toys 99
Does this instance satisfy all the FDs ?
name color category department color, category price
![Page 55: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/55.jpg)
55
Example
name category color department price
Gizmo Gadget Green Toys 49
Tweaker Gadget Black Toys 99
Gizmo Stationary Green Office-supp. 59
What about this one ? (At home…)
name color category department color, category price
![Page 56: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/56.jpg)
56
An Interesting Observation
If all these FDs are true: name color category department color, category price
Then this FD also holds: name, category price
Why ??
![Page 57: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/57.jpg)
57
Goal: Find ALL Functional Dependencies
• Anomalies occur when certain “bad” FDs hold
• We know some of the FDs
• Need to find all FDs, then look for the bad ones
![Page 58: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/58.jpg)
58
Armstrong’s Rules (1/3)
Is equivalent to
Splitting rule and Combing rule
A1 ... Am B1 ... Bm
A1, A2, …, An B1, B2, …, Bm
A1, A2, …, An B1 A1, A2, …, An B2 . . . . . A1, A2, …, An Bm
![Page 59: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/59.jpg)
59
Armstrong’s Rules (2/3)
Trivial Rule
Why ? A1 … Am
where i = 1, 2, ..., n
A1, A2, …, An Ai
![Page 60: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/60.jpg)
60
Armstrong’s Rules (3/3)
Transitive Closure Rule
If
and
then
Why ?
A1, A2, …, An B1, B2, …, Bm
B1, B2, …, Bm C1, C2, …, Cp
A1, A2, …, An C1, C2, …, Cp
![Page 61: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/61.jpg)
61
A1 … Am B1 … Bm C1 ... Cp
![Page 62: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/62.jpg)
62
Example (continued)
Start from the following FDs:
Infer the following FDs:
1. name color 2. category department 3. color, category price
Inferred FD Which Rule did we apply ?
4. name, category name 5. name, category color 6. name, category category 7. name, category color, category 8. name, category price
![Page 63: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/63.jpg)
63
Example (continued)
Answers:
Inferred FD Which Rule did we apply ?
4. name, category name Trivial rule 5. name, category color Transitivity on 4, 1 6. name, category category Trivial rule 7. name, category color, category Split/combine on 5, 6 8. name, category price Transitivity on 3, 7
1. name color 2. category department 3. color, category price
THIS IS TOO HARD ! Let’s see an easier way.
![Page 64: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/64.jpg)
64
Closure of a set of Attributes Given a set of attributes A1, …, An
The closure, {A1, …, An}+ = the set of attributes B s.t. A1, …, An B
name color category department color, category price
Example:
Closures: name+ = {name, color} {name, category}+ = {name, category, color, department, price} color+ = {color}
![Page 65: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/65.jpg)
65
Closure Algorithm X={A1, …, An}.
Repeat until X doesn’t change do:
if B1, …, Bn C is a FD and B1, …, Bn are all in X then add C to X.
{name, category}+ = { }
name color category department color, category price
Example:
name, category, color, department, price
Hence: name, category color, department, price
![Page 66: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/66.jpg)
66
Example
Compute {A,B}+ X = {A, B, }
Compute {A, F}+ X = {A, F, }
R(A,B,C,D,E,F) A, B C A, D E B D A, F B
In class:
![Page 67: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/67.jpg)
67
Why Do We Need Closure
• With closure we can find all FD’s easily
• To check if X → A – Compute X+ – Check if A ∈ X+
![Page 68: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/68.jpg)
68
Using Closure to Infer ALL FDs
A, B C A, D B B D
Example:
Step 1: Compute X+, for every X:
A+ = A, B+ = BD, C+ = C, D+ = D AB+ =ABCD, AC+=AC, AD+=ABCD, BC+=BCD, BD+=BD, CD+=CD ABC+ = ABD+ = ACD+ = ABCD (no need to compute– why ?) BCD+ = BCD, ABCD+ = ABCD
Step 2: Enumerate all FD’s X Y, s.t. Y ⊆ X+ and X∩Y = ∅: AB CD, ADBC, BCD
![Page 69: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/69.jpg)
69
Another Example
• Enrollment(student, major, course, room, time) student major major, course room course time
What else can we infer ? [in class, or at home]
![Page 70: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/70.jpg)
70
Keys
• A superkey is a set of attributes A1, ..., An s.t. for any other attribute B, we have A1, ..., An B
• A key is a minimal superkey – I.e. set of attributes which is a superkey and for which
no subset is a superkey
![Page 71: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/71.jpg)
71
Computing (Super)Keys
• Compute X+ for all sets X • If X+ = all attributes, then X is a key • List only the minimal X’s
![Page 72: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/72.jpg)
72
Example
Product(name, price, category, color)
name, category price category color
What is the key ?
![Page 73: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/73.jpg)
73
Example
Product(name, price, category, color)
name, category price category color
What is the key ?
(name, category) + = name, category, price, color
Hence (name, category) is a key
![Page 74: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/74.jpg)
74
Examples of Keys
Enrollment(student, address, course, room, time)
student address room, time course student, course room, time
(find keys at home)
![Page 75: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/75.jpg)
75
Eliminating Anomalies
Main idea:
• X → A is OK if X is a (super)key
• X → A is not OK otherwise
![Page 76: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/76.jpg)
76
Example
What the key? {SSN, PhoneNumber}
Name SSN PhoneNumber City Fred 123-45-6789 206-555-1234 Seattle Fred 123-45-6789 206-555-6543 Seattle Joe 987-65-4321 908-555-2121 Westfield Joe 987-65-4321 908-555-1234 Westfield
SSN Name, City
Hence SSN Name, City is a “bad” dependency
![Page 77: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/77.jpg)
77
Key or Keys ?
Can we have more than one key ?
Given R(A,B,C) define FD’s s.t. there are two or more keys
![Page 78: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/78.jpg)
78
Key or Keys ?
Can we have more than one key ?
Given R(A,B,C) define FD’s s.t. there are two or more keys
ABC BCA
ABC BAC or
what are the keys here ? Can you design FDs such that there are three keys ?
![Page 79: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/79.jpg)
79
Boyce-Codd Normal Form A simple condition for removing anomalies from relations:
In other words: there are no “bad” FDs
A relation R is in BCNF if:
If A1, ..., An B is a non-trivial dependency
in R , then {A1, ..., An} is a superkey for R
Equivalently: ∀ X, either (X+ = X) or (X+ = all attributes)
![Page 80: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/80.jpg)
80
BCNF Decomposition Algorithm
A’s Others B’s
R1
Is there a 2-attribute relation that is not in BCNF ?
repeat choose A1, …, Am B1, …, Bn that violates BNCF split R into R1(A1, …, Am, B1, …, Bn) and R2(A1, …, Am, [others]) continue with both R1 and R2 until no more violations
R2 In practice, we have a better algorithm (coming up)
![Page 81: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/81.jpg)
81
Example
What the key? {SSN, PhoneNumber}
Name SSN PhoneNumber City Fred 123-45-6789 206-555-1234 Seattle Fred 123-45-6789 206-555-6543 Seattle Joe 987-65-4321 908-555-2121 Westfield Joe 987-65-4321 908-555-1234 Westfield
SSN Name, City
use SSN Name, City to split
![Page 82: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/82.jpg)
82
Example
Name SSN City Fred 123-45-6789 Seattle Joe 987-65-4321 Westfield
SSN PhoneNumber 123-45-6789 206-555-1234 123-45-6789 206-555-6543 987-65-4321 908-555-2121 987-65-4321 908-555-1234
SSN Name, City
Let’s check anomalies: • Redundancy ? • Update ? • Delete ?
![Page 83: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/83.jpg)
83
BCNF Decomposition Algorithm BCNF_Decompose(R)
find X s.t.: X ≠X+ ≠ [all attributes]
if (not found) then “R is in BCNF”
let Y = X+ - X let Z = [all attributes] - X+ decompose R into R1(X ∪ Y) and R2(X ∪ Z) continue to decompose recursively R1 and R2
![Page 84: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/84.jpg)
84
Example BCNF Decomposition Person(name, SSN, age, hairColor, phoneNumber)
SSN name, age age hairColor
Find X s.t.: X ≠X+ ≠ [all attributes]
In class….
![Page 85: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/85.jpg)
85
Example BCNF Decomposition Person(name, SSN, age, hairColor, phoneNumber)
SSN name, age age hairColor
Iteration 1: Person SSN+ = SSN, name, age, hairColor Decompose into: P(SSN, name, age, hairColor) Phone(SSN, phoneNumber)
Iteration 2: P age+ = age, hairColor Decompose: People(SSN, name, age) Hair(age, hairColor) Phone(SSN, phoneNumber)
Find X s.t.: X ≠X+ ≠ [all attributes]
What are the keys ?
![Page 86: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/86.jpg)
86
Example
What are the keys ?
A B B C
R(A,B,C,D) A+ = ABC ≠ ABCD
R(A,B,C,D)
What happens if in R we first pick B+ ? Or AB+ ?
R1(A,B,C) B+ = BC ≠ ABC
R2(A,D)
R11(B,C) R12(A,B)
![Page 87: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/87.jpg)
87
Decompositions in General
R1 = projection of R on A1, ..., An, B1, ..., Bm R2 = projection of R on A1, ..., An, C1, ..., Cp
R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)
R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)
![Page 88: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/88.jpg)
88
Theory of Decomposition
• Sometimes it is correct: Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
Gizmo 19.99 Camera
Name Price
Gizmo 19.99
OneClick 24.99
Gizmo 19.99
Name Category
Gizmo Gadget
OneClick Camera
Gizmo Camera
Lossless decomposition
![Page 89: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/89.jpg)
89
Incorrect Decomposition
• Sometimes it is not:
Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
Gizmo 19.99 Camera
Name Category
Gizmo Gadget
OneClick Camera
Gizmo Camera
Price Category
19.99 Gadget
24.99 Camera
19.99 Camera
What’s incorrect ??
Lossy decomposition
![Page 90: Lecture 02: Conceptual Design, Normal Forms](https://reader031.vdocument.in/reader031/viewer/2022012104/616a1aba11a7b741a34ed957/html5/thumbnails/90.jpg)
90
Decompositions in General R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)
If A1, ..., An B1, ..., Bm Then the decomposition is lossless
R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)
BCNF decomposition is always lossless. WHY ?
Note: don’t need A1, ..., An C1, ..., Cp