unit-4-DBMS
unit-4-DBMS
1. Update Anomaly: If there is an update in the fee from 5000 to 7000, then we have to
update the FEE column in all the rows; otherwise, the data will become inconsistent.
Insertion Anomalies:
When a new course, say C4, is introduced, but no student is enrolled in the C4 subject. Because of
the insertion of some data, it is forced to insert some other dummy data.
Deletion Anomaly:
Deletion of the S3 student causes the deletion of the course. Because of the deletion of some
data, it forces the deletion of some other useful data.
Solutions to Anomalies:
DECOMPOSITION OF TABLES
Purpose of Normalization:
Advantages of Normalization:
Disadvantages of Normalization:
1. Normalization produces many tables with a relatively small number of columns. These
columns then have to be joined using their primary/foreign key relationship.
⇒ Performance: All the joins required to merge data slow down processing and place
additional stress on your hardware.
⇒ Complex Queries: Developers have to code complex queries to merge data from different
tables.
2) Functional Dependency
Example: R (A, B, C, D) and set of Functional Dependencies are A→B, B→D, C→B then
what is the Closure of A, B, C, D?
Solution: A* is
A functional dependency is said to be a full dependency if and only if the determinant of the
functional dependency is either a candidate key or a super key, and the dependent can be
either a prime or non-prime attribute.
Explanation: Let’s take the functional dependency X →Y (i.e., X determines Y). Here, Y is
said to be fully dependent if it cannot determine any subset of X.
Example: Consider the following determinant ABC→D. Here, ABC determines D, but D is
not determined by any subset of A, B, C, or AB, BC, AC. So, D is fully functionally
dependent on ABC.
2. Partial Functional Dependency:
If a non-prime attribute of the relation is derived by only a part of the candidate key, then
such a dependency is known as a partial dependency.
Explanation: In a relation having more than one key field, a subset of non-key fields may
depend on all key fields, but another subset or a particular non-key field may depend on only
one of the key fields. Such a dependency is defined as a partial dependency.
Example: Consider the determinants AC→P, A→D, D→P. From these determinants, P is not
fully functionally dependent on AC. If we find A+ (i.e., A’s closure), A→D, D→P, i.e., A→P.
But we don’t have any requirement of C. The C attribute is removed completely. So, P is
partially dependent on AC.
Example: If X→Y and Y→Z, then we can determine that X→Z holds.
5. Multi-Valued Dependency:
Consider three fields X, Y, and Z in a relation. If for each value of X, there is a well-defined
set of values for Y and a well-defined set of values for Z, and the set of values for Y is
independent of the set of values for Z, this dependency is a multi-valued dependency, i.e.,
X→Y and X→Z.
Types of Decomposition
There are two types of Decomposition:
Lossless Decomposition
Lossy Decomposition
Lossless Decomposition
This table has redundant data as the Course_Id and Course_Detail are common
for several students. Let’s decompose this relation into two relations.
Student Table:
Course Table:
Let’s check all the three rules of lossless decomposition to check whether this
decomposition is lossless or not.
Rule 1:
{Student} U {Course}
Union Result:
The union results in the original relation StudentCourse so we can say that
the first rule holds true.
Rule 2 & 3:
R1 ∩ R2
Result:
Course_Id
C01
C02
C03
The result is a super key of the second relation R2 so the third rule also applies
here.
These dependencies are still present in the decomposed relations. Thus we can
say that this decomposition is dependency preserving.
Since all the three rules applies here, the decomposition of relation StudentCourse
into Student and Course is a lossless decomposition.
2. Lossy Decomposition
StudentCourse Table:
Student Table:
Course Table:
Course_Id Course_Detail
C01 Maths
C02 Science
C03 English
In this decomposition, the relation of Student and Course is lost, there is no way
to form the original relation from these two relations as the information that
suggests who is attending which course is lost during decomposition.
4) Normal forms
STUDENT table:
The decomposition of the STUDENT table into 1NF has been shown below:
There are several conditions for keeping the second normal shape of the
database table. These are the following -
Example: let's we assume that teachers' data and the subjects they teaches
can be stored by a school. A teacher can teach more than one subject in a
school.
TEACHER table:
T1 Hindi 45
T1 Science 45
T2 Maths 35
T3 English 28
T3 SST 28
The non-prime attribute TEACHER AGE in the provided table is based on
TEACHER ID, which is the right subset of a candidate key. That is why it
violates the 2NF law.
We split it down into two tables to translate the given table into 2NF:
TEACHER_ID TEACHER_AGE
T1 45
T2 35
T3 28
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
T1 Hindi
T1 Science
T2 Math
T3 English
T3 SST
X is a Super key.
Y is a primary attribute.
If A->B and B->C are two FDs then A->C is called transitive dependency.
Example-1:
In relation TEACHER given in following table,
TEACHER table:
101 Rex Z1
102 Rohan Z2
103 Puneet Z3
104 Jack Z4
105 Amit Z5
TEACHER_ZIP table:
TEACHER_ZIP_CODE TEACHER_CITY
Z1 Noida
Z2 Boston
Z3 Chicago
Z4 Norwich
Z5 Bhopal
There are several conditions for keeping the Boyce Codd normal
form which forms of the database table. These are the following -
The modern variant of 3NF is the BCNF. Stricter than 3NF, it is.
STUDENT table:
1. ST_ID → ST_COUNTRY
2. ST_SPECIALISATION → {DEPT_TYPE,
ST_SPECIALISATION_NO}
To convert the given table into BCNF, we decompose it into three tables:
ST_COUNTRY table:
ST_ID ST_COUNTRY
101 India
101 India
ST_SPECIALISATION table:
Engineering D101 S1
IT D101 S2
Architecture D201 S3
Maths D201 S4
ST_SPECIALISATION_MAPPING table:
ST_ID ST_SPECIALISATION
D101 S1
D101 S2
D201 S3
D201 S4
Functional dependencies:
1. ST_ID → ST_COUNTRY
2. ST_SPECIALISATION→{DEPT_TYPE,
ST_SPECIALISATION_NO}
Candidate keys:
Now, this is in BCNF because left side part of both the functional
dependencies is a key.
Example
STUDENT
The given STUDENT table is in 3NF, but the COURSE and HOBBY are
two independent entity. Hence, there is no database between COURSE and
HOBBY.
STUDENT_COURSE
STU_ID COURSE
101 Maths
101 Physics
102 Chemistry
103 Science
104 English
STUDENT_HOBBY:
STU_ID HOBBY
101 Dancing
101 Singing
102 Dancing
103 Cricket
104 Hockey
Example:
In the table above for semester 1, Ram takes both computer and math
classes, but for semester 2, he does not take math classes. In this case, all
these fields must be combined to classify valid data.
Table 1:
SEMESTER SUBJECT
I Physics
II Physics
I Chemistry
II Math
Table 2:
SUBJECT LECTURER
Table 3:
SEMSTER LECTURER
I Mr. Kabir
II Mrs. Hemlata
I Mr. Ramkumar
II Mr. Lal K.