ADBMS-Module 2 Normalisation
ADBMS-Module 2 Normalisation
2
Denormalization produces a lower normal form;
3
Consider the table: student
4
If we want to insert a student information, we have to repeat the
information about department, building and room number for each
student.
This problem occurs when we take large schemas. (storing all the
information in only one table)
5
If we want to insert a student information, we have to repeat the
information about department, building and room number for each
student.
This problem occurs when we take large schemas. (storing all the
information in only one table)
6
If we decompose this table into smaller tables, then we can reduce the
redundancy.
Data Anomalies
7
Example:
If we want to insert a new department ME to this table, we must
insert sid, sname and credits in the tuple. But if no students are enrolled
into the department, we can not insert the student information.
8
We can’t insert null value for Sid because Sid is the primary key that can
not be null.
9
An update anomaly is a data inconsistency that results from data
redundancy and a partial update.
So we must update these info. in all the tuples where Dept_name is “CSE”.
10
Here the last row of CSE is not updated.
There exists a data inconsistency that results from data redundancy and a
partial update. This is called update anomaly
11
A deletion anomaly is the unintended loss of data due to deletion of
other data.
12
How to solve the anomalies?
14
Normalization:
• Process of making the table free from insert, update and delete
anomalies.
• Saves space by reducing the redundant data or duplicate data.
• Simplifies the queries(when dividing large tables into smaller one)
• Minimizes Null values.
• Important for OLTP systems where insert, update and delete
anomalies exist very frequently by end users.
• It simplifies the database structures.
• Small tables – easily understand the schema.
• Searching will be faster.
15
Functional dependency
The attribute set on the left side of the arrow, X is called Determinant,
while on the right side, Y is called the Dependent.
16
Let R be the relation schema and X,Y be some sets of attributes over R.
17
18
Check whether the given functional dependencies are valid or not
roll_no → name
name → dept_name
roll_no → dept_name
roll_no → dept_building
dept_building → dept_name
dept_name → dept_building
{roll_no, name} → {dept_name, dept_building}
name → roll_no
dept_building → roll_no
19
Some valid functional dependencies:
Here, roll_no can determine values of fields name, dept_name and dept_building,
hence a valid Functional dependency.
roll_no → dept_name , Since, roll_no can determine whole set of {name, dept_name,
dept_building}, it can determine its subset dept_name also.
Students with the same name can have different dept_name, hence this is
not a valid functional dependency.
dept_building → dept_name
21
Inference Rule (IR):
For example,
23
3. Augmentation (IR3) : If X → Y is a valid dependency, then XZ → YZ is
also valid by the augmentation rule.
For example,
24
4. Union Rule (IR4) :
If X → Y and X → Z then X → YZ
25
5. Decomposition Rule (IR5) or Splitting property :
If X → YZ then X → Y and X → Z
If X → Y and YZ → W
Then XZ → W
27
7. Composition(IR7) :
If X → Y and A → B
Then XA → YB
28
Types of Functional dependencies in DBMS:
29
1. Trivial Functional Dependency
30
For example, Here, {roll_no, name} → name is a trivial
functional dependency,
since the dependent name is a subset of
determinant set {roll_no, name}
31
Non-trivial Functional Dependency
32
For Example,
Here, roll_no → name is a non-trivial functional
dependency, since the dependent name is not a
subset of determinant roll_no.
33
3. Multivalued Functional Dependency
34
For Example,
Here, roll_no → {name, age} is a multivalued
functional dependency,
35
4. Transitive Functional Dependency
36
For example
Here, enrol_no → dept and
dept → building_no,
A->B
B->C
C->D
D->E
38
Closure of A ie, A+ :
39
Or we can find out A+ like this:
A->A(reflexivity)
40
B+ = {B,C,D,E}
C+ = {C,D,E}
D+ = {D,E}
E+ = {E}
AD+ = {A,D,B,C,E}
CD+ = {C,D,E}
41
Example-
Find out A+
D+
And { B , C }+
42
Consider the given functional dependencies-
AB → CD
AF → D
DE → F
C→G
F→E
G→A
Find out
(A) { CF }+
(B) { BG }+
(C) { AF }+
(D) { AB }+
43
Minimal set of Functional Dependencies
A canonical cover is a simplified and reduced version of the given set of
functional dependencies.
Characteristics-
Canonical cover is free from all the extraneous functional dependencies.
Canonical cover is not unique and may be more than one for a given set of
functional dependencies.
44
Need-
This reduces the computation time and working with the irreducible set
becomes easier.
45
Algorithm
Step 1: Write the FD’s in such a way that the right hand side must contain
1 attribute.
46
Example:
Step 1: Write the FD’s in such a way that the right hand side must contain
1 attribute.
47
Step 2: Find the Closure of Attributes. And check whether the FD is
essential or not. X -> W
48
2. Consider WZ -> X X -> W
WZ + = { W,Z,X,Y} WZ -> X
WZ + = { W,Z,X,Y} Y -> W
49
2. Consider WZ -> Y
X -> W
WZ + = { W,Z,Y,X}
By ignoring WZ -> X
WZ -> Y
WZ + = { W,Z}
Y -> W
So by ignoring WZ -> Y
, we can not derive the same closure. Y -> X
50
2. Consider Y -> W
X -> W
Y + = {Y,W,X,Z}
By ignoring Y -> W
WZ -> Y
Y + = {Y,X,Z,W}
Y -> W
So by ignoring Y -> W, we can derive the
same closure. Y -> X
So Y -> W is not essential and can be
removed. Y -> Z
51
2. Consider Y -> X
X -> W
Y + = {Y,X,Z,W}
By ignoring Y -> X
WZ -> Y
Y + = {Y,Z}
52
2. Consider Y -> Z
X -> W
Y + = {Y,X,Z,W}
By ignoring Y -> Z
WZ -> Y
Y + = {Y,X,W}
53
So we removed all extraneous attributes, we get the
functional dependencies as X -> W
WZ -> Y
Y -> X
Y -> Z
54
Step 3: Check whether any left hand attributes can be reduced.
Consider WZ -> Y and check whether we can eliminate any one of the
attributes or not.
56
First Normal Form (1NF)
For a table to be in the First Normal Form, it should follow the following
4 rules:
57
Rule 1: Single Valued Attributes
Each column of your table should be single valued which means they should
not contain multiple values.
In each column the values stored must be of the same kind or type.
101 Rahul 18
102 Anjali Good Student
The column Age/Remarks stores mixed data types (a number and a text
comment - Not in 1NF)
59
Rule 3: Unique name for Attributes/Columns
This rule expects that each column in a table should have a unique name.
This is to avoid confusion at the time of retrieving data or performing any
other operation on the stored data.
Column names are not unique → Confusing and invalid (Not in 1NF)
60
Rule 4: Order doesn't matters
This rule says that the order in which you store the data in your table
doesn't matter.
change the order of rows — it does not affect the database or normalization.
61
roll_no name subject
101 Abin OS, CN
103 Ciya Java
102 Binu C, C++
Our table already satisfies 3 rules out of the 4 rules, as all our column
names are unique, we have stored data in the order we wanted to and
we have not inter-mixed different type of data in columns.
But out of the 3 different students in our table, 2 have opted for more
than 1 subject. And we have stored the subject names in a single column.
But as per the 1st Normal form each column must contain atomic value.
62
How to solve this Problem?
It's very simple, because all we have to do is break the values into
atomic values.
Here is our updated table and it now satisfies the First Normal Form
Roll_no name subject
101 Abin OS
101 Abin CN
103 Ciya Java
102 Binu C
102 Binu C++
63
Student_ID Name Courses Contact Numbers
9876543210,
101 Rahul Math, Science
9123456789
9876501234,
103 Kiran Math, English
9123009876
•Contact Numbers also contains multiple phone numbers in one cell → Not atomic
64
Convert to 1NF: We break down multi-valued attributes into separate rows, so
each value becomes atomic.
Student_ID Name Course Contact Number
65
By doing so, although a few values are getting repeated but values for
the subject column are now atomic for each record/row.
Using the First Normal Form, data redundancy increases, as there will be
many columns with same data in multiple rows but each row as a whole
will be unique.
66
Second Normal Form
67
Example:
student_id subject_id student_name marks
101 S1 Rahul 80
101 S2 Rahul 70
102 S1 Anjali 90
102 S2 Anjali 60
There can be many different solutions for this, but our objective is to
decompose the table into two:
69
To Convert to 2NF: We remove partial dependency by decomposing the table into
two:
1. Student Table
student_id student_name
101 Rahul
102 Anjali
2. Marks Table
71
Notes
72
Third Normal Form (3NF)
73
Initial Table: Marks Entry (In 2NF but Not in 3NF)
74
• This table is in 1NF
(Because each student can have multiple subjects, and marks are identified
by both)
75
Depends on Depends on Depends on
Attribute Comment
student_id? subject_id? both?
Depends on the
specific student
marks ❌ ❌ ✅
and subject (full
key)
A student's exam
exam_type ❌ ❌ ✅ type is based on
subject taken
Depends only on
exam_type (not on
max_marks ❌ ❌ ❌
student or subject
76 directly) ❌
marks and exam_type depend on the full composite key → No partial
dependency
77
Why Not in 3NF
exam_type → max_marks
78
Transitive Dependency.
A transitive dependency occurs in a database table when a non-key
attribute depends on another non-key attribute, not directly on the
primary key.
A→B
B→C
Then, A → C is a transitive dependency
Where:
A is a candidate key (or part of the primary key)
B is a non-prime attribute
C is also a non-prime attribute
This violates the rules of Third Normal Form (3NF).
79
How to remove Transitive Dependency
To Convert to 3NF:
1. Score Table
exam_type max_marks
Midterm 100
Practical 50
81
Now the Tables Are in 3NF
• No transitive dependency
82
Boyce-Codd Normal Form (BCNF)
Rules for BCNF
For a table to satisfy the Boyce-Codd Normal Form, it should satisfy the
following two conditions:
83
Consider a table college enrolment table with columns student_id,
subject and professor.
84
In the table :
One student can enrol for multiple subjects. For example, student with
student_id 101, has opted for subjects - Java & C++
And, there can be multiple professors teaching one subject like we have
for Java.
85
here, one professor teaches only one subject, but one subject may have
two different professors.
86
This table satisfies the 1st Normal form because all the values are atomic,
column names are unique and all the values stored in a particular column
are of same domain.
This table also satisfies the 2nd Normal Form as their is no Partial
Dependency.
87
Why this table is not in BCNF?
In the table above, student_id, subject form primary key, which means
subject column is a prime attribute.
88
To make this relation(table) satisfy BCNF, we will decompose this table
into two tables, student table and professor table.
Student Table
student_id p_id
101 1
101 2
Professor Table
p_id professor subject
1 [Link] Java
2 [Link] C++
For a table to satisfy the Fourth Normal Form, it should satisfy the
following two conditions:
90
What is Multi-valued Dependency?
A table is said to have multi-valued dependency, if the following conditions are
true,
If all these conditions are true for any relation(table), it is said to have multi-
valued dependency.
91
Example
Below we have a college enrolment table with columns s_id, course and
hobby.
92
student with s_id 1 has opted for two courses, Science and Maths, and has
two hobbies, Cricket and Hockey. this can lead to some problems.
The two records for student with s_id 1, will give rise to two more records,
as shown below, because for one student, two hobbies exists, hence along
with both the courses, these hobbies should be specified.
s_id course hobby
1 Science Cricket
1 Maths Hockey
1 Science Hockey
1 Maths Cricket
93
And, in the table above, there is no relationship between the columns
course and hobby.
94
How to satisfy 4th Normal Form?
To make the above relation satify the 4th normal form, we can decompose
the table into 2 tables.
CourseOpted Table
s_id course
1 Science
1 Maths
2 C#
2 Php
95
And, Hobbies Table,
s_id hobby
1 Cricket
1 Hockey
2 Cricket
2 this relation satisfies the fourthHockey
Now normal form.
Any relation in order to be in the fifth normal form must satisfy the
following conditions:
In the fifth normal form the relation must be decomposed in as many sub-
relations as possible so as to avoid any kind of redundancy and there must
be no extra tuples generated when the sub-relations are combined
together by using natural join.
97
5NF is also known as Project Join Normal Form (PJNF).
Thus if natural join is performed on all the three relations then there will
be no extra tuples.
98
Spurious Tuples are those rows in a table, which occur as a result of joining
two tables in wrong manner. They are extra tuples (rows) which might not
be required.
R1 ⨝ R2 ⨝ R3 .... ⨝ Rn = R
Whereas condition for getting Spurious Tuples is denoted by,
R ⊂ R1 ⨝ R2 ⨝ R3 .... ⨝ Rn
99
Example-1:
Example to check if given relation contains Spurious Tuples.
Let R be Relation, and R1 and R2 be relations which we get after
decomposing R.
100
Thus if natural join is performed on all these relations then there will be no extra tuples.
Hence R1, R2 are in fifth normal form (5NF). 101
102
103
SURROGATE KEY CONSIDERATIONS
A composite primary key is too cumbersome to use as the number of
attributes grows.
There will also be problems when the key attribute might simply have
too much descriptive content
105
Consider an example :
Suppose we have two tables of two different schools having the same
column registration_no , name and percentage , each table having its own
natural primary key, that is registration_no.
Table of school A –
registration_no name percentage
210101 Harry 90
210102 Maxwell 65
210103 Lee 87
210104 Chris 76
106
Table of school B –
CS107 Taylor 49
CS108 Simon 86
CS109 Sam 96
CS110 Andy 58
107
Now, suppose we want to merge the details of both the schools in a single
table .
surr_no registration_no name percentage
Resulting table will be –
1 210101 Harry 90
2 210102 Maxwell 65
3 210103 Lee 87
4 210104 Chris 76
5 CS107 Taylor 49
6 CS108 Simon 86
7 CS109 Sam 96
8 CS110 Andy 58
As there is no direct information related with the table, so the changes are
only based on the requirements of the application.
109
Improving the Design of a Database
110
Evaluate Primary Key Assignments
You will need to focus on specific entities, usually the Primary Keys, in
order to make a successful and reliable database.
111
A surrogate key could be used in this instance by creating tables through
primary keys.
112
Evaluate Naming Conventions
113
Refine Attribute Atomicity
114
Identify New Attributes
115
Identify New Relationships
116
Refine Primary Keys as Required for Data Granularity
Obviously data that is created in the lower levels of the database (Atomic)
and sub-levels are not as important as the higher, broader attributes.
However using the granularity of these low key atomic values will help
keep the database more detailed and specific on what needs to be
categorized where. An example of this could involve assigning the number
of hours an individual drives. However this attribute isn't clear enough by
forgetting to state the specifics of the time periods the individual drives
such as daily, weekly, monthly and so on. Be sure to focus on explaining
these details clearly so if you're arranging a report you can sort by more
specific details.
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142