Story so Far…..
Intro to Databases and Data Management Systems
Conceptual modeling using ER
Relational Model
Mapping ER → Relations
Preview of SQL to support database creation
NEXT: More general forms of constraints and a bit more about relational design, and then relational algebra enroute to learning SQL query language!
1
More Constraints in the Relational model …
2
2
Functional Dependencies
(A1,...,An → B1,...,Bm) iff
for any two tuples r1 and r2 in R,
r1(A1,...,An ) = r2(A1,...,An ) implies r1(B1,...,Bm) = r2(B1,...,Bm)
3
3
Example
4
4
Take(StudentID, CID, Semester, Grade)
FD: (StudentId,Cid,semester) → Grade
What if FD: (StudentId, Cid) → Semester?
Illegal
“Each student can take a course only once.”
Types of Functional Dependencies
Example: AB→A
Examples: AB→AC
We can always change a set of FDs to an equivalent set that are completely non-trivial.
5
functional dependencies can help determine if relational representation stores data redundantly!
Good Design Principle: Avoid redundancy
Why?
Wastage of Storage
Anomalies during modifications
Increased programming benefits to ensure consistency
6
FD can lead to Redundancy
CS 22 Projects | student | project | data |
| Boris | Document Stores | 11/16/2021 |
| Stefan | Document Stores | 11/16/2021 |
| Monica | Vector Databases | 11/21/2021 |
Redundancy in turn leads to Anomalies
Error in
Updating. Forgot to update all entries
CS 22 Projects | student | project | date |
| Boris | Document Stores | 11/16/2021 |
| Stefan | Document Stores | 11/18/2021 |
| Monica | Vector Databases | 11/21/2021 |
More Anomalies...
CS 22 Projects | student | project | date |
| Boris | Document Stores | 11/16/2021 |
| Stefan | Document Stores | 11/16/2021 |
| Monica | Vector Databases | 11/21/2021 |
�More Anomalies ...
CS 22 Projects | student | project | data |
| Boris | Document Stores | 11/16/2021 |
Stefan | Document Stores | 11/16/2021 | |
Monica | Vector Databases | 11/21/2021 |
Redundancy and FDs
CS 22 Projects | student | project | data |
| Boris | Document Stores | 11/16/2021 |
Stefan | Document Stores | 11/16/2021 | |
Monica | Vector Databases | 11/21/2021 |
When does a relation contain no redundancy due to FDs?
Assume functional dependency: X → Y
Since t1[X] = t2[X], we have that t1[Y] = t2[Y] (potential redundancy!)
However, if X is a superkey, then t1[Z] = t2[Z], thus, z2 == z1
Since x1 = x2, y1 = y2 and z1 = z2 , then , t2 == t1
Since R is a set (no duplicate tuples), so such a t2 cannot exist!
Thus, R does not contain redundancy if for each FD X → Y, when X is a superkey.
Such a relational scheme is said to be boyce-codd normal form
z1
X
Boyce Codd Normal Form
Let R be a relation scheme. F be the functional dependency set. R is in BCNF if for all functional dependencies X → Y in F, either
Y is a subset of X (i.e., trivial dependency), or
X is a superkey.
X is a superkey for R if X → A1, A2, ..., An , where A1,A2, ....An is the set of attributes of R.
Example (BCNF Violation)
project is not superkey!
CS 22 Projects | student | project | data |
| Boris | Document Stores | 11/16/2021 |
| Stefan | Document Stores | 11/16/2021 |
| Monica | Vector Databases | 11/21/2021 |
Example (BCNF Violation)
Now this is in BCNF since Student is a superkey
CS 22 Projects | student | project | data |
| Boris | Document Stores | 11/16/2021 |
| Stefan | Document Stores | 11/18/2021 |
| Monica | Vector Databases | 11/21/2021 |
Checking for BCNF
Given a relation and corresponding functional dependencies, how do we check if it is in BCNF or not?
To do so, for each functional dependency X → Y which is non-trivial, we need to check of X is a superkey.
To check if X is superkey…..
we need to probe a bit deeper into functional dependency theory….
16
Reasoning about FD’s
Suppose fd3 does not hold.
So there are tuples r1, r2 in R: r1[A] = r2[A], but r1[C] <> r2[C]
Because of fd1 and r1[A] = r2[A]: r1[B] = r2[B]
Because of fd2 and r1[B] = r2[B]: r1[C] = r2[C]
Contradiction! So fd3 must hold!
17
17
Thus, given a set of FDs we can derive additional FDs!!
Armstrong’s Axioms: Inferring Additional FDs
18
18
Importance of Armstrong’s Axioms (AAs)
19
19
More Rules Derived from AAs
20
20
Closure of FD Set
21
21
Can we compute F+ -- that is the set of all functional dependencies implied by F?
Using Armstrong’s Axioms to Derive F+
i.e., as long as they keep generating additional FDs.
We can redefine F+ as all the FDs that follow from F using AAs.
22
22
How big can F+ be?
23
23
Back to our problem of checking if a relation is in BCNF…..
Checking for BCNF
Given a relation and corresponding functional dependencies, how do we check if it is in BCNF or not?
To do so, for each functional dependency X → Y which is non-trivial, we need to check of X is a superkey.
But how do check if X is superkey…..
to learn how to do so, we need to probe a bit deeper into functional dependency theory….
24
Testing if X is a superkey
Testing if X → Y is in F+ �
First compute F+ from F using Armstrong Axioms.
Not very practical since the size of F+ can be really very large.
In general, computing F+ might take exponential time!
Membership of F+�
Closure of attribute set:
Membership of F+�
Since X →Y holds over R if and only if Y is a subset of X+,
we can check if X →Y holds by computing X+ and testing if Y is a subset of X+.
Computing X+
X+ = X
repeat
oldX+ = X+
for each fd Y→ Z in F do
if (Y is a subset of oldX+) then
X+ = X+ union Z
until (oldX+ == X+)
maximum number of iterations = cardinality of F times the number of attributes in R!
(polytime)
Example�
Let the set F contain the following fds:
AB → C, D →EG, C→A, BE→C, BC →D CG →BD, ACD→ B, CE→AG
Let X = BD. Compute X+.
iteration 1: X+ = {BD}
iteration 2: X+ = {BDEG} (due to dependency 2)
iteration 3: X+ = {BDCEG} (due to dependency 4)
iteration 4: X+ = {BCDEGA} (due to dependency 8)
iteration 5: X+ = {BCDEGA}
Algorithm exits the loop since no new attribute added in last iteration and (BD)+ = {ABCDEG}
BCNF Examples
Is the following relation in BCNF.
To check, for each FD, we determine if the left hand side is a key.
Is A a key?
Execute the algorithm to determine {A}+
Step 1: {A}+ = {A}
Step 2: {A}+ = {A,B}
Step 3 : {A}+ = {A,B,C}
Step 4: {A}+ = {A,B,C,D}
Thus, A → ABCD Hence, the constraint A→B does not violate BCNF.
What about the other FDs -- B→C, C→D, D→A? check to make sure that they also do not violate BCNF
Another Example
Interesting Aside
So far …
We have just learnt how to check if a relation contains redundancy.
But what do we do if indeed a relation contains redundancy?....
33
Schema Normalization
Lossless Joins
r is a subset of r1 r2
hence the join is lossy!
Testing for Lossless Decomposition
Let R be a relation with the set of functional dependencies F.
Let R1 and R2 be a decomposition of R.
Is the decomposition lossless?
To check if the decomposition is lossless, check if either of the following holds:
What if R is decomposed into more than 2 subrelations.
Consider the decomposition as a sequence of binary decompositions and test for losslessness at each step.
(this may not always work)
Read the more general algorithm in the Book!
Schema Normalization
Given a relation R which is not in BCNF, we want to decompose it in such a way that the subschemes do not contain redundancy and the decomposition is lossless.
this will take us deeper into relational design theory --- we will do so towards the end if we have time.
37