DBDM Unit 3
DBDM Unit 3
Syllabus
ER-to-Relational Mapping – Update anomalies-Functional Dependencies – Inference
rules-minimal cover-properties of relational decomposition- Normalization (upto
BCNF).
ER to Relational Mapping
In this section we will discuss how to map various ER model constructs to
Relational Model construct.
The SQL statement captures the information for above ER diagram as follows -
CREATE TABLE Employee( EmpID
CHAR(11), EName CHAR(30),
Salary INTEGER,
PRIMARY
KEY(EmpID))
Here the constraint is each department has at the most one manager to
manage it. Hence no two tuples can have same DeptID. Hence there can
be a separate table named Manages with DeptID as Primary Key. The
table can be defined using following SQL statement
Method 1 : All the entities in the relationship are mapped to individual tables
InventoryItem(ID ,
name)
Book(ID,Publisher)
DVD(ID, Manufacturer)
Method 2 : Only subclasses are mapped to tables. The attributes in the
superclass are duplicated in all subclasses. For example -
Book(ID,name,Publisher)
DVD(ID, name,Manufacturer)
InventoryItem(ID , name,Publisher,Manufacturer)
This method will introduce null values. When we insert a Book record in
the table, the Manufacturer column value will be null. In the same way,
when we insert a DVD record in the table, the Publisher value will be null.
R N
1 AAA
2 BBB
3 CCC
1 XXX
2 YYY
In above table for RollNumber 1 we are getting two different names - “AAA”
and “XXX”. Hence here it does not hold the functional dependency.
repeat
Use the union rule to replace any dependencies in Fc of the form
1 1 and 1 2 and 1 12
Find a functional dependency in Fc with an extraneous attribute either in
or in .
/* The test for extraneous attributes is done using
Fc, not F */ If an extraneous attribute is found,
delete it from in Fc .
until (Fc does not change)
Example 3.8.3 Consider the following functional dependencies over the
R(ABCDE) for finding set
attribute minimal cover FD = {A->C, AC->D, B->ADE}
Solution :
Step 1 : Split the FD such that R.H.S contain single attribute.
Hence we get A->C
AC-
>D B-
>A
B-
>D
B-
>E
Step 2 : Find the redundant entries and delete them. This can be done as
follows -
Department Table
Deptid Eid DeptName
D001 E001 Finance
D002 E002 Production
D003 E003 Sales
D004 E004 Marketing
D005 E005 Human Resource
The decomposition is used for eliminating redundancy.
For example : Consider following relation Schema R in which we
assume that the grade determines the salary, the redundancy is
caused
Schema R
Hence, the above table can be decomposed into two Schema S and T as
follows :
Solution :
Step 1 : Here Att(R1) ∪ Att(R2) = Att(R) i.e R1(A,B,C) ∪ R2(A,D)=(A,B,C,D) i.e
R.
Thus first condition gets satisfied.
Step 2 : Here R1 ∩ R2={A}. Thus Att(R1) ∩ Att(R2) ≠ . Here the second
condition gets satisfied.
Step 3 : Att(R1) ∩ Att(R2) -> {A}. Now (A)+={A,B,C} attributes of
R1. Thus the third condition gets satisfied.
This shows that the given decomposition is a lossless join.
Example 3.10.2 Consider the following relation R(A,B,C,D,E,F) and FDs A-
>BC, C->A,
D->E, F->A, E->D is the decomposition of R into R1(A,C,D), R2(B,C,D),
and R3(E,F,D). Check for lossless.
Solution :
nothing but R.
Step 2 : Consider R1∩ R2={CD} and R2∩R3={D}. Hence second
condition of intersection not being gets satisfied.
Step 3 : Now, consider R1(A,C,D) and R2(B,C,D). We find R1∩R2={CD}
(CD)+ = {ABCDE} attributes of R1 i.e.{A,C,D}. Hence condition 3 for
checking lossless join for R1 and R2 gets satisfied.
Step 4 : Now, consider R2(B,C,D) and R3(E,F,D) . We find R2∩R3={D}.
(D)+={D,E} which is neither complete set of attributes of R2 or R3.[Note that
F is missing for being attribute of R3].
Hence it is not lossless join decomposition. Or in other words we can say
it is a
lossy decomposition.
Example 3.10.3 Suppose that we decompose schema R=(A,B,C,D,E) into
(A,B,C) (C,D,E) Show that it is not a lossless decomposition.
Solution :Step 1 : Here we need to assume some data for the
attributes A, B, C, D, and E. Using this data we can represent the
relation as follows –
Relation R
A B C D E
a 1 x p q
b 2 x r s
Relation R1 = (A,B,C)
A B C
a 1 x
b 2 x
Relation R2 = (C,D,E)
C D E
x p q
X r s
A B C D E
a 1 x p q Here we get more rows
or tuples than original
a 1 x r s
relation R
b 2 x p q
b 2 x r s
Clearly R1 ⋈ R2 R. Hence it is not lossless decomposition.
F2 ∪ … ∪ Fm) = F.
If decomposition is not dependency-preserving, some dependency is
lost in the decomposition.
Example 3.10.4 Consider the relation R (A, B, C) for functional dependency set
{A -> B and
B -> C} which is decomposed into two relations R 1 = (A, C) and R2 = (B,
C). Then check if this decomposition dependency preserving or not.
Solution : This can be solved in following steps :
F+ = (F1 F2)+
Step 2 : We have with us the F+ ={ A->B and B->C }
Step 3 : Let us find (F1)+ for relation R1 and (F2)+ for relation R2
R1(A,C) R2(B,C)
A->A Trivial B->B Trivial
C->C Trivial C->C Trivial
A->C In (F)+A->B->C and it is B->C In (F)+ B->C and it is Non-
Nontrivial AC->AC Trivial Trivial BC->BC Trivial
A->B but is not useful as B is not We can not obtain C->B
part of R1 set
We can not obtain C->A
Step 4 : We will eliminate all the trivial relations and useless relations. Hence
we can obtain R1 and R2 as
R1(A,C) R2(B,C)
A->C Nontrivial
B->C Non-Trivial
(F1∪ F2)+ = {A->C, B->C} {A->B, B->C} i.e.(F)+
Thus the condition specified in step 1 i.e. F+=(F1 F2)+ is not true. Hence
it is not dependency preserving decomposition.
Step 3 : We will eliminate all the trivial relations and useless relations.
Hence we can obtain R1 ∪ R2 ∪ R3 as
R1(A, R2(B,C) R2(B,D)
B) A- B- B->
>B >C D D-
C- >B
>B
2 BBB 33333
3 CCC 44444
55555
As there are multiple values of phone number for sid 1 and 3, the above
table is not in 1NF. We can make it in 1NF. The conversion is as follows -
sid sname Phone
1 AAA 11111
1 AAA 22222
2 BBB 33333
3 CCC 44444
3 CCC 55555
3.11.2 Second Normal Form
Before understanding the second normal form let us first discuss the
concept of partial functional dependency and prime and non prime attributes.
Student_Course
sid sname cid cnam
e
1 AAA 101 C
2 BBB 102 C++
3 CCC 101 C
4 DDD 103 Java
This table is not in 2NF. For converting above table to 2NF we must follow
the following steps -
Step 1 : The above table is in 1NF.
Step 2 : Here sname and sid are associated similarly cid and cname
are associated with each other. Now if we delete a record with sid=2,
then automatically the course C++ will also get deleted. Thus,
sid->sname or cid->cname is a partial functional dependency, because
{sid,cid} should be essentially a candidate key for above table. Hence
to bring the above table to 2NF we must decompose it as follows :
Student
Here candidate key is
sid sname cid (sid,cid)
and
1 AAA 101 (sid,cid)->sname
2 BBB 102
3 CCC 101
4 DDD 103
Course
cid cnam
e Here candidate key is
cid
101 C
Here cid-
102 >cname
101 C
103 Java
101 1 AAA
102 2 BBB
103 3 CCC
104 4 DDD
Superkeys
{RegID}
{RegID, RollNo}
{RegID,Sname}
{RollNo,Sname}
{RegID, RollNo,Sname}
Candidate Keys
{RegID}
{RollNo}
Zip
zipcode cityname state
11111 Pune Maharashtra
22222 Surat Gujarat
33333 Chennai Tamilnadu
44444 Jaipur Rajasthan
55555 Mumbai Maharashtr
a
Example 3.11.1 Consider the relation R = {A, B, C, D, E, F, G, H, I, J}
and the set of functional dependencies F= {{A, B} C, A {D, E}, B
F, F {G, H}, D {I, J} }
1.What is the key for R ? Demonstrate it using the inference rules.
2.Decompose R into 2NF, then 3NF relations.
Solution : Let,
A DE (given)
A D, A E
As D I J, A I J
Using union rule we get
A DEIJ
As A A
we get A ADEIJ
Using augmentation rule we compute AB
AB ABDEIJ
But AB C (given)
AB ABCDEIJ
B F (given) F GH B GH (transitivity)
AB AGH is also true
Similarly AB AF ∵BF
(given) Thus now using union rule
AB ABCDEFGHIJ
AB is a key
The table can be converted to 2NF as
R1 = (A, B, C)
R2 = (A, D, E, I, J)
R3 = (B, F, G, H)
The above 2NF relations can be converted to 3NF as follows
R1 = (A, B, C)
R2 = (A, D, E)
R3 = (D, I, J)
R4 = (B, E)
R5 = (E, G, H).
University Questions
What is database normalization ? Explain the first normal form, second
normal form and third normal form. AU : May-18, Marks 13; Dec.-15, Marks 16
What are normal forms. Explain the types of normal form with an example.
Enrollment Table
sid course Teacher
1 C Ankita
1 Java Poonam
2 C Ankita
3 C++ Supriya
4 C Archana
From above table following observations can be made :
One student can enroll for multiple courses. For example student
with sid=1 can enroll for C as well as Java.
For each course, a teacher is assigned to the student.
There can be multiple teachers teaching one course for example
course C can be taught by both the teachers namely - Ankita and
Archana.
The candidate key for above table can be (sid,course), because using
these two columns we can find
The above table holds following dependencies
o (sid,course)->Teacher
o Teacher->course
The above table is not in BCNF because of the dependency teacher-
>course. Note that the teacher is not a superkey or in other words,
teacher is a non prime attribute and course is a prime attribute and
non-prime attribute derives the prime attribute.
To convert the above table to BCNF we must decompose above table
into Student and Course tables
Student
sid Teacher
1 Ankita
1 Poonam
2 Ankita
3 Supriya
4 Archana
Course
Teacher course
Ankita C
Poonam Java
Ankita C
Supriya C++
Archana C
{ABCD} = R
(AC) + = {AC}
R
There is no involvement of D on LHS of the FD rules. Hence D can not be part
of any
candidate key. Thus we obtain two candidate keys (AB)+ and (BC)+. Hence
prime attributes = {A,B,C}
Non prime attributes = {D}
Step 2 : Now, we will start checking from reverse manner, that means
from BCNF, then 3NF, then 2NF.
Step 3 : For R being in BCNF for X->Y the X should be candidate key or
super key.
From above FDs consider C->D in which C is not a candidate key or
super key. Hence given relation is not in BCNF.
Step 4 : For R being in 3NF for X->Y either i) the X should be candidate
key or super key or ii) Y should be prime attribute. (For prime and non
prime attributes refer step 1)