DatabaseDesign 2024
DatabaseDesign 2024
2024
Strong Entity
Weak entity and Identifying Relationship
Weak Entity connected Weak Entity
Update Methodology
Mapping Weak Entity to Rational Database
2.6 Attributes of Relationship
2.7 Change M:N Relationship
2.8 Recursive Relationship
Structural Constraints
Multiple Relationship
2.9 Mapping Recursive Relationship
2.10 Derived or Redundant Relationship
2.11 Enhanced EntityRelationship (EER)
Generalization & Specialization
Subclasses of Subclasses
Mapping rules
2.12 Union Types
Mapping rules
3. Logical Design
3.1 Functional Dependencies
Definition
Notation
Satisfaction
Integrity Constraint
Inference
3.2 Amstrong’s Axioms
Amstrong’s Axioms
Lemma
Proof
3.3 Closure
Theorem
Preserve Dependencise
Testing Preserve Dependencies
Decomposition Algorithm
Synthesis Algorithm
4. Physical Design
4.1 Overview
4.2 Conceptual to Physical
Problem
ERD
Relational Model
Normal Form
Normalization
Customize Name
Foreign Keys
SQL Script
4.3 Denormalization
4.4 Customized data views
4.5 Indexes
• One-to-One (1:1) one entity is associated with one other entity and vice versa
• Many-to-One (M:1) many entity are associated with one another entity
• One-to-Many (1:M)
• Many-to-Many (M:N)
• Participation (Full/Partial)
Full (doube lines): All entity associated with another entity
Partial (single line): Not every entity associated with another entity
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Constraints of relationships 15/175
Pattern 1
1(full):1 Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 16/175
Pattern 1
M(full):1 Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 17/175
Pattern 2
1(partial):1 Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 18/175
Pattern 2
M(partial):1 Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 19/175
Pattern 3
1(full):M Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 20/175
Pattern 3
M(full):N Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 21/175
Pattern 4
1(partial):M Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 22/175
Pattern 4
M(partial):N Relationship
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Patterns and Relationships 23/175
Summary of the notation for ER diagrams
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Summary of the notation 24/175
Summary of the notation for ER diagrams
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Summary of the notation 25/175
COMPANY database - Using Tool to draw ERD
Nguyễn Văn Diêu 2. Conceptual Design :: ER Conceptual Model :: Database Example 26/175
ER Design Methodology
• Step 1. Select one primary entity from the database requirements description;
Show attributes.
• Step 2. Find out if information about attributes refer to another thing.
• Step 3. If another entity is appropriate, draw the second entity. Repeat step 2.
• Step 4. Connect entities with relationships if relationships exist.
• Step 5. Define Cardinality ratio of relationship.
• Step 6. Show some sample data.
Consider a model for a simplified airport where PASSENGERS and FLIGHTS are to
be recorded. Suppose the attributes of PASSENGERS are name, luggage_pieces
and frequent_flier_no. Suppose the attributes for FLIGHT are flight_no,
destination, arrive_time and depart_time. Draw the ER diagram.
ER Design Methodology:
Mapping rule 1: Mapping strong entities to a relation with attibutes and the primary
key. If more than one candidate key is indicated on the ER diagram, choose one for the
primary key.
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Entity 32/175
Mapping Entity to Relation
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Entity 33/175
Mapping Relationship to Relation
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Relationship 34/175
Mapping Relationship to Relation
Mapping rule 5: Mapping a binary 1:1 relationship when one side of the relationship
has full participation and the other has partial participation.
AUTOMOBILE(vehicle_id, make,
body_style, color, year,
student_number)
STUDENT(student_number,
name.first, name.mid,
name.last, address)
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Relationship 35/175
Mapping Relationship to Relation
Mapping rule 6: Mapping a binary 1:1 relationship when both sides have partial
participation constraints.
AUTOMOBILE( vehicle_id, make,
body_style, color, year)
STUDENT(student_number,
name.first, name.mid,
name.last, address, vehicle_id)
———
AUTOMOBILE( vehicle_id, make, body_style, color,
year)
STUDENT(student_number, name.first, name.mid,
name.last, address)
STUDENT_AUTOMOBILE(student_number, vehicle_id)
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Relationship 36/175
Mapping Relationship to Relation
Mapping rule 7: Mapping a binary 1:1 relationship when both sides have full
participation constraints. Using M:N
Mapping rule 8: Mapping binary 1:N relationships when the N side has full
participation. Include the key of the entity on the 1 side of the relationship as a foreign
key on the N side. Using rule 5.
Nguyễn Văn Diêu 2. Conceptual Design :: Mapping to Relational Database :: Mapping Relationship 37/175
Strong Entity
e.g.
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Strong Entity 38/175
Strong Entity
Sample data:
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Strong Entity 39/175
Weak Entity
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Weak entity and Identifying Relationship 40/175
Weak Entity
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Weak entity and Identifying Relationship 41/175
Weak Entity - Weak Entity
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Weak Entity connected Weak Entity 42/175
Mapping Weak Entity to Relational DB
EMPLOYEE(employee_id, fname,
minit, lname)
DEPENDENT(employee_id, dname,
insurance, birth_date)
HOBBY(employee_id, dname, type,
year_involved)
Nguyễn Văn Diêu 2. Conceptual Design :: Weak Entity :: Mapping Weak Entity to Rational Database 43/175
Attributes of Relationship
Change to:
• Intersection Weak Entity:
STUDENT-COURSE
• Attribute: grade
• Key: student_ID +
course_ID
• Relationship 1: Rel1
• Relationship 2: Rel2
e.g.
Nguyễn Văn Diêu 2. Conceptual Design :: Recursive Relationship :: Structural Constraints 47/175
One to Many Recursive Relationship
e.g.
Nguyễn Văn Diêu 2. Conceptual Design :: Recursive Relationship :: Structural Constraints 48/175
Many to Many Recursive Relationship
e.g.
Nguyễn Văn Diêu 2. Conceptual Design :: Recursive Relationship :: Structural Constraints 49/175
Multiple Relationship
e.g.
Nguyễn Văn Diêu 2. Conceptual Design :: Recursive Relationship :: Multiple Relationship 50/175
Mapping Recursive Relationship
Mapping Rule 10: Mapping 1:N recursive relationships. Re-include the primary key of
the table with the recursive relationship in the same table, giving it some other role
name.
e.g.
PERSONNELL(emplye_id, fname, mname, lname, super_id)
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Generalization & Specialization 53/175
Generalization or Specialization
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Generalization & Specialization 54/175
Generalization or Specialization
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Generalization & Specialization 55/175
Subclasses of Subclasses
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Subclasses of Subclasses 56/175
Subclasses of Subclasses
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Subclasses of Subclasses 57/175
Mapping rules to Relation
Nguyễn Văn Diêu 2. Conceptual Design :: Enhanced EntityRelationship (EER) :: Mapping rules 58/175
Union Types
Nguyễn Văn Diêu 2. Conceptual Design :: Union Types :: Mapping rules 64/175
Functional Dependencies Definition
Let:
• R(A1 , · · ·, An ) be a relational scheme,
• X and Y be subsets of {A1 , · · ·, An }
We say:
• X → Y, read ”X functionally determines Y” or ”Y functionally depends on X” if:
∀(t1 , t2 ) ∈ ∀r(R) :
Not possible: t1 (X) = t2 (X) but t1 (Y) ̸= t2 (Y)
• In the FD X → Y, X is called the Left side and Y called the Right side.
StudentID → StudentName
SubjectID → SubjectName
StudentID, SubjectID → Grades
• r satisfies FD X → Y
if ∀(t1 , t2 ) ∈ r(R) : t1 (X) = t2 (X) then t1 (Y) = t2 (Y)
• ”if ... then” statement, it can be satisfied either by:
1. t1 (X) ̸= t2 (X) or
2. t1 (Y) = t2 (Y)
• If r does not satisfy X → Y, the r violate X → Y.
• If we declared X → Y hold for R, then all instance r of scheme R will satisfy
X → Y.
• FD X → ∅ is trivially satisfied by any relation.
• FD ∅ → Y is satisfied by those relations in which every tuple has the same
Y − value.
Nguyễn Văn Diêu 3. Logical Design :: Functional Dependencies :: Integrity Constraint 69/175
Inference FD
Let R; A, B, C : attributes.
FD: A → B and B → C are known to hold in R.
We claim that A → C must also hold in R
Proof by Contradiction
Suppose r satisfies A → B and B → C,
but ∃(t1 , t2 ) ∈ r : t1 .A = t2 .A and t1 .C ̸= t2 .C
• if t1 .B ̸= t2 .B, then r violate A → B
• if t1 .B = t2 .B but t1 .C ̸= t2 .C then r violate B → C
Hence r must satisfy A → C ■
Direct Proof
∀(t1 , t2 ) ∈ ∀r(R) : t1 .A = t2 .A =⇒ t1 .B = t2 .B (A → B)
=⇒ t1 .C = t2 .C (B → C)
from FD definition =⇒ A → C ■
Nguyễn Văn Diêu 3. Logical Design :: Functional Dependencies :: Inference 70/175
Logically Inference
Nguyễn Văn Diêu 3. Logical Design :: Amstrong’s Axioms :: Amstrong’s Axioms 72/175
e.g.
Nguyễn Văn Diêu 3. Logical Design :: Amstrong’s Axioms :: Amstrong’s Axioms 73/175
Lemma
There are several other inference rules that follow from Amstrong’s Axioms
Lemma:
{ }
1. Union. X → Y, X → Z |= X → YZ
{ }
2. Pseudotransitivity. X → Y, WY → Z |= WX → Z
{ }
3. Decomposition. X → Y, Z ⊆ Y |= X → Z
Proof:
Union. Given X → Y |= X → XY (augment)
Given X → Z |= XY → YZ (augment)
{ }
By transitivity X → XY, XY → YZ |= X → YZ
Pseudotransitivity. Given X → Y |= WX → WY (augment)
Since given WY → Z |= WX → Z (transitivity)
Decomposition. Given Z ⊆ Y |= Y → Z (reflexivity)
Given X → Y, by transitivity, |= X → Z ■
Nguyễn Văn Diêu 3. Logical Design :: Closure :: Closure of FDs Set 78/175
e.g.
{ }
Let R(ABC) and F = A → B, B → C . Then F+ consists of all those dependencies
X → Y such that either
1. X contains A e.g., ABC → AB, AB → BC, or A → C,
2. X contains B but not A, and Y does not contain A e.g., BC → B, B → C, or
B → ∅, and
3. X → Y is one of the three dependencies C → C, C → ∅, or ∅ → ∅.
Nguyễn Văn Diêu 3. Logical Design :: Closure :: Closure of FDs Set 79/175
Closure of Attribute Set
Let:
• R be an attributes set.
• F be a functional dependencies set hold on R.
• X be a subset of R.
We define X+
F is the closure of X (with respect of F) is the set of attributes A such
that X → A can be logically implied from
{ F (by FD definition).
}
X+F = A | F |= X → A
e.g. { }
R(ABCD) , F = B → D, A → B, C → B
A+
F = ABD
Nguyễn Văn Diêu 3. Logical Design :: Closure :: Closure of Attribute Set 80/175
Algorithm
• X → X+F
• Z ⊆ X+
F , then X → Z
Proof: X → Z:
Suppose ∃(t1 , t2 ) : t1 .X = t2 .X, but t1 .Z ̸= t2 .Z
So ∃ A ∈ Z : t1 .A ̸= t2 .A. That is contradiction with closure set definition, because of
A ∈ Z ∈ X+
F ■
Similarly with X → X+
F ■
Let X → Y and F
Determine F |= X → Y ? We have three methods:
1. Using FD definition
2. Using Amstrong’s axioms
3. Test if X → Y ∈ F+
In the 3th method, F+ can be very lager
How to show X → Y ∈ F+ without generating all of F+
Algorithm:
1. X+
F
2. if Y ⊆ X+
F then F |= X → Y
if Y ⊈ XF then F |=
+
X → Y
Definition:
• Two FDs F and G over R are equivalent if F+ = G+
• Denote F ≡ G
• If F ≡ G, then F is a cover for G
Test whether F and G are equivalent:
• Step 1: F |= G
− Test every g : X → Y ∈ G is g ∈ F+
− g : X → Y ∈ G, compute X+
F
− If Y ⊆ X+
F , then g ∈ F , if not: g ∈
+ / F+
− If ∃ g ∈ G, ∈
/ F+ , then surely F+ ̸= G+
• Step 2: G |= F
Analogous manner Step 1:, test every f ∈ F is f ∈ G+
Nguyễn Văn Diêu 3. Logical Design :: Closure :: Covers 86/175
e.g.
{ }
F = { A → BC, A → D, CD → E }
G = A → BCE, A → ABD, CD → E
F≡G?
Step 1: F |= G.
A → BCE: A+ F = ABCDE ⇒ F |= A → BCE
A → ABD: A+ F = ABCDE ⇒ F |= A → ABD
CD → E: CD+ F = CDE ⇒ F |= CD → E
Step 2: G |= F.
A → BC: A+ G = ABCED ⇒ G |= A → BC
A → D: AG = ABCED ⇒ G |= A → D
+
CD → E: CD+ G = CDE ⇒ G |= CD → E
F≡G■
Definition
• F have many cover sets, its equivalent to F
• We define a set Fc is a minimal cover of F if:
R, F ; R1 : Attributes Set.
F1 = projected FDs of F on R1 ,
denote F1 = π R1 (F):
• F1 follow from F and
• Involve only attributes of R1 .
Input: R , F.
Output: All keys of R.
Method: Computing a sequence steps:
1. Generate all subsets of R, not ∅ set. Sorting it by ascending.
2. Computing closure those sets.
3. Only keep the sets, such that its closure equal R.
4. Remove the sets, it contains any other sets in list.
5. All sets remain are the all keys of relation scheme R.
Input: Q , F.
Output: All keys on Q.
Method: Computing a sequence steps:
1. L : attributes in the left side of FDs
R : attributes in the right side of FDs
NF = Q − (L + R) : attributes do not in FDs
OL = L − R : attributes only in left side of FDs
LR = L ∩ R : att. both in the left side and right side of FDs
2. Generate all subsets of LR including ∅.
Sorting it by ascending.
3. Add NF and OL into subsets above.
4. Computing closure for all subsets.
Decide the keys like general algorithm.
R , F,
K1 , K2 , · · · Kn : keys of R, then we have FDs:
K1 → R, or K1 → (R − K1 )
···
Kn → R, or Kn → (R − Kn )
e.g. { }
R(ABCDE) , F = AB → CD, D → E, BC → A
Two keys: K1 = AB, K2 = BC
Then, there are FDs logically implied by keys:
AB → CDE
BC → ADE
• Base on Keys checking system of DBMS, we have a concept to check all FDs on
relation.
• This concept is call Normal Form.
Relation Gender, shown below, is not in 1NF because it contains values that are sets
of atomic values.
Gender(Name, Sex)
⋉GateAssign
PilotAssign⋊
• Given R , F;
X, Y ⊆ R
• Y is called fully dependent upon X if:
1. X → Y ∈ F+
′ ′
2. ∄ X ⊊ X : X → Y ∈ F+
{ }
e.g. F = Flight, Day → Pilot, Gate; Flight → Gate
Gate is not fully dependent upon {Flight, Day}.
Pilot is fully dependent upon {Flight, Day}.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: Fully dependent 111/175
Prime and nonprime attributes
• Given R , F
• A is an attribute in R
• A is prime in R with respect to F if A is contained in some key of R.
• Otherwise A is nonprime in R.
e.g. Assign(Flight, Day, Pilot, Gate)
with F = {Flight, Day → Pilot; Flight → Gate}
Key: {Flight, Day}
Prime: Flight and Day.
Nonprime: Pilot and Gate.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: Prime and nonprime attributes 112/175
Second normal form (2NF)
• R,F
• X ⊆ R, A ∈ R
• A : transitively dependent upon X in R if there is a Y ⊆ R with:
• X → Y ∈ F+
• Y ↛ X ∈ F+
• Y → A ∈ F+
• A∈/ XY
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: Transitively Dependent 115/175
e.g.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: Transitively Dependent 116/175
Third Normal Form (3NF)
Classic Definition
R is in third normal form (3NF) if:
• It is in 1NF, and
• no nonprime attribute in R is transitively dependent upon a key of R.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: 3NF Classic 117/175
e.g.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: 3NF Classic 118/175
Third Normal Form (3NF)
Modern Definition
R is in third normal form (3NF) if:
• It is in 1NF, and
• Whenever X → Y is a nontrivial FD in F+ , either
• X is a superkey, or
• Each B ∈ Y is a member of some key of R
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: 3NF Modern 119/175
Lemma 3NF ⇒ 2NF
R is not in 2NF ⇒ exists non-prime attribute A is not fully dependent upon key
′
K ⊆ R. So that ∃ K ⊊ K:
′
• K → A ∈ F+ and because of
′
• K ↛ K ∈ F+
′
• A∈/ K=K ∪K
Therefore A is transitively dependent upon key K, so that R is not in 3NF. ■
Classic Definition
R : Boyce-Codd normal form (BCNF) if:
• It is in 1NF, and
• No attribute in R is transitively dependent upon a key of R.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: BCNF Classic 121/175
Boyce-Codd Normal Form (BCNF)
Modern Definition
R : Boyce-Codd normal form (BCNF) if:
• It is in 1NF, and
• for every Y ⊆ R and for every attribute A ∈ (R − Y)
• if Y → A, then Y → R
• That is, if Y non-trivially determines any attribute of R, then Y is a superkey for R.
Nguyễn Văn Diêu 3. Logical Design :: Normal Form :: BCNF Modern 122/175
Overview
r = π R1 (r) ⋊
⋉ π R2 (r) ⋊
⋉ ··· ⋊
⋉ π Rk (r)
In general:
r ⊆ π R1 (r) ⋊
⋉ π R2 (r) ⋊
⋉ ··· ⋊
⋉ π Rk (r)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Preserve Information 125/175
Tableau Test for Preserve Information
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 127/175
e.g.
{ }
R(ABCD) , F = {A → B, B → C, CD → A }
Decomposed into R1 (AD), R2 (AC), R3 (BCD)
Preserve information testing.
The initial tableau:
A B C D
R1 (AD) 0 1 1 0
R2 (AC) 0 2 0 2
R3 (BCD) 3 0 0 0
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 128/175
e.g. (cont.)
A B C D
R1 (AD) 0 1 1 0
R2 (AC) 0 1 0 2
R3 (BCD) 3 0 0 0
{ }
F = A → B, B → C, CD → A
Apply B → C to equate 1, 0. Chosing 0 as the representative symbol (Figure below).
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 129/175
e.g. (cont.)
A B C D
R1 (AD) 0 1 0 0
R2 (AC) 0 1 0 2
R3 (BCD) 3 0 0 0
{ }
F = A → B, B → C, CD → A
Apply CD → A for row 1 and row 3, to equate 0, 3. Chosing 0 as the representative
symbol (Figure below).
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 130/175
e.g. (cont.)
A B C D
R1 (AD) 0 1 0 0
R2 (AC) 0 1 0 2
R3 (BCD) 0 0 0 0
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 131/175
e.g.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 132/175
e.g. (cont.)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 133/175
e.g.
{ }
R(ABCDE), F = A → C, B → C, C → D, DE → C, CE → A ,
R1 (AD), R2 (AB), R3 (BE), R4 (CDE), R5 (AE)
The initial tableau:
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 2 2 2
R3 (BE) 3 0 3 3 0
R4 (CDE) 4 4 0 0 0
R5 (AE) 0 5 5 5 0
Apply A → C for rows: 1, 2, 5 to equate 1, 2, 5. Choosing 1 as the representative
symbol (Figure below).
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 134/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 2 2
R3 (BE) 3 0 3 3 0
R4 (CDE) 4 4 0 0 0
R5 (AE) 0 5 1 5 0
{ }
F= A → C, B → C, C → D, DE → C, CE → A
Apply B → C for rows: 2, 3 to equate 1, 3. Choosing 1 as the representative symbol.
(Figure below)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 135/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 2 2
R3 (BE) 3 0 1 3 0
R4 (CDE) 4 4 0 0 0
R5 (AE) 0 5 1 5 0
{ }
F= A → C, B → C, C → D, DE → C, CE → A
Apply C → D for roes: 1, 2, 3, 5 to equate 0, 2, 3, 5. The result symbol must be 0.
(Figure below)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 136/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 0 2
R3 (BE) 3 0 1 0 0
R4 (CDE) 4 4 0 0 0
R5 (AE) 0 5 1 0 0
{ }
F= A → C, B → C, C → D, DE → C, CE → A
Apply DE → C for rows: 3, 4, 5 to equate 1 with 0.
(Figure below)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 137/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 0 2
R3 (BE) 3 0 0 0 0
R4 (CDE) 4 4 0 0 0
R5 (AE) 0 5 0 0 0
{ }
F= A → C, B → C, C → D, DE → C, CE → A
Apply CE → A for rows: 3, 4, 5 to equate 3, 4, 0 must be with 0.
(Figure below)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 138/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 0 2
R3 (BE) 0 0 0 0 0
R4 (CDE) 0 4 0 0 0
R5 (AE) 0 5 0 0 0
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 139/175
e.g. (cont.)
A B C D E
R1 (AD) 0 1 1 0 1
R2 (AB) 0 0 1 0 2
R3 (BE) 0 0 0 0 0
R4 (CDE) 0 4 0 0 0
R5 (AE) 0 5 0 0 0
The third row has all 0. Preserve information decomposition ■
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Tableau Test 140/175
Preserve Information Decomposition Theorem
If (R1 ∩ R2 ) → (R1 − R2 ) ∈ F+ .
Result has the second row to be all 0.
R1 ∩ R2 R1 − R2 R2 − R1
row for R1 0···0 0···0 1···1
row for R2 0···0 0···0 0···0
If (R1 ∩ R2 ) → (R2 − R1 ) ∈ F+ .
Result has the first row to be all 0.
R1 ∩ R2 R1 − R2 R2 − R1
row for R1 0···0 0···0 0···0
row for R2 0···0 2···2 0···0
We has ρ Preserve information decomposition ■
A B C
1 1 1
2 1 2
π AB (r) π BC (r)
A B B C
1 1 1 1
2 1 1 2
A B C
1 1 1
1 1 2
2 1 1
2 1 2
This natural join is proper superset of r.
Preserve Dependencies
R , F.
{ }
R decomposed into ρ = R1 , R2 , · · · , Rn .
Decomposition ρ preserve F if the union of all the dependencies in π Ri (F), for i = 1..n
logically implies all the dependencies in F:
{ }
π R1 (F) ∪ π R2 (F) ∪ · · · ∪ π Rn (F) |= F, aka
∪
n
π Ri (F) |= F
i=1
Note: The converse is always true; F always implies all its projections, and therefore
implies their union.
∪
n
F |= π Ri (F)
i=1
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Preserve Dependencise 146/175
e.g.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Preserve Dependencise 147/175
e.g. (cont.)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Preserve Dependencise 148/175
Testing Preserve Dependencies
R , F , ρ = {R1 , · · · , Rn }.
Easy to test ρ preserves F by:
1. Computing Fi = π Ri (F+ ).
∪
n
2. Test whether: Fi |= F ?
i=1
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Testing Preserve Dependencies 149/175
e.g.
{ }
R(ABCD) , F = A → B, B → C, C → D, D → A . Decomposition
{ }
ρ = R1 (AB), R2 (BC), R3 (CD)
Project F+ onto Ri , we get result:
{ }
F1 = A → B, B → A ,
{ }
F2 = B → C, C → B ,
{ }
F3 = C → D, D → C
∪
3
So Fi |= F
i=1
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Testing Preserve Dependencies 150/175
Preserve Information Decomposition into BCNF
Decomposition Algorithm
Input: R0 , F0 .
Output: A decomposition of R0 into a collection of relations, all of which are in BCNF.
Method: R = R0 and F = F0 .
Apply recursively < R, F >.
1. If R is in BCNF, return R. Done.
2. If R violated BCNF by X → Y (X is not a super key), decomposition R into
R1 (X+F ) and R2 (X ∪ (R − XF )).
+
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Decomposition Algorithm 151/175
e.g.
Instock(Product,
{ Category, Supply, Date, Quantity), with
F = Product → Category; Category
} → Supply;
Product, Date → Quantity
Key = {Product, Date} and Instock is in 1NF.
1. R = Instock
R violated BCNF
{ by Product → Category}
+
Product = Product, Category, Supply
R1 (Product,
{ Category, Supply), }
F1 = Product
{ → } Category; Category → Supply
Key = Product , violated BCNF by Category → Supply
R2 (Product
{ ∪ (R − Product+ )), R}2 (Product, Date, Quantity)
F2 = Product,
{ Date →} Quantity
Key = Product, Date , it is in BCNF.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Decomposition Algorithm 152/175
e.g. (cont.)
2. R1 (Product,
{ Category, Supply) }
F1 = Product
{ →} Category; Category → Supply
Key = Product { , violated BCNF } by Category → Supply
Category+ = Category, Supply
R11 (Category,
{ Supply) }
F11 = {Category → } Supply
Key = Category , it is in BCNF.
R12 (Product,
{ Category) }
F12 = {Product → } Category
Key = Product , it is in BCNF.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Decomposition Algorithm 153/175
e.g. (cont.)
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Decomposition Algorithm 154/175
e.g. (cont.)
R({
PCSDQ); Key = PD; 1NF }
F = P → C; C → S; PD → Q
P→C
R1 (PCS
{ ); Key = P; 2NF
} R2 (PDQ); Key
F1 = P → C; C → S { = PD;}BCNF
F2 = PD → Q
C→S
Notice: This algorithm decomposition into BCNF, its preserve information but not
sure about preserve FDs.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Decomposition Algorithm 155/175
Preserve Information Decomposition into 3NF
Synthesis Algorithm
It decompose into 3NF Relations With preserve information and dependency.
Input: R , F
Output: Decomposition ρ, each of which is in 3NF. It has preserve information and
dependency.
Method: Perform the following steps:
1. Find a minimum cover of F, say Fc .
2. For each X → A ∈ Fc , use XA as the scheme of one of the relations in the
decomposition.
3. If none of the relation schemes from Step 2 is a superkey of R, add another
relation whose scheme is a key of R.
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Synthesis Algorithm 156/175
e.g.
{ }
R(ABCDE) , F = AB → C, C → B, A → D
F is also a minimum cover. So we have:
R1 (ABC) , R2 (BC) , R3 (AD).
Not to use a relation whose is a proper subset of another relation, so we can drop R2 .
R has two keys: K1 = ABE and K2 = ACE.
Neither of these keys is a subset of the schemes chosen so far. Thus, we must add one
of them, say R4 (ABE).
The final decomposition of R is thus:
{ }
R1 (ABC); F1 = { AB → C, } C→B ; 3NF
R2 (BC); F2 = { C → B }; BCNF
R3 (AD); F3 = A → D ; BCNF
R4 (ABE); F4 = ∅; BCNF
Nguyễn Văn Diêu 3. Logical Design :: Database Normalization :: Synthesis Algorithm 157/175
Overview
• A construction materials store wants to manage Product including names and unit
prices.
• List of ordering customers or potential customers with customer name and
address information.
• Every time an order is placed by a customer, the order needs to store the order
date, pick-up date, and customer. Order along with a list of products ordered and
quantity ordered.
• When delivery is due, issue a delivery invoice knowing that each order can be
delivered in multiple deliveries with different delivery quantities and each Order
there is an invoice for billing.
• The unit price at delivery may be different from the reference price in stock at the
time of ordering.
Nguyễn Văn Diêu 4. Physical Design :: Conceptual to Physical :: Relational Model 162/175
Functional Dependencies
Nguyễn Văn Diêu 4. Physical Design :: Conceptual to Physical :: Relational Model 163/175
Normal Form
Nguyễn Văn Diêu 4. Physical Design :: Conceptual to Physical :: Normal Form 164/175
Normalization
Normalization for:
• Invoice Detail(Invoice ID, Order ID, Product ID, Invoice Unit Price,
Invoice Quantity) 1NF
Result:
• R1 (Invoice ID, Order ID) BCNF
• R2 (Invoice ID, Product ID, Invoice Unit Price, Invoice Quantity) BCNF
Nguyễn Văn Diêu 4. Physical Design :: Conceptual to Physical :: Customize Name 167/175
Foreign Keys
Nguyễn Văn Diêu 4. Physical Design :: Conceptual to Physical :: Foreign Keys 168/175
SQL Script
Using indexes:
• To optimize data access
Accessing to data is faster with an index than without an index.
• To ensure uniqueness
A table with a unique index cannot have two rows with the same values in the
column or columns that form the index key.
• To enable clustering
When indexing, similar values will partition, optimizing page access for data.