Relational Database Design
Relational Database Design
Design
customer = ((customer_id,
_ customer_name,
_ customer_street,
_
customer_city)
z Repetition of Information.
Design Goals:
Redundancy:
z Data for branch-name, branch-city, assets are repeated for each loan that a
branch makes
z Wastes space
z Complicates updating, introducing possibility of inconsistency of assets value
Null values
z Cannot store information about a branch if no loans exist
z Can use null values, but they are difficult to handle.
Goal — Devise a Theory for the Following
z each
h relation
l ti iis iin good
d fform
Loan-info-schema
Loan info schema = (customer
(customer-name
name, loan-number
loan number,
branch-name, amount)
Cust_name L_no Amount
Ajay L 21 21000
Suresh L 23 26500
Suresh L 43 2300
Aj
Ajay L 100 74500
Rakshita L 45 100000
Decomposition
Sometimes it is required to reconstruct loan relation from the Branch-schema
Branch schema
and Loan-info-schema: so we can do this by
Branch-schema
Branch schema Loan-info-schema
Loan info schema
In the last example we are not able to identify which customers are
join decomposition.
decomposition
R = R1 ∪ R2 ∪ R3 ..............∪ Rn
Lossless-join decomposition.
3. If we decompose it into
Branch--schema = (B_name
Branch (B_name,, assets, B_city
B_city))
Loan--info
Loan info--schema = (B_name
(B_name,, L_no
L_no,, amount)
Borrow--schema = (cust
Borrow ((cust_name
cust
cust_name,
name, L_no)
L no)
L_no
no)
we claim this decomposition has several desirable properties.
Desirable Properties of Decomposition
a) Lossless Decomposition
b) Dependency Preservation
c) Repetition of information
Desirable Properties of Decomposition
a) Lossless Decomposition
The decomposition
p is a lossless-join
j decomposition
p of R if at least
( ) R1 ∩ R2 → R1
(a)
(b) R1 ∩ R2 → R2
Example
a) Lossless Decomposition
R = (A, B, C)
F = {{A → B, B → C))
z Can be decomposed in two different ways
R1 = (A, B), R2 = (B, C)
z Lossless-join decomposition:
R1 ∩ R2 = {B} and B → BC
z Dependency preserving
R1 = (A, B), R2 = (A, C)
z Lossless-join decomposition:
R1 ∩ R2 = {A} and A → AB
z Not dependency preserving
(cannot check B → C without computing R1 R2)
Desirable Properties of Decomposition
a) Lossless Decomposition
Example:
Example Continue:
As L_no
L no is the common attribute,
attribute and
L_no → L_no amount B_name
F’ = F1 U F2 U . . . U Fn
Branch--schema = ((B_name
Branch B_name,, assets, B_city
B_city))
Loan--info
Loan info--schema = ((B_name
B_name,, L_no,
L_no, amount)
B
Borrow
Borrow--schema
h = (cust_name
(cust_name,
t , L_no
L
L_no)
)
problem.
bl
We will see how this may be achieved through the use of normal
forms.
Functional dependency
Course_Pref_Table
Course Pref
Dept Prof
Course Course dept
Course_dept
101 CS
Rajiv 102 CS
103 EC
CE 101 CS
102 CS
Mahesh
103 EC
104 EC
101 CS
CL Ruchika 103 EC
106 EE
103 EC
104 EC
IT Rajesh 106 EE
102 CS
105 EE
First Normal Form Example
Course_Pref_Table
Course Pref Table
Dept Prof Course Course_dept
CE Rajiv 101 CS
CE Rajiv 102 CS
CE Rajiv 103 EC
CE Mahesh 101 CS
CE Mahesh 102 CS
CE Mahesh 103 EC
CE Mahesh 104 EC
CL Ruchika 101 CS
CL Ruchika 103 EC
CL Ruchika 106 EE
IT Rajesh 103 EC
IT Rajesh 104 EC
IT Rajesh 106 EE
IT Rajesh 102 CS
IT Rajesh 105 EE
Second normal form: 2NF
Order_table
OrderNo Customer Total
1 Acme Widgets $134.23
OrderNo Customer Æ Total
2 ABC Corporation $521.24
4 A
Acme Wid
Widgets $928 3
$928.53
Boyce--Codd Normal Form
Boyce
A relation schema R is in BCNF with respect to a set F of functional dependencies if
for all functional dependencies in F+ of the form
α→β
where α ⊆ R and β ⊆ R, at least one of the following holds:
α → β is trivial (i.e., β ⊆ α)
α is a superkey for R
violation of BCNF.
We decompose R into:
• (α U β )
• (R-(β-α))
In our example,
z α = loan_number
z β = amount
z ( R - ( β - α ) ) = ( customer_id, loan_number )
Decomposing a Schema into BCNF
Lending-schema = (B_name, assets, B_city, L_no, cust_name,amount)
B_name Æ assets B_city (not trivial and B_name is not a super key)
L_no Æ amount B_name (not trivial and L_no is not a super key)
Candidate key for this Schema is { L_no, cust_name}. This Schema is not in
BCNF form. So decompose this schema into below given two schemas
Branch--schema = (B_name,
Branch B_name, B_city,
B_city, assets)
Loan--info
Loan info--schema = (B_name,
B_name, cust_name,
cust_name, L_no,
L_no, amount)
L_no Æ amount B_name (not trivial and L_no is not a super key)
This Schema is not in BCNF form. So decompose this schema into below
given two schemas
Loan--schema = ((B_name
Loan B_name,, L_no,
L_no, amount)
Borrow--schema = ((cust_name
Borrow cust_name,, L_no)
L_no)
Decomposition of Lending--schema
Lending to all these three schema
Branch--schema
Branch schema, Loan
Loan--schema and Borrow
Borrow--schema having dependency
preservation and lossless decomposition.
BCNF and Dependency Loss…Example
banker
banker--schema = ( branch-
branch-name, customer
customer--name, banker
banker--name)
banker--name Æ branch
banker branch--name
branch--name customer-
branch customer-name Æ banker
banker--name
banker--branch
banker branch--schema = (banker
(banker--name, branch
branch--name)
customer--banker
customer banker--schema = (customer
(customer--name, banker
banker--name)
banker--name Æ branch
banker branch--name
z Eith test
Either t t Ri for
f BCNF with
ith respectt to th restriction
t the t i ti off F (i.e.
(i Fi) to
t Ri (that
(th t
α → β in F+
at least one of the following holds:
z α → β is trivial (i
(i..e., β ∈ α)
z α is a superkey for R
Note that 3NF is concerned with transitive dependencies which do not involve
candidate
did t keys.
k A 3NF relation
l ti with
ith more than
th one candidate
did t key
k will
ill clearly
l l
p y α → β, if α is a superkey.
Use attribute closure to check for each dependency p y
candidate key of R
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, 1 ≤ j ≤ i contains α β
then begin
i := i + 1;
1
Ri := α β
end
if none of the schemas Rj, 1 ≤ j ≤ i contains a candidate key for R
then begin
i := i + 1;
Ri := any candidate key for R;
end
return (R1, R2, ..., Ri)
3NF Decomposition Algorithm (Cont.)
If A B, then we cannot have two tuples with the same A value but different B
values.
relation.
α →→ β
Tabular representation of α →→ β
MVD (Cont.)
Trivial MVD
1. To test relations to determine whether they are legal under a given set of
ourselves only with relations that satisfy a given set of functional and
multivalued dependencies.
Demerits of Normalization
Data
D t retrieval
ti l or SELECT operation
ti performance
f will
ill be
b severely
l affected.
ff t d