DBMS Unit 4
DBMS Unit 4
Introduction
Database anomaly is a flaw in databases because of poor planning and
storing everything in a flat database. Anomalies occur when there is too
much redundancy in the database. Poor table design has related data
scattered over various tables. Any new change in the database should be
updated in many places. It is also possible that the information is only half
present. It's there in one table, but missing in another one.
In this article, we will dive deeper into learning in database anomalies. Start
reading with What are Anomalies in DBMS first.
The table will look like this. The table given below is not normalized. We will
see how problems arise when a table is not normalized.
e_id e_name e_address e_dept
Database System Concepts - 7th Edition 7.2 ©Silberschatz, Korth and Sudarshan
Overview of Normalization
Database System Concepts - 7th Edition 7.3 ©Silberschatz, Korth and Sudarshan
Overview
Database System Concepts - 7th Edition 7.4 ©Silberschatz, Korth and Sudarshan
Features of Good Relational Designs
Database System Concepts - 7th Edition 7.5 ©Silberschatz, Korth and Sudarshan
A Combined Schema Without Repetition
Database System Concepts - 7th Edition 7.6 ©Silberschatz, Korth and Sudarshan
Decomposition
The problem arises when we have two employees with the same name
§ The next slide shows how we lose information -- we cannot reconstruct
the original employee relation -- and so, this is a lossy decomposition.
Database System Concepts - 7th Edition 7.7 ©Silberschatz, Korth and Sudarshan
Normalization Theory
Database System Concepts - 7th Edition 7.8 ©Silberschatz, Korth and Sudarshan
A Lossy Decomposition
Database System Concepts - 7th Edition 7.9 ©Silberschatz, Korth and Sudarshan
Lossless Decomposition
Database System Concepts - 7th Edition 7.10 ©Silberschatz, Korth and Sudarshan
Example of Lossless Decomposition
§ Decomposition of R = (A, B, C)
R1 = (A, B) R2 = (B, C)
Database System Concepts - 7th Edition 7.11 ©Silberschatz, Korth and Sudarshan
Normalization Theory
Database System Concepts - 7th Edition 7.12 ©Silberschatz, Korth and Sudarshan
Functional Dependencies
§ There are usually a variety of constraints (rules) on the data in the real
world.
§ For example, some of the constraints that are expected to hold in a
university database are:
• Students and instructors are uniquely identified by their ID.
• Each student and instructor has only one name.
• Each instructor and student is (primarily) associated with only one
department.
• Each department has only one value for its budget, and only one
associated building.
Database System Concepts - 7th Edition 7.13 ©Silberschatz, Korth and Sudarshan
Functional Dependencies (Cont.)
Database System Concepts - 7th Edition 7.14 ©Silberschatz, Korth and Sudarshan
Functional Dependencies Definition
§ Let R be a relation schema
a Í R and b Í R
§ The functional dependency
a®b
holds on R if and only if for any legal relations r(R), whenever any two
tuples t1 and t2 of r agree on the attributes a, they also agree on the
attributes b. That is,
1 4
1 5
3 7
Database System Concepts - 7th Edition 7.15 ©Silberschatz, Korth and Sudarshan
Closure of a Set of Functional Dependencies
Database System Concepts - 7th Edition 7.16 ©Silberschatz, Korth and Sudarshan
Keys and Functional Dependencies
Database System Concepts - 7th Edition 7.17 ©Silberschatz, Korth and Sudarshan
Use of Functional Dependencies
Database System Concepts - 7th Edition 7.18 ©Silberschatz, Korth and Sudarshan
Trivial Functional Dependencies
Database System Concepts - 7th Edition 7.19 ©Silberschatz, Korth and Sudarshan
Lossless Decomposition
Database System Concepts - 7th Edition 7.20 ©Silberschatz, Korth and Sudarshan
Example
§ R = (A, B, C)
F = {A ® B, B ® C)
§ R1 = (A, B), R2 = (B, C)
• Lossless decomposition:
R1 Ç R2 = {B} and B ® BC
§ R1 = (A, B), R2 = (A, C)
• Lossless decomposition:
R1 Ç R2 = {A} and A ® AB
§ Note:
• B ® BC
is a shorthand notation for
• B ® {B, C}
Database System Concepts - 7th Edition 7.21 ©Silberschatz, Korth and Sudarshan
Dependency Preservation
Database System Concepts - 7th Edition 7.22 ©Silberschatz, Korth and Sudarshan
Dependency Preservation Example
§ Consider a schema:
dept_advisor(s_ID, i_ID, department_name)
§ With function dependencies:
i_ID ® dept_name
s_ID, dept_name ® i_ID
§ In the above design we are forced to repeat the department name once
for each time an instructor participates in a dept_advisor relationship.
§ To fix this, we need to decompose dept_advisor
§ Any decomposition will not include all the attributes in
s_ID, dept_name ® i_ID
§ Thus, the composition NOT be dependency preserving
Database System Concepts - 7th Edition 7.23 ©Silberschatz, Korth and Sudarshan
Steps of
Normalization
Database System Concepts - 7th Edition 7.24 ©Silberschatz, Korth and Sudarshan
Normal Forms
In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!
Database System Concepts - 7th Edition 7.25 ©Silberschatz, Korth and Sudarshan
Normal Forms
In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!
Database System Concepts - 7th Edition 7.26 ©Silberschatz, Korth and Sudarshan
Normal Forms
In practice, 1NF, 2NF, 3NF, and BCNF are enough for database.
So we will focus on them!
Database System Concepts - 7th Edition 7.27 ©Silberschatz, Korth and Sudarshan
Normal Forms
Database System Concepts - 7th Edition 7.28 ©Silberschatz, Korth and Sudarshan
Goals of Normalization
Database System Concepts - 7th Edition 7.29 ©Silberschatz, Korth and Sudarshan
First Normal Form (1NF)
Additional:
• Choose a primary key.
•
Reminder:
• A primary key is unique, not null, unchanged.
• A primary key can be either an attribute or combined attributes.
Database System Concepts - 7th Edition 7.30 ©Silberschatz, Korth and Sudarshan
First Normal Form (formally)
Database System Concepts - 7th Edition 7.31 ©Silberschatz, Korth and Sudarshan
First Normal Form (1NF)
Database System Concepts - 7th Edition 7.32 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)
Database System Concepts - 7th Edition 7.33 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)
The Course Name depends on only CourseID, a part of the primary key not
the whole primary {CourseID, SemesterID}. It’s called partial dependency.
Solution:
Remove CourseID and Course Name together to create a new table.
Database System Concepts - 7th Edition 7.34 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)
The Course Name depends on only CourseID, a part of the primary key not
the whole primary {CourseID, SemesterID}. It’s called partial dependency.
Solution:
Remove CourseID and Course Name together to create a new table.
Database System Concepts - 7th Edition 7.35 ©Silberschatz, Korth and Sudarshan
Third Normal Form (3NF)
Database System Concepts - 7th Edition 7.36 ©Silberschatz, Korth and Sudarshan
Third Normal Form
Database System Concepts - 7th Edition 7.37 ©Silberschatz, Korth and Sudarshan
Third Normal Form (3NF)
Database System Concepts - 7th Edition 7.38 ©Silberschatz, Korth and Sudarshan
Second Normal Form (2NF)
Database System Concepts - 7th Edition 7.39 ©Silberschatz, Korth and Sudarshan
3NF Example
§ Consider a schema:
dept_advisor(s_ID, i_ID, dept_name)
§ With function dependencies:
i_ID ® dept_name
s_ID, dept_name ® i_ID
§ Two candidate keys = {s_ID, dept_name}, {s_ID, i_ID }
§ We have seen before that dept_advisor is not in BCNF
§ R, however, is in 3NF
• s_ID, dept_name is a superkey
• i_ID ® dept_name and i_ID is NOT a superkey, but:
§ { dept_name} – {i_ID } = {dept_name } and
§ dept_name is contained in a candidate key
Database System Concepts - 7th Edition 7.40 ©Silberschatz, Korth and Sudarshan
Redundancy in 3NF
Database System Concepts - 7th Edition 7.41 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form
Database System Concepts - 7th Edition 7.42 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form
Database System Concepts - 7th Edition 7.43 ©Silberschatz, Korth and Sudarshan
Boyce-Codd Normal Form (Cont.)
Database System Concepts - 7th Edition 7.44 ©Silberschatz, Korth and Sudarshan
Decomposing a Schema into BCNF
Database System Concepts - 7th Edition 7.45 ©Silberschatz, Korth and Sudarshan
Example
§ R = (A, B, C)
F = {A ® B, B ® C)
§ R1 = (A, B), R2 = (B, C)
• Lossless-join decomposition:
R1 Ç R2 = {B} and B ® BC
• Dependency preserving
§ R1 = (A, B), R2 = (A, C)
• Lossless-join decomposition:
R1 Ç R2 = {A} and A ® AB
• Not depend ency preserving
(cannot check B ® C without computing R1 R2)
Database System Concepts - 7th Edition 7.46 ©Silberschatz, Korth and Sudarshan
Example
Database System Concepts - 7th Edition 7.47 ©Silberschatz, Korth and Sudarshan
Example
Database System Concepts - 7th Edition 7.48 ©Silberschatz, Korth and Sudarshan
BCNF and Dependency Preservation
Database System Concepts - 7th Edition 7.49 ©Silberschatz, Korth and Sudarshan
Comparison of BCNF and 3NF
Database System Concepts - 7th Edition 7.50 ©Silberschatz, Korth and Sudarshan
How good is BCNF?
Database System Concepts - 7th Edition 7.51 ©Silberschatz, Korth and Sudarshan
How good is BCNF? (Cont.)
Database System Concepts - 7th Edition 7.52 ©Silberschatz, Korth and Sudarshan
Higher Normal Forms
• inst_phone:
§ This suggests the need for higher normal forms, such as Fourth
Normal Form (4NF), which we shall see later
Database System Concepts - 7th Edition 7.53 ©Silberschatz, Korth and Sudarshan
Design Goals
Database System Concepts - 7th Edition 7.54 ©Silberschatz, Korth and Sudarshan
Testing for BCNF
Database System Concepts - 7th Edition 7.56 ©Silberschatz, Korth and Sudarshan
Testing Decomposition for BCNF
Database System Concepts - 7th Edition 7.57 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition Algorithm
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 a ® b be a nontrivial functional dependency that
holds on Ri such that a ® Ri is not in F +,
and a Ç b = Æ;
result := (result – Ri ) È (Ri – b) È (a, b );
end
else done := true;
Database System Concepts - 7th Edition 7.58 ©Silberschatz, Korth and Sudarshan
Example of BCNF Decomposition
Database System Concepts - 7th Edition 7.59 ©Silberschatz, Korth and Sudarshan
BCNF Decomposition (Cont.)
§ course is in BCNF
• How do we know this?
§ building, room_number→capacity holds on class-1
• but {building, room_number} is not a superkey for class-1.
• We replace class-1 by:
§ classroom (building, room_number, capacity)
§ section (course_id, sec_id, semester, year, building,
room_number, time_slot_id)
§ classroom and section are in BCNF.
Database System Concepts - 7th Edition 7.60 ©Silberschatz, Korth and Sudarshan
Overall Database Design Process
Database System Concepts - 7th Edition 7.61 ©Silberschatz, Korth and Sudarshan
ER Model and Normalization
Database System Concepts - 7th Edition 7.62 ©Silberschatz, Korth and Sudarshan
Denormalization for Performance
Database System Concepts - 7th Edition 7.63 ©Silberschatz, Korth and Sudarshan
Other Design Issues
Database System Concepts - 7th Edition 7.64 ©Silberschatz, Korth and Sudarshan
End of Chapter 7
Database System Concepts - 7th Edition 7.65 ©Silberschatz, Korth and Sudarshan