Lecture16
Lecture16
1
Back to Database Design
• Do we just translate the ER diagram?
In HW1, we saw that, we don’t really need a separate table for the relationship PLACES
2
Back to Database Design
• Is it a good idea to store the same information multiple times?
.
.
An instance of the ORDERS table . 4
Anomalies due to Redundant Storage
• Update anomaly: If one copy of a repeated data is updated, an
inconsistency is created unless all copies are similarly updated
• Rating determines the hourly wage
Suppose, we update hourly_wages for Attishoo, what will happen to Smiley and Madayan?
5
Anomalies due to Redundant Storage
• Insertion anomaly: It may not be possible to store certain information
unless some other, unrelated, information is stored
• Rating determines the hourly wage
Suppose, we want to insert a new employee whose rating is 6. what should be the hourly_wage?
6
Anomalies due to Redundant Storage
• Delete anomaly: It may not be possible to store certain information
without losing some other, unrelated, informatio
• Rating determines the hourly wage
Suppose, we delete Shemthusrt and Guldu’s informaiton from the table. How to keep the
association between rating and hourly_wages? 7
Decompositions
• Replace the relation with a collection of smaller relations
ssn name lot rating hourly_wages hours_worked
123-22-3666 Attishoo 48 8 10 40
231-31-5368 Smiley 22 8 10 30
131-24-3650 Smethurst 35 5 7 30
434-26-3751 Guldu 35 5 7 32
612-67-4134 Madayan 35 8 10 40
9
Functional Dependency
• A functional dependency is a kind of Integrity Constraint (IC)
• X → 𝑌 holds over relation R if the following holds for every pair of
tuples 𝑡1 and 𝑡2
• If 𝑡1 . 𝑋 = 𝑡2. 𝑋 then 𝑡1 . 𝑌 = 𝑡2 . 𝑌
A B C D
a1 b1 c1 d1
Do these FDs hold over the given relation?
a1 b1 c1 d1
A→𝐷
a1 b2 c2 d1 AB → 𝐶
a2 b1 c3 d2
a1 b2 c2 d1
10
Primary Key
• A special case of an FD beccause it uniquely determines the value of
other attributes
• Since no two tuples can have the same primary key, we never
encounter a situation where 𝑡1 . 𝑋 = 𝑡2. 𝑋, so no chance of violation the
FD condition: If 𝑡1 . 𝑋 = 𝑡2. 𝑋 then 𝑡1 . 𝑌 = 𝑡2 . 𝑌
What are the FDs in Relation?
P A B C D
P→𝐴 A→𝐷
p1 a1 b1 c1 d1 P→𝐵 AB → 𝐶
p2 a1 b1 c1 d1 P→𝐶
p3 a1 b2 c2 d1 P→𝐷
p4 a2 b1 c3 d2
P → 𝐴𝐵
P → 𝐴𝐵C
p5 a1 b2 c2 d1
P → 𝐴𝐵𝐶𝐷
11
Primary Key
• FD X → 𝑌 does not require the X to be minimal
• If X is minimal, then it’s a key, candidate key
• If not, then it’s a super key
P A B C D P→𝐴 𝑃𝐴 → 𝐴𝐵𝐶𝐷
p1 a1 b1 c1 d1 P→𝐵 𝑃𝐵 → 𝐴𝐵𝐶𝐷
p2 a1 b1 c1 d1 P→𝐶
P→𝐷
p3 a1 b2 c2 d1
P → 𝐴𝐵
p4 a2 b1 c3 d2 P → 𝐴𝐵C
p5 a1 b2 c2 d2 P → 𝐴𝐵𝐶𝐷
If XZ → 𝑌 then X → 𝑌 and Z → 𝑌
You can add attributes to LHS, but cannot remove anything from LHS
13
Closure of a set of FDs
• Suppose, we have a relation names Contracts
• Contracts(contractid, supplierid, taskid, deptid, partid, qty, value)
• Schema for Contracts: CSTDPQV
• This schema holds following ICs:
• The contractid C is a key: 𝐶 → 𝐶𝑆𝑇𝐷𝑃𝑄𝑉
• A task purchases a given part using a single contract: 𝑃𝑇 → 𝐶
• A department purchases at most one part from a supplier: 𝑆𝐷 → 𝑃
14
Closure of a set of FDs
• Suppose, we have a relation names Contracts
• Contracts(contractid, supplierid, taskid, deptid, partid, qty, value)
• Schema for Contracts: CSTDPQV
• This schema holds following ICs:
• The contractid C is a key: 𝐶 → 𝐶𝑆𝑇𝐷𝑃𝑄𝑉
• A task purchases a given part using a single contract: 𝑃𝑇 → 𝐶
• A department purchases at most one part from a supplier: 𝑆𝐷 → 𝑃
From 𝑃𝑇 → 𝐶𝑆𝑇𝐷𝑃𝑄𝑉 and 𝑆𝑇𝐷 → 𝑃𝑇, using transitivity we get 𝑆𝑇𝐷 → 𝐶𝑆𝑇𝐷𝑃𝑄𝑉
You can not conclude 𝑆𝐷 → 𝐶𝑆𝐷𝑃𝑄𝑉 by removing T from both sides
15
Attribute Closure
• Given a set of functional dependencies (FDs) in a relation, the total set
of FDs that can be inferred is called the closure of the FDs.
• Computing the closure can be very expensive
• Typically we just want to check if a given FD X→Y is in the closure
of a set of FDs
• Suppose we have the following FDs:
• 𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸}
• Is 𝐴 → 𝐸 in the closure 𝐹?
• If A’s closure contains E, then we can say 𝐴 → 𝐸
16
Attribute Closure
Algorithm to find closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸}
Is 𝐴 → 𝐸 in the closure 𝐹?
so we have to find A’s closure
17
Attribute Closure
Algorithm to find A’s closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸} Closure = {𝐴}
Is 𝐴 → 𝐸 in the closure 𝐹?
18
Attribute Closure
Algorithm to find A’s closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸} Closure = {𝐴}
Is 𝐴 → 𝐸 in the closure 𝐹? LHS of 𝐴 → 𝐵 (=A) is a subset of the current closure
we add RHS of 𝐴 → 𝐵 (=B) to closure set
Closure = {𝐴, 𝐵}
19
Attribute Closure
Algorithm to find A’s closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸} Closure = {𝐴}
Is 𝐴 → 𝐸 in the closure 𝐹? LHS of 𝐴 → 𝐵 (=A) is a subset of the current closure
we add RHS of 𝐴 → 𝐵 (=B) to closure set
Closure = {𝐴, 𝐵}
20
Attribute Closure
Algorithm to find A’s closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸} Closure = {𝐴, 𝐵}
Is 𝐴 → 𝐸 in the closure 𝐹? LHS of 𝐵 → 𝐶 (=B) is a subset of the current closure
we add RHS of 𝐵 → 𝐶 (=C) to closure set
Closure = {𝐴, 𝐵, 𝐶}
21
Attribute Closure
Algorithm to find A’s closure
𝐹 = {𝐴 → 𝐵, 𝐵 → 𝐶, 𝐶 → 𝐸} Closure = {𝐴, 𝐵, 𝐶}
Is 𝐴 → 𝐸 in the closure 𝐹? LHS of C→ 𝐸 (=C) is a subset of the current closure
we add RHS of 𝐶 → 𝐸 (=E) to closure set
Closure = {𝐴, 𝐵, 𝐶, 𝐸}
23
Exercise
• Consider a relation R with 5 attributes ABCDE. You are given the
following dependencies:
• 𝐴→𝐵
• 𝐵𝐶 → 𝐸
• 𝐸𝐷 → 𝐴
24