Relational Database Design
Relational Database Design
1 [email protected]
Relational Database Design
2 [email protected]
Pitfalls in Relational-Database Design
① Repetition of information
② Inability to represent certain information
3 [email protected]
Pitfalls in Relational-Database Design
4 [email protected]
Pitfalls in Relational-Database Design
5 [email protected]
Pitfalls in Relational-Database Design
Solution:
Introduce null values to handle updates through
views.(difficult)
6 [email protected]
Decomposition
7 [email protected]
Decomposition
:
Consider an alternative design in which Lending-schema is
decomposed into the following two schemas:
Branch-customer-schema=(branch-name, branch-city,
assets,customer-name)
Customer-loan-schema=(customer-name,loan-number,
amount)
8 [email protected]
Decomposition
Branch-customer B Customer-loan C
9 [email protected]
Decomposition
Find all branches that have loans with amount less than $1000.
branch- branch-city assets customer loan- amount
name -name number
Downtown Brooklyn 9000000 Jones L-17 1000
Downtown Brooklyn 9000000 Jones L-93 500
Redwood Palo Alto 2100000 Smith L-23 2000
Perryridge Horseneck 1700000 Hayes L-15 1500
Downtown Brooklyn 9000000 Jackson L-14 1500
Mianus Horseneck 400000 Jones L-17 1000
Mianus Horseneck 400000 Jones L-93 500
Round Hill Horseneck 8000000 Turner L-11 900
10 [email protected]
Decomposition
Problems:
Additional tuples:
(Downtown, Brooklyn, 9000000,Jones, L-93, 500)
(Mianus, Horsereck, 400000,Jones, L-17, 1000)
Consider the query, “Find all branches that have made
a loan in an amount less than $1000”.
From the un-decomposed table: Mianus Round Hill
∏branch-name(amount<1000(branch-customer
customer-loan))
Through join between decomposed table:
11 [email protected]
Mianus Round Hill Downtown
Lossy Decomposition
We have more tuples (Spurious tuples) in the joined tables
but actually have less information.
12 [email protected]
Lossless Decomposition
14 [email protected]
Functional Dependency
15 [email protected]
Trivial Dependency
16 [email protected]
Functional Dependency
17 [email protected]
An example
18 [email protected]
Closure of a Set
19 [email protected]
Closure of a set of attributes
20 [email protected]
Computation: Closure of a set of attributes
21 [email protected]
Attribute Closure: Uses
22 [email protected]
Closure: Findings
23 [email protected]
Irreducible Set of Dependencies
24 [email protected]
A typical example
25 [email protected]
A typical example
26 [email protected]
Lossless Join Decomposition
Lossless-join Decomposition:
Criterion for detecting lossy decomposition
② R1∩R2→R2
27 [email protected]
Example
28 [email protected]
Example: Decomposition
Branch-customer B Customer-loan C
29 [email protected]
Lossless Decomposition
30 [email protected]
Lossless Decomposition
Example:
Let us consider an alternative decomposition on the same
relation.
① Branch-schema=(branch-name,branch-city, assets)
Loan-info-schema=(branch-name,customer-
name,loan-number, amount)
Branch-schema∩Loan-info-schema={branch-name}
branch-name→{branch-name,branch-city,assets}
31 [email protected]
Lossless Decomposition
② Loan-info-schema
Loan-schema=(branch-name, loan-number, amount)
Borrower-schema=(customer-name, loan-number)
Loan-schema∩Borrower-schema={loan-number}
loan-number→{loan-number,branch-name,amount}
32 [email protected]
First Normal Form
33 [email protected]
Anomalies
35 [email protected]
Second Normal Form
37 [email protected]
Boyce-Codd Normal Form
38 [email protected]
BCNF…
39 [email protected]
BCNF
40 [email protected]
BCNF..
41 [email protected]
Dependency Preservation
42 [email protected]
Algorithm: Dependency Preservation
43 [email protected]
Dependency Preservation
44 [email protected]
Lossless Decomposition
Example:
Suppose a schema R=(A,B,C,D,E,F) is decomposed into R1
(A,B,E) and R2 = (CDEF). Is the decomposition is lossless? The
following set F of functional dependencies holds.
F={A→B,C→F,E→A,CE→D}
{ABE }∩{ CDEF}= {E}
E→A A→B E→B
E→A E→B E→AB E→ABE
It’s a lossless-join decomposition
45 [email protected]
Examples: BCNF
Examples:
① customer-schema=(customer-name,customer- street,
customer-city)
customer-name→{customer-street,customer-city}
customer-name is a candidate key
customer-schema is in BCNF
② branch-schema=(branch-name,assets,branch-city)
branch-name→ {assets,branch-city}
branch-schema is a candidate key
46 branch-schema is in BCNF
[email protected]
Examples: BCNF
③ loan-info-schema=(branch-name, customer-name,
loan-number, amount)
loan-number→{amount, branch-name}
loan-number is not a candidate key based on following facts
(Downtown,John Bell,L-44,1000)
(Downtown,Jane Bell,L-44,1000)
{Customer name, Roll no} is candidate key; thus each determinant
Is not considered as candidate keys.
Thus loan-info-schema is not in BCNF
47 [email protected]
Examples: BCNF
49 [email protected]
BCNF Decomposition
Algorithm:Input a rel. R; output: Ri s in BCNF
result:={R};
done:=false;
compute F+;
while (not done) do
if (there is a schema Ri in result that is not in BCNF)
then begin
let be a nontrivial functional dependency that
holds on Ri such that Ri is not in F+ ,and ∩ = ;
result:=(result-Ri) ∪(Ri- ) ∪( ,);
end
50 else done:=true;[email protected]
BCNF Decomposition
Example:
Lending-schema={branch-name,branch-city, assets,customer-
name,loan-number,amount}
branch-name→{branch-city,assets}
F=
loan-number→{amount,branch-name}
A candidate key for this schema is:
{loan-number,customer-name}
BCNF decomposition :
Each determinant (branch-name & loan-number) is not a
candidate key. Thus ,Lending-schema is not in BCNF, so
51 decompose it.
[email protected]
BCNF Decomposition
branch-name→{branch-city,assets}
{branch-name,branch-city, assets,customer-
name,loan-number,amount}{branch-name}+
① branch-name→ {branch-name,branch-city,
assets,customer-name,loan-number,amount} is not in F+
② branch-name∩{branch-city,assets}=
R1=branch-name∪{branch-city assets}
R2={branch-name,branch-city, assets,customer-
name,loan-number,amount}-{branch-city,assets}
52 [email protected]
BCNF Decomposition
Branch-schema={branch-name,branch-city, assets}
Loan-info-schema={branch-name,customer-name,loan-
number,amount}
loan-number→{amount,branch-name}
{branch-name,customer-name,loan-
number,amount}{loan-number}+
① branch-name→{branch-name,customer-name,loan-
number,amount} is not in F’+
② loan-number∩{amount,branch-name}=
Loan-schema={branch-name,loan-number,amount}
Borrower-schema={customer-name,loan-number}
Branch-schema and Borrower-schema are in BCNF.
Lending-schema=(branch-name,branch-city,
assets,customer-name,loan-number,amount)
Branch-schema={branch-name,branch-city, assets}
Loan-schema={branch-name,loan-number,amount}
Borrower-schema={customer-name,loan-number}
55 [email protected]
BCNF Decomposition: Algo
Algorithm:
Input: a schema R and R satisfied the FDs F.
Output: R is a lossless-join decomposition which
satisfies the FDs F and for each subschemas Ri is a
BCNF decomposition which satisfies Fi=∏Ri(F)
56 [email protected]
BCNF Decomposition: Algo
59 [email protected]
BCNF Decomposition
Example:
Relvar R=(O,I,S,Q,B,D) satisfies the following FDs.
F={S→D, I →B, IS →Q, B →O}
Give a lossless-join decomposition into BCNF of the schema R
L: S, I R: D, Q, O LR: B
(SI)+=SIDBQO=U have only one candidate key.
S→D ISS not in BCNF
S→D D-S≠ S do not include IS
Decompose: R1=SD R2=OISQB
60 [email protected]
BCNF Decomposition
Example:
Relvar R=(A,B,C,D) satisfies the following FDs.
F={A→D, C→D, D →B}
Give a lossless-join decomposition into BCNF of the schema R
L: A,C R: B LR: D
(AC)+=ACDB=U have only one candidate key.
Decompose: R1=AD R2=ACB
FR2={C→B, A →B}
……
62 [email protected]
BCNF Decomposition
Every BCNF decomposition is not dependency preserving.
Example:
Banker-schema={branch-name,customer-name,banker-name}
banker-name→{branch-name}
F=
{branch-name,customer-name}→banker-name
banker-name is not a key for Banker-schema.
Thus, Banker-schema is not in BCNF. Decompose it.
63 [email protected]
BCNF Decomposition
Banker-branch-schema={banker-name,branch-name}
Customer-banker-schema={customer-name,banker-name}
F1={banker-name→branch-name}
F2=
{branch-name,customer-name}→banker-name in F+
{branch-name,customer-name}→banker-name not in
{F1∪F2}+
{F1∪F2}+<>F+ and the decomposition is not dependency
preserving
64 [email protected]
BCNF Decomposition
65 [email protected]
3NF Decomposition
Example:
Banker-schema={branch-name,customer-name,banker-name}
banker-name→ {branch-name}
F=
{branch-name,customer-name}→banker-name
banker-name is not a key for Banker-schema.
Thus, Banker-schema is not in BCNF.
{branch-name,customer-name} is a candidate key
for Banker-schema. Banker-schema is in 3NF.
66 [email protected]
3NF Decomposition
67 [email protected]
3NF Decomposition
Algorithm:
let Fc be a canonical cover for F’;
i:=0;
for each functional dependency in Fc do
if none of the schemas Rj, j=1,2……,i contains
then begin
i:=i+1;
Ri:= ;
end
If none of the schemas Rj, j=1,2……,i contains a candidate key for R
then begin
i:=i+1;
Ri:= any candidate key for R ;
end
68 Return(R1,R2,……,Ri) [email protected]
3NF Decomposition
Example:
Relvar R=(O,I,S,Q,B,D) satisfies the following FDs.
Example:
Banker-info-schema={branch-name,customer-name,banker-
name,office-number}
Banker-name→{branch-name,office-number}
{customer-name,branch-name}→banker-name
3NF decomposition:
R1= banker-name∪{branch-name,office-number}
R2= {customer-name,branch-name}∪banker-name
70 [email protected]
3NF decomposition
Banker-office-schema={banker-name,branch-name,office-number}
Banker-schema={customer-name,branch-name,banker-name}
Banker-info-schema={branch-name,customer-name,banker-
name,office-number}
Banker-office-schema={banker-name,branch-name,office-number}
Banker-schema={customer-name,branch-name,banker-name}
71 [email protected]
Comparison: 3 NF & BCNF
Advantage of 3NF:
It is always possible to obtain a 3NF design without sacrificing a
lossless join or dependency preservation.
Disadvantage of 3NF:
If we do not eliminate all transitive dependencies, we may have to
use null values to represent some of the possible meaningful
relationships among data items, and there is the problem of
repetition of information.
72 [email protected]
Higher Order Normal Form
73 [email protected]
Converted Table: CTB
Course TEACHER TEXT
74 [email protected]
Higher order Normal Form
75 [email protected]
Multi Valued Dependency
76 [email protected]
Multi Valued Dependency
77 [email protected]
MVD and 4 NF
78 [email protected]
Exercise: