SQL modules
SQL modules
P R E PA R E D B Y :
Sumati Baral
Introduction:
⚫ Roll Name
⚫ Roll Mark Mathematical Representation of F D
( LHS RHS )
⚫ Roll Address
Example:
X Y
if AB C
if X YZ
Functional Dependency In DBMS : Armstrong’s Axioms
Axioms in database management systems was introduced by William W. Armstrong in
late 90’s and these axioms play a vital role while implementing the concept of functional
dependency in DBMS for database normalization. There exists six inferences known a s
“Armstrong’s Axioms” which are discussed below.
1.Reflexive : It means, if set “B” is a subset of “A”, then A B.
2.Augmentation : It means, if A B, then AC BC.
3.Transitive : It means, if A B&B C, then A C.
4.Decomposition : It means, if A BC, then A B&A C.
5.Union : It means, if A B&A C, then A BC.
6.Pseudo-Transitivity : It means, if A B and DB C, then DA C.
NOTE : These rules are helpful while dealing with determination of closure of functional
dependency in DBMS and calculation of canonical cover. Both of these topics are
discussed in the next chapter.
Inference Rules for Functional Dependencies (ARMSTRONG AXIOMS)
A. BASIC RULES:
1. Reflexive Rule:
The LHS can determine itself or part of itself.
3. Transitive Rule
It states that if an LHS determine an RHS and further that RHS
determines another RHS then it can be said that the first LHS determine
the last RHS.
If A →B and B → C then A → C
If A → B and A→ C then A → B C
Proof:
Given A→ B and A → C
Then AA → A B (By Augmentation Rule),
ie A→ A B ------( a)
Proof:
Given A → B and B C → D
Then A C → B C (By Augmentation Rule),
Thus AC → D (By Transitive Rule),
ANAMOLIES IN DESIGNING DB
Student
Insertion Anomaly: Let’s say we have a table that has 4 columns. Student ID, Student Name,
Student Address and Student Grades. Now when a new student enroll in school, even though
first three attributes can be filled but 4th attribute will have NULL value because he doesn't have
any marks yet.
Update Anomaly: It refers to the inconsistencies arising due to update of multiple
copies of the same facts ie whenever updates are made and not all, but only
some copies are updated.
For example:
{Employee ID} → {Employee Date of Birth} would hold.
For example:
{Employee ID, Employee Address} → {Employee Address}
Full functional dependency : An attribute is fully functionally dependent
on a set of attributes X if it is: functionally dependent on X, and not
functionally dependent on any proper subset of X.
Example:
{Employee ID, Skill} → {Employee Address} is not a full functional
dependency as {Employee ID} → {Employee Address}
Definition:
⚫ Database normalization is the process of structuring a relational
database, in accordance with a series of so-called normal forms in
order to reduce data redundancy and improve data integrity.
For a table to be in the First Normal Form, it should follow the following 4 rules:
⚫ It should only have single(atomic) valued attributes/columns.
⚫ Values stored in a column should be of the same domain
⚫ All the columns in a table should have unique names.
⚫ And the order in which data is stored, does not matter.
⚫ The Student table can now be decomposed as it contains redundant
data.
[Tables in 2NF]
Third Normal Form (3NF)
A relation is said to be in 3NF if it is in 2NF and no non key attribute is
transitively dependent on a key through some other non key attribute.
Remove all transitive dependencies.
Department
DeptNo DeptName
10 Finance
20 Admin
But, “ProfessorId” is a non prime attribute where as “Subject” is a part of the key and
hence a prime attribute. So BCNF doesn’t hold good as the LHS is not a superkey.
Let’s decompose the table
Student
Roll ProfessorId
10 101
10 102
20 103
20 102
30 104
40 105
Professor
ProfessorId Professor Subject
101 Mr. Smith C
102 Mr. Jack Java
103 Mr. Henry C++
104 Mr Bob C
105 Mr. Kenny Java
Example 2
ProfessorCode Department HeadOfDepartment PercentTime
P1 Physics Ghosh 50
P1 Mathematics Krishnan 50
P2 Chemistry Rao 25
P2 Physics Ghosh 75
The relation diagram for the above relation is given as the following:
The relation is not in B C N F so it need to be decomposed.
Professor Code Department Percent Time
P1 Physics 50
P1 Mathematics 50
P2 Chemistry 25
P2 Physics 75
P3 Mathematics 100
Department HeadOfDepartment
Physics Ghosh
Mathematics Krishnan
Chemistry Rao
Fourth Normal Form(4NF)
It should satisfy BCNF.
A table is said to have multi-valued dependency, if the following conditions are true,
For a dependency A → B, if for a single value of A, multiple value of B exists,
then the table may have multi-valued dependency.
The two records for student with Roll 1, will give rise to two more records, as shown
below
ROLL SUBJECTS HOBBY
1 Physics Cricket
1 Physics Badminton
1 Math Cricket
1 Math Badminton
Hobbies
ROLL HOBBY
1 Cricket
1 Badminton
2 Hockey
2 Cricket
In case of a table with functional dependency along with multi-valued dependency.,
the functionally dependent columns are moved in a separate table and the multi-
valued dependent columns are moved to separate tables.
Example:
ROLL ADDRESS SUBJECTS HOBBY
SPC table
ER diagram:
SUPPLIER CUTOMER
SUPPLIER PRODUCT
Smith Hundai
Smith Switch
CUTOMER PRODUCT
Hundai Switch
If a relation has join dependency then it can be divided into smaller relations such
that if we combine the smaller relations then we can get the original table.
If join dependency doesn’t exist then either data is lost or new entries are created.
AGENT PRODUCT
AGENT C O M PA N Y
Vivek Laptop
Vivek ABC
Vivek Modem
Vivek XYZ
Vivek Desktop
Swati ABC
Swati Desktop
AGENT C O M PA N Y PRODUCT
V ivek ABC L aptop
V ivek ABC Modem
V ivek ABC Desktop **(spurious row)
V ivek XYZ Laptop **(spurious row)
V ivek XYZ Modem **(spurious row)
V ivek XYZ Desktop
S wati ABC Desktop
C O M PA N Y PRODUCT
ABC Laptop
ABC Modem
ABC Desktop
XYZ Desktop
P1 * P2 *P3
AGENT C O M PA N Y PRODUCT
Vivek ABC Laptop
Vivek ABC Modem
Vivek ABC Desktop **(spurious row)
Vivek XYZ Desktop
Swati ABC Desktop
Now consider different case:
P1 * P2
P1 * P2 * P3
Joint dependency – Join decomposition is a further generalization of Multivalued
dependencies.
If the join of R1 and R2 over C is equal to relation R then we can say that a join
dependency (JD) exists, where R1 and R2 are the decomposition R1(A, B, C) and
R2(C, D) of a given relations R (A, B, C, D).
But if breaking down the table doesn’t lead to information loss and by using the
decomposed table we can still verify all the facts about the data then we must
decompose the relation.
For a relation R(A,B,C), if there is a multi-valued dependency between, A & B and A
& C where B and C are independent of each other then 4th Normal Form will be
applied.
Lossless Decomposition
⚫ If the information is not lost from the relation that is decomposed, then the
decomposition will be lossless.
⚫ The lossless decomposition guarantees that the join of relations will result in the
same relation as it was decomposed.
⚫ The relation is said to be lossless decomposition if natural joins of all the
decomposition give the original relation.
Example of lossless join:
After decomposition:
Example of lossy join:
Spurious Tuples
Rule for decomposing the relation:
Common attribute should be Candidate key or super key of either R1 or R2 or both.