ch3 dbms
ch3 dbms
Unit-3
Relational
Database Design
• Example
• Consider the relation Account(account_no, balance, branch).
• account_no can determine balance and branch.
• So, there is a functional dependency from account_no to balance and branch.
• This can be denoted by account_no → {balance, branch}.
account_no balance branch
Types of Functional Dependency (FD)
• Full Functional Dependency
• In a relation, the attribute B is fully functional dependent on A if B is functionally
dependent on A, but not on any proper subset of A.
• Eg. {Roll_No, Semester, Department_Name} → SPI
• We need all three {Roll_No, Semester, Department_Name} to find SPI.
• Partial Functional Dependency
• In a relation, the attribute B is partial functional dependent on A if B is functionally
dependent on A as well as on any proper subset of A.
• If there is some attribute that can be removed from A and the still dependency holds then
it is partial functional dependancy.
• Eg. {Enrollment_No, Department_Name} → SPI
• Enrollment_No is sufficient to find SPI, Department_Name is not required to find SPI.
Types of Functional Dependency (FD)
• Transitive Functional Dependency
• In a relation, if attribute(s) A → B and B → C, then A → C (means C is transitively
depends on A via B).
Sub_Fa
c Facult Ag
Subject
y e
DS Shah 35
DBMS Patel 32
DF Shah 35
We have
A→B Transitivity
A→H
B→H rule
Closure of a set of FDs [Example]
Suppose we are given a relation schema R(A,B,C,G,H,I) and the set of functional
dependencies are:
F = (A → B, A → C, CG → H, CG → I, B → H)
▪ The functional dependency CG → HI is logical implied.
We have
CG → H
Union rule CG → HI
CG → I
Closure of a set of FDs [Example]
Suppose we are given a relation schema R(A,B,C,G,H,I) and the set of functional
dependencies are:
F = (A → B, A → C, CG → H, CG → I, B → H)
▪ The functional dependency AG → I is logical implied.
We have
A→C Pseudo-transiti
AG → I
CG → I vity rule
Closure of a set of FDs [Example]
Suppose we are given a relation schema R(A,B,C,G,H,I) and the set of functional
dependencies are:
F = (A → B, A → C, CG → H, CG → I, B → H)
▪ The functional dependency AG → I is logical implied.
We have
AG →
A→C Augmentation rule
CG
AG →
CG Transitivity rule AG → I
CG → I
Closure of a set of FDs [Example]
Suppose we are given a relation schema R(A,B,C,G,H,I) and the set of functional
dependencies are:
F = (A → B, A → C, CG → H, CG → I, B → H)
▪ Find out the closure of F.
F+ = (A → BC, CD → EF, A → E, AD → E, AD
→ F)
Closure of a set of FDs [Example]
Compute the closure of the following set F of functional dependencies FDs for relational
schema R = (A,B,C,D,E):
F = (AB → C, D → AC, D → E )
▪ Find out the closure of F.
F+ = (D → A, D → C, D → ACE)
Closure of attribute sets
Section – 3
What is a closure of attribute sets?
• Given a set of attributes α, the closure of α under F is the set of attributes that
are functionally determined by α under F.
• It is denoted by α+.
What is a closure of attribute sets?
• Given a set of attributes α, the closure of α under F is the set of attributes that
are functionally determined by α under F.
• It is denoted by α+.
Algorith
m
Algorithm to compute α+, the closure of α under F
Steps
1. result = α
2. while (changes to result) do
for each β → γ in F do
▪ begin
• if β ⊆ result then result = result U γ
• else result = result
▪ end
Closure of attribute sets [Example]
• Consider the relation schema R = (A, B, C, G, H, I).
• For this relation, a set of functional dependencies F can be given as
F = {A → B, A → C, CG → H, CG → I, B → H}
+ Step 1.
• Find out the closure of (AG) .
Algorith result = α => result = AG
m
Algorithm to compute α+, the closure of α under F A→B A ⊆ AG result = ABG
Steps
A→C A ⊆ ABG result = ABCG
1. result = α
2. while (changes to result) do CG → CG ⊆ ABCG result = ABCGH
H
CG → I CG ⊆ result = ABCGHI
for each β → γ in F do
▪ begin B → H ABCGH
B⊆ result = ABCGHI
• if β ⊆ result then result = result U ABCGHI
γ
• else result = result AG+ = ABCGHI
▪ end
Closure of attribute sets [Exercise]
• Given functional dependencies (FDs) for relational schema R = (A,B,C,D,E):
• F = {A → BC, CD → E, B → D, E → A}
• Find Closure for A
• Find Closure for CD
• Find Closure for B Answer
• Find Closure for BC
A+ = ABCDE
• Find Closure for E
CD+ = ABCDE
B+ = BD
BC+ = ABCDE
E+ = ABCDE
Canonical cover
Section – 4
What is extraneous attributes?
• Let us consider a relation R with schema R = (A, B, C) and set of functional
dependencies FDs F = { AB → C, A → C }.
• In AB → C, B is extraneous attribute. The reason is, there is another FD A →
C, which means when A alone can determine C, the use of B is unnecessary
(extra).
• An attribute of a functional dependency is said to be extraneous if we can
remove it without changing the closure of the set of functional
dependencies.
What is canonical cover?
• A canonical cover of F is a minimal set of functional dependencies equivalent
to F, having no redundant dependencies or redundant parts of
dependencies.
• It is denoted by Fc
• A canonical cover for F is a set of dependencies Fc such that
• F logically implies all dependencies in Fc and
• Fc logically implies all dependencies in F and
• No functional dependency in Fc contains an extraneous attribute and
• Each left side of functional dependency in Fc is unique.
F = {A → B, A →
Decomposition Rule C} Union Rule
Fc = {A → BC}
Algorithm to find canonical cover
• Repeat
• Use the union rule to replace any dependencies in F α1 → β1 and α1 → β2 with α1 →
β1β2
• Find a functional dependency α → β with an extraneous attribute either in α or in β
/* Note: test for extraneous attributes done using Fc, not F */
• If an extraneous attribute is found, delete it from α → β
• until F does not change
/* Note: Union rule may become applicable after some extraneous attributes have
been deleted, so it has to be re-applied */
Canonical cover [Example]
Consider the relation schema R = (A, B, C) with FDs
F = {A → BC, B → C, A → B, AB → C}
Find canonical cover.
• Combine A → BC and A → B into A → BC (Union Rule)
• Set is {A → BC, B → C, AB → C}
• A is extraneous in AB → C
• Check if the result of deleting A from AB → C is implied by the other dependencies
• Yes: in fact, B → C is already present
• Set is {A → BC, B → C}
• C is extraneous in A → BC
• Check if A → C is logically implied by A → B and the other dependencies
• Yes: using transitivity on A → B and B → C.
• The canonical cover is: A → B, B → C
Canonical cover [Example]
Consider the relation schema R = (A, B, C, D, E, F) with FDs
F = {A → BC, CD → E, B → D, E → A}
Find canonical cover.
Custome
rAno Balance Bname
• Suppose a new department (IT) has been started by the organization but initially
there is no employee appointed for that department.
• We want to insert that department detail in Emp_Dept table.
• But the tuple for this department cannot be inserted into this table as the EID will
have NULL value, which is not allowed because EID is primary key.
• This kind of problem in the relation where some tuple cannot be inserted is known as
insert anomaly.
Delete anomaly
• Consider a relation Emp_Dept(EID, Ename, City, DID, Dname, Manager) EID
as a primary key Emp_De
Enam ptDI Dnam Manage A delete anomaly exists when certain attributes
EID City
e D e r are lost because of the deletion of another
1 Raj Delhi 1 CE Shah attribute.
2 Meet Noida 1 CE Shah Want to delete (Jay)
3 Jay Gurgaon 2 IT Dave
employee's detail
• Now consider there is only one employee in some department (IT) and that
employee leaves the organization.
• So we need to delete tuple of that employee (Jay).
• But in addition to that information about the department also deleted.
• This kind of problem in the relation where deletion of some tuples can lead to
loss of some other data not intended to be removed is known as delete anomaly.
Update anomaly
• Consider a relation Emp_Dept(EID, Ename, City, Dname, Manager) EID as a primary
key
Emp_De
pt Enam Manage
EID City Dname An update anomaly exists when one or more
e r
records (instance) of duplicated data is
1 Raj Delhi CE Sah
updated, but not all.
2 Meet Noida C.E Shah
3 Jay Gurgaon Computer Shaah Want to update manager
of CE department
4 Hari Delhi IT Dave
• Suppose the manager of a (CE) department has changed, this requires that the
Manager in all the tuples corresponding to that department must be changed to
reflect the new status.
• If we fail to update all the tuples of given department, then two different records
of employee working in the same department might show different Manager lead
to inconsistency in the database.
How to deal with insert, delete and update anomaly
Emp_De Em Dept
pt Enam DI Dnam Manage p
EI Enam DI DI Dnam Manage
EID City City
e D e r D e D D e r
1 Raj Delhi 1 CE Shah 1 Raj Delhi 1 1 CE Shah
2 IT Dave
2 Meet Noida 1 C.E Shah 2 Meet Noida 1
3 Jay Gurgaon 2 IT Dave 3 Jay Gurgao 2 3 EC NULL
n
NUL NULL NULL 3 EC NULL
L
• What we do in normalization?
• Normalization generally involves splitting an existing table into multiple (more than
one) tables, which can be re-joined or linked each time a query is issued (executed).
How many normal forms are there?
• Normal forms:
• 1NF (First normal form)
• 2NF (Second normal form)
• 3NF (Third normal form)
• BCNF (Boyce–Codd normal form)
• 4NF (Forth normal form)
• 5NF (Fifth normal form)
As we move from 1NF to 5NF number of tables and complexity increases but
redundancy decreases.
Normal forms
1NF (First Normal Form)
Section – 7.1
1NF (First Normal Form)
• Conditions for 1NF
Each cells of a table should contain a single
value.
• A relation R is in first normal form (1NF) if and only if it does not contain any
composite attribute or multi-valued attributes or their combinations.
OR
• A relation R is in first normal form (1NF) if and only if all underlying domains
contain atomic values only.
1NF (First Normal Form) [Example - Composite attribute]
Custome
r
CID Name Address
• In customer relation address is composite attribute
C01 Raju Mathura Road, Delhi
which is further divided into sub-attributes as “Road” and
C02 Mitesh Nehru Road, Mathura
“City”.
C03 Jay C.G Road, Faridabad • So customer relation is not in 1NF.
• Solution: Split the table into two tables in such as way that
• the first table contains all attributes except multi-valued attribute with same primary
key and
• second table contains multi-valued attribute and place a primary key in it.
• insert the primary key of first table in the second table as a foreign key.
Normal forms
2NF (Second Normal Form)
Section – 7.2
2NF (Second Normal Form)
• Conditions for 2NF
It is in 1NF and each table should contain a single primary
key.
• Problem: For example, in case of a joint account multiple (more than one)
customers have common (one) accounts.
• If an account ’A01’ is operated jointly by two customers says ’C01’ and
’C02’ then data values for attributes Balance and BranchName will be
duplicated in two different tuples of customers ’C01’ and ’C02’.
2NF (Second Normal Form) [Example]
Custome Table-1 Table-2
r BranchNam BranchNam
CID ANO AccessDate Balance CID ANO AccessDate
ANO Balance
e e
C01 A01 01-01-2017
C01 A01 01-01-2017 50000 Delhi A01 50000 Delhi
C02 A01 01-03-2017
C02 A01 01-03-2017 50000 Delhi A02 25000 Noida
C01 A02 01-05-2017
C01 A02 01-05-2017 25000 Noida
C03 A02 01-07-2017
C03 A02 01-07-2017 25000 Noida
• Solution: Decompose relation in such a way that resultant relations do not have
any partial FD.
• Remove partial dependent attributes from the relation that violets 2NF.
• Place them in separate relation along with the prime attribute on which they are fully
dependent.
• The primary key of new relation will be the attribute on which it is fully dependent.
• Keep other attributes same as in that table with the same primary key.
Normal forms
3NF (Third Normal Form)
Section – 7.3
3NF (Third Normal Form)
• Conditions for 3NF
It is in 2NF and there is no transitive
dependency.
(Transitive dependency???) A → B & B → C then A
→C
• Problem: In this relation, branch address will be stored repeatedly for each
account of the same branch which occupies more space.
3NF (Third Normal Form) [Example]
Custome Table-1 Table-2
rAN BranchNam BranchAddres BranchNam BranchAddres AN BranchNam
Balance Balance
O e s e s O e
A01 50000 Delhi Kalawad road Delhi Kalawad road A01 50000 Delhi
A02 40000 Delhi Kalawad Road Noida C.G Road A02 40000 Delhi
A03 35000 Noida C.G Road A03 35000 Noida
A04 25000 Noida C.G Road A04 25000 Noida
• Solution: Decompose relation in such a way that resultant relations do not have
any transitive FD.
• Remove transitive dependent attributes from the relation that violets 3NF.
• Place them in a new relation along with the non-prime attributes due to which transitive
dependency occurred.
• The primary key of the new relation will be non-prime attributes due to which transitive
dependency occurred.
• Keep other attributes same as in the table with same primary key and add prime attributes
of other relation into it as a foreign key.
Normal forms
BCNF (Boyce-Codd Normal
Form)
Section – 7.4
BCNF (Boyce-Codd Normal Form)
Primary Determinan
• Conditions for BCNF Key t
Dependent
BCNF is based on the concept of a AccountNO → {Balance,
determinant.
Branch}
It is in 3NF and every determinant should be primary
key.
• A relation R is in Boyce-Codd normal form (BCNF)
• if and only if it is in 3NF and
• for every functional dependency X → Y, X should be the primary key of the table. OR
• A relation R is in Boyce-Codd normal form (BCNF)
• if and only if it is in 3NF and
• every prime key attribute is non-transitively dependent on the primary key OR
• A relation R is in Boyce-Codd normal form (BCNF)
• if and only if it is in 3NF and
• no any prime key attribute is transitively dependent on the primary key
BCNF (Boyce-Codd Normal Form) [Example]
Student FD1
RN Facult • FD1: RNO, Subject → Faculty
Subject
O y • FD2: Faculty → Subject
RN Subject Facult
101 DS Patel • So {RNO, Subject} → Subject (Transitivity
O y
102 DBMS Shah rule)
FD2
103 DS Jadeja
104 DBMS Dave In FD2, determinant is Faculty which is not a primary key. So student table is not
in BCNF.
105 DBMS Shah
102 DS Patel
Problem: In this relation one student can learn more than one subject with
101 DBMS Dave different faculty then records will be stored repeatedly for each student, language
105 DS Jadeja and faculty combination which occupies more space.
• Here, one faculty teaches only one subject, but a subject may be
taught by more than one faculty.
• A student can learn a subject from only one faculty.
BCNF (Boyce-Codd Normal Form) [Example]
Student Table-1 Table-2 • Solution: Decompose relation in
RN Facult Facult RN Facult such a way that resultant relations
Subject Subject
O y y O y
do not have any transitive FD.
101 DS Patel Patel DS 101 Patel
• Remove transitive dependent prime
102 DBMS Shah Shah DBMS 102 Shah attribute from relation that violets
103 DS Jadeja Jadeja DS 103 Jadeja BCNF.
• Place them in separate new relation
104 DBMS Dave Dave DBMS 104 Dave
along with the non-prime attribute due
105 DBMS Shah 105 Shah to which transitive dependency
102 DS Patel 102 Patel occurred.
• The primary key of new relation will
101 DBMS Dave 101 Dave
be this non-prime attribute due to
105 DS Jadeja 105 Jadeja which transitive dependency occurred.
• Keep other attributes same as in that
table with same primary key and add a
prime attribute of other relation into it
as a foreign key.
Multivalued dependency (MVD)
• For a dependency X → Y, if for a single value of X, multiple values of Y
exists, then the table may have multi-valued dependency.
Student
RN Facult
Subject
O y
101 DS Patel
101 DBMS Patel
101 DS Shah
101 DBMS Shah
• Let a relation R with attributes ABCD with FDs B → C, D → A. Find keys for
relation R.
• The core is BD. B determines C and D determines A, so BD is a key. Therefore BD is the
key.
Candidate Key is BD
Relation R is in 1NF but not 2NF. In above FDs, there is a partial dependency
(As per FD B → C, C depends only on B but Key is BD so C is partial depends on
key (BD))
(As per FD D → A, A depends only on D but Key is BD so A is partial depends on
key (BD))
Find (candidate) key & check for normal forms [Example]
Suppose you are given a relation R with four attributes ABCD. For each of the following sets
of FDs, do the following: F = (C → D, C → A, B → C)
Identify the candidate key(s) for R.
Identify the best normal form that R satisfies (1NF, 2NF, 3NF or BCNF).
Candidate Key is B
Relation R is in 2NF but not 3NF. In above FDs, there is a transitive
dependency
(As per FDs B → C & C → D then B → D so D is transitive depends on key
(B))
(As per FDs B → C & C → A then B → A so A is transitive depends on key
(B))
Find (candidate) key & check for normal forms [Example]
Suppose you are given a relation R with four attributes ABCD. For each of the following sets
of FDs, do the following: F = (A → B, BC → D, A → C)
Identify the candidate key(s) for R.
Identify the best normal form that R satisfies (1NF, 2NF, 3NF or BCNF).
Candidate Key is A
Relation R is in 2NF but not 3NF. In above FDs, there is a transitive
dependency
(As per FDs A → B & A → C then A → BC using union rule) and
(As per FDs A → BC & BC → D then A → D so D is transitive depends on key
(A))
Find (candidate) key & check for normal forms [Example]
Suppose you are given a relation R with four attributes ABCD. For each of the following sets
of FDs, do the following: F = (ABC → D, D → A)
Identify the candidate key(s) for R.
Identify the best normal form that R satisfies (1NF, 2NF, 3NF or BCNF).
3NF