Unit 5 Relational Database Design
Unit 5 Relational Database Design
Prof. S.W.Thakare
Assistant Professor,
Computer science & Engineering
CHAPTER-5
• As you can see over here, for each value of Sid there is only one
Sname value. Hence, Sid → Sname is valid FD.
Types of Functional Dependency:
Trivial FD:
•For example, Consider Table 1.1. Following FDs are Trivial FDs.
• SidSname → Sname
Types of Functional Dependency:
Non- Trivial FD:
• Armstrong's axioms are a set of rules used to infer (derive) all the
functional dependencies on a relational database.
•Answer: A+ is { A, B, C, D, E)
•[AF]+ = { A, B, C, D, E, F, G}
Closure Of FD:
∙ The Closure Of Functional Dependency means the complete set of all possible attributes that
can be functionally derived from given functional dependency using the inference rules known
as Armstrong’s Rules.
∙ If “F” is a functional dependency then closure of functional dependency can be denoted using
“{F}+”.
∙ There are three steps to calculate closure of functional dependency. These are:
Step-1 : Add the attributes which are present on Left Hand Side in the original functional
dependency.
Step-2 : Now, add the attributes present on the Right Hand Side of the functional dependency.
Step-3 : With the help of attributes present on Right Hand Side, check the other attributes that can
be derived from the other given functional dependencies. Repeat this process until all the possible
attributes which can be derived are added in the closure.
Closure Of FD:
∙ Example-2 : Consider a relation R(A,B,C,D,E) having below mentioned functional
dependencies.
1) FD1 : A BC, 2) FD2 : C B 3)FD3 : D E 4)FD4 : E D
Now, we need to calculate the closure of attributes of the relation R. The closures will be:
∙ {A}+ = {A, B, C}
∙ {B}+ = {B}
∙ {C}+ = {B, C}
∙ {D}+ = {D, E}
∙ {E}+ = {E}
Closure Of FD:
Example-1 : Consider the table student_details having (Roll_No, Name,Marks, Location) as the
attributes and having two functional dependencies.
∙ FD1 : Roll_No Name, Marks
∙ FD2 : Name Marks, Location
∙ Now, We will calculate the closure of all the attributes present in the relation using the three
steps mentioned below.
∙ Step-1 : Add attributes present on the LHS of the first functional dependency to the closure.
∙ {Roll_no}+ = {Roll_No}
∙ Step-2 : Add attributes present on the RHS of the original functional dependency to the
closure.
∙ {Roll_no}+ = {Roll_No, Marks}
Closure Of FD:
Step-3 : Add the other possible attributes which can be derived using attributes present
on the RHS of the closure. So Roll_No attribute cannot functionally determine any
attribute but Name attribute can determine other attributes such as Marks and Location
using 2nd Functional Dependency(Name [icon name="long-arrow-right" class=""
unprefixed_class=""] Marks, Location).
Therefore, complete closure of Roll_No will be :
{Roll_no}+ = {Roll_No, Marks, Name, Location}
Closure Of FD:
Similarly, we can calculate closure for other attributes too i.e “Name”.
Step-1 : Add attributes present on the LHS of the functional dependency to the closure.
{Name}+ = {Name}
Step-2 : Add the attributes present on the RHS of the functional dependency to the
closure.
{Name}+ = {Name, Marks, Location}
Step-3 : Since, we don’t have any functional dependency where “Marks or Location”
attribute is functionally determining any other attribute , we cannot add more attributes to
the closure. Hence complete closure of Name would be :
{Name}+ = {Name, Marks, Location}
Closure Of FD:
NOTE : We don’t have any Functional dependency where marks and location can
functionally determine any attribute. Hence, for those attributes we can only add the
attributes themselves in their closures. Therefore,
{Marks}+ = {Marks}
and
{Location}+ = { Location}
Closure Of An Attribute:
Closure of an attribute x is the set of all attributes that are functional dependencies on X with
respect to F. It is denoted by X+ which means what X can determine.
Algorithm
Let’s see the algorithm to compute X+
∙ Step 1 − X+ =X
∙ Step 2 − repeat until X+ does not change
o For each FD Y->Z in F
▪ If Y ⊆ X+ then X+ = X+ U Z
Example 1
Consider a relation R(A,B,C,D,E,F)
F: E->A, E->D, A->C, A->D, AE->F, AG->K.
Find the closure of E or E+
Closure Of An Attribute:
Solution: The closure of E or E+ is as follows −
E+ = E
=EA {for E->A add A}
=EAD {for E->D add D}
=EADC {for A->C add C}
=EADC {for A->D D already added}
=EADCF {for AE->F add F}
=EADCF {for AG->K don’t add k AG ⊄ D+)
Example 2 Let the relation R(A,B,C,D,E,F)
F: B->C, BC->AD, D->E, CF->B. Find the closure of B.
Closure Of An Attribute:
Solution
The closure for B is as follows −
B+ = {B,C,A,D,E}
Closure is used to find the candidate keys of R and compute F+
Candidate key of R: X is a candidate keys of R if X->{R}
Super Key:
• Example: R(ABCD), F = { A → B, B → C, C → D }
• [A]+ = { A, B, C, D}
Example:
•R(ABCD), F = { AB → C, C → D, B → E }
•[AB]+ = { A, B, C, D, E}
•Hence, AB is one of the super keys.
Example:
•R(ABCDEF), F = { A → D, F → E, C → F, F → A }
•Here, B is not part of non-trivial FD set so it must be part of candidate key.
•[C]+ = { A, C, E, F}
•Candidate key: { BC }
•Prime attributes: B and C
Candidate Key:
Note 2: If some attribute X is only part of determinant side but not belongs
to any determiner of non-trivial FD set of relation R then X must be part of
Candidate key.
Example:
•R(ABCDEF), F = { AB → C, E → F, D → C, C → B }
•Here, A,E are at the determinant side only.
•Candidate key: { ABE}
•Prime attributes: A,B and E
Candidate Key:
1.R(ABCDE), F = { AB → B, BC → D, CD → E, E → A}
2. R(ABCD), F = { AB → CD, C → A, D → B}
3.R(ABCDEF), F = { AB → C, C → DE, E → F, EF → B, E → A}
4.R(ABCDEFG), F = { AB → CD, AF → D, DE → F, C → G, F → E, G → A}
Normalization:
• Normalization is the procedure to reduce the Redundancy.
S3 B 22 C1 DBMS 5000
S2 A 22 C3 DAA 7000
Redundancy
S2 A 22 C4 DS 7000
Normalization:
• Problem:
• It if difficult to retrieve the list of Customers living in ‘Vadodara’ from
above table.
• Reason is address attribute is composite attribute which contains name
of the area as well as city name in single cell.
Normalization:
1NF:
• Solution:
• Divide composite attributes into number of sub- attribute and insert
value in proper sub attribute.
Table 1.7: 1 NF Example solution
• R(ABCDE), F = { AB → C, C → A, C → D, D → E }
• Super key/Candidate key: { AB,BC}
Case 1: E.g. C → D
Proper Subset of CK (X) Non-Prime attribute (Y)
Case 2:
Non-Prime attribute (X) Non-Prime attribute (Y) E.g. D → E
1 NF 2 NF 3 NF BCNF
Case 1 Yes yes No No
Case 2 Yes Yes No No
Case 3 Yes Yes Yes no
Normalization:
2NF:
• Above relation has five attributes cid, ano, acess_date, balance, bname
and two FDS
FD1 : {cid,ano} -> {acess_date,balance,bname} and
FD2 : {ano} -> {balance,bname}
Normalization:
2NF Example:
Problem:
• For example in case of joint account multiple customers have common
accounts.
• If some account says ‘A02’ is jointly by two customers says ‘C02’ and
‘C04’ then data values for attributes balance and bname will be
duplicated in two different tuples of customers ‘C02’ and ‘C04’.
Normalization:
2NF Example:
Solution:
• Decompose relation in such a way that resultant relation does not have
any partial FD.
• For this purpose remove partial dependent attribute that violets 2NF
from relation.
Normalization:
2NF Example:
Solution:
• Place them in separate new relation along with the prime attribute on
which they are full dependent.
• The primary key of new relation will be the attribute on which it if fully
dependent.
• Keep other attribute same as in that table with same primary key.
Normalization:
3NF:
A relation schema R is in third normal form (3NF) if and only if it is in 2NF and no
transitive dependencies in R.
Normalization:
3NF Example:
• Above relation has five attributes ano, balance, bname and baddress
and two FDS
FD1 : ano -> {balance, bname, baddress} and
FD2 : bname -> baddress
• Using transitivity rule, ano -> baddress
Normalization:
3NF Example:
Problem:
• Transitively dependency results in data redundancy.
• In this relation branch address will be stored repeatedly from each
account of same branch which occupy more space.
Normalization:
3NF Example:
Table 1.11 : 3NF Solution
Solution:
Solution:
R1
Dept Subject
CSE C
CSE Java
IT C
Normalization:
R2
Dept Name
CSE Ammu
CSE Amar
IT Bhanu
R3
Subject Name
C Ammu
C Amar
Java Amar
C Bhanu
In above decomposition of table reduction of redundancy is not apparent,
Normalization:
The above relation is in 4NF. Anomalies can occur in relation in 4NF if the primary
key has three or more fields. The primary key is (dept,subject, name). Sometimes
decomposition of a relation into two smaller relations does not remove redundancy.
In such cases it may be possible to decompose the relation in three or more
relations using 5NF.
Normalization:
1.R(ABCDE), F = { ABC → D, D → A, BD → E}
Question:
Solution:
• UNF
Lecturer Number, Lecturer Name, Lecturer Grade, Department Code,
Department Name, Subject Code, Subject Name, Subject Level
• After removing multi valued attributes we get 1NF
• Lecturer Number, Lecturer Name, Lecturer Grade, Department Code,
Department Name
• Lecturer Number, Subject Code, Subject Name, Subject Level
(Partial Dependency)
Normalization:
Solution:
• After removing partial dependency we get 2NF
• Lecturer Number, Lecturer Name, Lecturer Grade, Department Code,
Department Name (Transitive Dependency)
• Lecturer Number, Subject Code
• Subject Code, Subject Name, Subject Level
• After removing transitive dependency we get 3NF
• Lecturer Number, Lecturer Name, Lecturer Grade, Department Code
• Department Code, Department Name
• Lecturer Number, Subject Code
• Subject Code, Subject Name, Subject Level
Decomposition:
• Properties of Decomposition:
• Let R be the Relational schema with FD set F Decomposed into R1, R2,
R3…… Rn sub relations with FD set F1, F2, F3,…….. Fn respectively.
R F
R1 R2 R3
(F1) (F2) (F3)
Decomposition:
Dependency Preserving Decomposition:
In general,
Example 1:
Example 1:
R2 (BC) : (F2)
[B]+ = { B, C, D, E} [ We can derive C from B. So B → C. A,D,E are not in R2]
[C]+ = { B, C, D, E} [ We cannot derive B from C. So C → B. A,D,E are not in R2]
R3 (CD) : (F3)
[C]+ = { B, C, D, E} [ We can derive D from C. So C → D. A,B,E are not in R2]
[D]+ = { B, C, D, E} [ We cannot derive C from D. So D → C. A,B,E are not in R2]
R4 (DE) : (F4)
[D]+ = { B, C, D, E} [ We can derive E from D. So D → E. A,B,E are not in R2]
[E]+ = {E} [ We cannot derive E from D.]
Decomposition:
Example 1:
{F1 ∪ F2 ∪ F3 ∪ F4 } = {A → B, B → C, C → B, C → D, D → C, D → E }
Now, F = { A → B, B → C, C → D, D → E, D → B }
Example 2:
R1 (ABC) : (F1)
[AB]+ = { A, B, C, D} [ We can derive C from A and B. So AB → C. D is not in R1]
Decomposition:
Example 2:
R2 (AD) : (F2)
[A]+ = {A} [ We cannot derive D from A.]
[D]+ = { A,D} [ We can derive A from d. So D → A. B,C are not in R2]
R3 (BCD) : (F3)
[B]+ = {B} [ We cannot derive C and D from B. A,E are not in R3]
[C]+ = {C} [ We cannot derive B and D from C. A,E are not in R3]
[D]+ = {DA} [ We cannot derive B and C from D. A,E are not in R3]
[BD]+ = { A, B, C, D} [ We can derive C from BD. A ,E is not in R3]
Decomposition:
Example 2:
{F1 ∪ F2 ∪ F3 } = {AB → C, D → A, BD → C }
• Let R be the Relational schema with FD set F Decomposed into R1, R2,
R3…… Rn sub relations.
R
Before Decomposition
After Decomposition
R1 R2 R3
Decomposition:
Lossless join Decomposition:
In general,
[R1 ⋈ R2 ⋈ R3 ⋈ …… ⋈ Rn] ⊇ R
S1 A R1⋈S1R2 C1 S1 A C1
S2 B S1 C2 S1 A C2
S3 B S2 C2 S2 B C2
S3 C3 S3 B C3
{Sid}: key
{SidCid}: key
R1⋈ R2 = R, so it is LOSSLESS JOIN DECOMPOSTION.
Decomposition:
Lossless join Decomposition:
S1 A R1⋈A R2 C1 S1 A C1
S2 B A C2 S1 A C2
S3 B B C2 S2 B C2
B C3 S2 B C3
{Sid}: key
S3 B C3
{SnameCid}: key
S3 B C2
R1⋈ R2 ⊃ R, so LOSSY JOIN DECOMPOSTION.
S3 B C3
Decomposition:
Lossless join Decomposition:
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
1)D = { ABC, CD }
R1(ABC) ∩ R2(CD) = C
[C]+ = { C,D }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
2)D = { ABC, DE }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
3)D = { ABC, CDE }
R1(ABC) ∩ R2(CDE) = C
[C]+ = { C,D }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
4)D = { ABCD, BE }
R1(ABCD) ∩ R2(BE) = B
[B]+ = { B,E }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
5)D = { ABC, ABDE }
R1(ABC) ∩ R2(ABDE) = AB
[AB]+ = { A,B,C,D,E }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
6)D = { ABC, CD, BE }
R1(ABC) ∩ R2(CD) = C
[C]+ = { C,D }
Example:
R(ABCDE), F = { AB → C, C → D, B → E}
Relation R is decomposed into,
7)D = { ABC, CD, DE }
R1(ABC) ∩ R2(CD) = C
[C]+ = { C,D }
Lossy Decomposition
As the name suggests, when a relation is decomposed into two or more relational
schemas, the loss of information is unavoidable when the original relation is
retrieved.
Let us see an example −
<EmpInfo>
Emp_ID Emp_Name Emp_Age Emp_Location Dept_ID Dept_Name
E001 Jacob 29 Alabama Dpt1 Operations
E002 Henry 32 Alabama Dpt2 HR
E003 Tom 22 Texas Dpt3 Finance
Decomposition:
Decompose the above table into two tables −
<EmpDetails>
Emp_ID Emp_Name Emp_Age Emp_Location
E001 Jacob 29 Alabama
E002 Henry 32 Alabama
E003 Tom 22 Texas
<DeptDetails>
Dept_ID Dept_Name
Dpt1 Operations
Dpt2 HR
Dpt3 Finance
Now, you won’t be able to join the above tables, since Emp_ID isn’t part of the
DeptDetails relation. Therefore, the above relation has lossy decomposition.
Anamolies:
Anomalies in the relational model refer to inconsistencies or errors that can arise when
working with relational databases, specifically in the context of data insertion, deletion,
and modification. There are different types of anomalies that can occur in referencing and
referenced relations which can be discussed as:
These anomalies can be categorized into three types:
1. Insertion Anomalies
2. Deletion Anomalies
3. Update Anomalies.
How Are Anomalies Caused in DBMS?
Database anomalies are the faults in the database caused due to poor management of
storing everything in the flat database. It can be removed with the process of
Normalization, which generally splits the database which results in reducing the
anomalies in the database.
Anomalies:
Anomalies STUD_NO STUD_NAME STUD_PHONE STUD_STATE STUD-COUNTRY STUD_AGE
STUDENT Table
1 RAM 9716271721 Haryana India 20
2 C2 Computer Networks
1 C2 Computer Networks
Anomalies:
Insertion anomaly: If a tuple is inserted in referencing relation and referencing attribute
value is not present in referenced attribute, it will not allow insertion in referencing
relation.
Example: If we try to insert a record in STUDENT_COURSE with STUD_NO =7, it
will not allow it.
Deletion and Updation anomaly: If a tuple is deleted or updated from referenced
relation and the referenced attribute value is used by referencing attribute in referencing
relation, it will not allow deleting the tuple from referenced relation.
∙ ON DELETE/UPDATE SET NULL: If a tuple is deleted or updated from
referenced relation and set the value of referencing attribute to NULL.
∙ ON DELETE/UPDATE CASCADE: If a tuple is deleted or updated from referenced
relation and the referenced attribute value is used by referencing attribute in
referencing relation, it will delete/update the tuple from referenced relation and
referencing relation as well.
References
[1] Abraham Silberschatz, Henry F. Korth and S. Sudarshan, Database System
Concepts, McGraw-Hill Education (Asia), Seventh Edition, 2019.
[2] C. J. Date, A. Kannan and S. Swamynathan, An Introduction to Database
Systems, Pearson Education, Eighth Edition, 2009.
[3] Database Management Systems, CSE, DIET,
https://www.darshan.ac.in/DIET/CE/GTU-Computer-Engineering-Study-Material
[4] Database management systems by Raghu Ramakrishnan and Johannes Gehrke
http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/slides/slides3ed.html
[5] Database management system tutorial,
https://www.tutorialspoint.com/dbms/index.htm
[6] Vishal Hatmode, Sonali Rangdale, 2014, Query Optimization Based on Heuristic
Rules, INTERNATIONAL JOURNAL OF ENGINEERING RESEARCH & TECHNOLOGY
(IJERT) Volume 03, Issue 07 (July 2014).
www.paruluniversity.ac.in