You are on page 1of 61

Functional Dependencies

1
• Functional Dependency (FD) is a constraint that determines
the relation of one attribute to another attribute in a
Database Management System (DBMS).

• Functional Dependency helps to maintain the quality of data


in the database.

• Functional Dependency helps to find the difference between


good and bad database design.

2
• A functional dependency is denoted by an arrow "→".

• The functional dependency of X on Y is represented by X → Y.

3
• SID → SNAME
SID SNAME SID SNAME
1 SAI 1 SAI
2 SAI 1 SAI

SID SNAME
SID SNAME
1 SAI
1 SAI
1 VARUN
2 VARUN

4
Rules of Functional Dependencies
• Reflexive rule –. If X is a set of attributes and Y is subset of X,
then X holds a value of Y.

• Augmentation rule: When x -> y holds, and c is attribute set,


then ac -> bc also holds. That is adding attributes which do
not change the basic dependencies.

• Transitivity rule: If x -> y holds and y -> z holds, then x -> z


also holds.

5
• Types of Functional Dependencies in DBMS
• There are mainly four types of Functional Dependency in
DBMS. Following are the types of Functional Dependencies in
DBMS:
• Multivalued Dependency
• Trivial Functional Dependency
• Non-Trivial Functional Dependency
• Transitive Dependency

6
• Multivalued Dependency in DBMS
• Multivalued dependency occurs in the situation where there
are multiple independent multivalued attributes in a single
table.
• A multivalued dependency is a complete constraint between
two sets of attributes in a relation. It requires that certain
tuples be present in a relation.
Example

Sid Sname Cid

• This dependence can be represented like this:


• sid-> Sname 7
Trivial Functional Dependency in DBMS
• The Trivial dependency is a set of attributes which are called a
trivial if the set of attributes are included in that attribute.
• So, X -> Y is a trivial functional dependency if Y is a subset of
X.

Example
Sid Sname

• This dependence can be represented like this:


• {Sid, Sname}-> Sid is a trivial functional dependency as Sid is a
subset of {Sid ,Sname}.
8
Non Trivial Functional Dependency in DBMS
• Functional dependency which also known as a nontrivial
dependency occurs when A->B holds true where B is not a subset of
A. In a relationship, if attribute B is not a subset of attribute A, then
it is considered as a non-trivial dependency.

Example:
Dept HoD

• (Dept} -> {HoD} (if we know the Dept, we knows the HoD name)
• But HoD is not a subset of Dept, and hence it's non-trivial functional
dependency.

9
• Transitive Dependency in DBMS
• A Transitive Dependency is a type of functional dependency which
happens when t is indirectly formed by two functional
dependencies.
• Example

Dept HoD Dept_phone


• {Dept} -> {HoD} (if we know the Dept, we know its HoD’s name)
• {HoD} -> {Dept_phone} If we know the HoD, we know the
Dept_phone
• Therefore according to the rule of rule of transitive dependency:
• {Dept} -> {Dept_phone} should hold, that makes sense because if
we know the Dept name, we can know dept Dept_phone.

10
Inference Rule (IR):
• The Armstrong's axioms are the basic inference rule.
• The inference rule is a type of assertion. It can apply to a set of
FD(functional dependency) to derive other FD.
• Using the inference rule, we can derive additional functional dependency
from the initial set.

11
Reflexive Rule (IR1)
• In the reflexive rule, if Y is a subset of X, then X determines Y.
• If X ⊇ Y then X → Y

• Example:
For R(ABC), if AB → B then A → B

12
Augmentation Rule (IR2)
• The augmentation is also called as a partial dependency. In augmentation,
if X determines Y, then XZ determines YZ for any Z.
If X → Y then XZ → YZ

• Example:
For R(ABC), if A → B then AC → BC

13
Transitive Rule (IR3)

• In the transitive rule, if X determines Y and Y determine Z, then X must


also determine Z.

• If X → Y and Y → Z then X → Z

• Example:
For R(ABC), if A → B, B → C then A → C

14
Union Rule (IR4)
• Union rule says, if X determines Y and X determines Z, then X must also
determine Y and Z.
• If X → Y and X → Z then X → YZ

• Example:
For R(ABC), if A → B, A → C then A → BC

• 1. A → B(given)
2. A → C(given)
3. A → AB (using IR2 on 1 by augmentation with A)
4. AB → BC (using IR2 on 2 by augmentation with Y)
5. A → BC (using IR3 on 3 and 4)

15
Decomposition Rule (IR5)
• Decomposition rule is also known as project rule. It is the reverse of union
rule.
• This Rule says, if X determines Y and Z, then X determines Y and X
determines Z separately.
• If X → YZ then X → Y and X → Z
• Example:
For R(ABC), if A → BC then A → B, A → C
• Proof:
• 1. A → BC (given)
2. BC → B (using IR1 Rule)
3. A → B (using IR3 on 1 and 2)

• 1. A → BC (given)
2. BC → C (using IR1 Rule)
16
3. A → C (using IR3 on 1 and 2)
Pseudo transitive Rule (IR6)
• In Pseudo transitive Rule, if X determines Y and YZ determines
W, then XZ determines W.
• If X → Y and YZ → W then XZ → W
• Example:
For R(ABCD), if A → B, BD → C then AD → C
• Proof:
• 1. A → B (given)
2. BD → C (given)
3. AD→ BD (using IR2 on 1 by augmenting with W)
4. AD → C (using IR3 on 3 and 2)

17
• Example: R(A, B, C, D)
• FD={A→B; B→C; C→D}
• Find candidate key?
• CK={} + A B+ C+ D+ AB+
A+ = {} B+ = {} C+ = D+ = {}
={A} ={B} {} ={D}
={A B} // ={B
A→B C}//B→C ={C}
={ A B C}// ={B C D} ={C
//C→ D
B→C D}
={A B C D}//
C→D

• CK={A }
• AB+ , AC+, AD+ , BC+ ,BD+, CD+ , ABC+, ABD+ , ACD+ ,BCD+ , ABCD+

A+ , AB+ , AC+, AD+ , ABC+, ABD+ , ACD+, ABCD+ -- Following attributes can visit all
attributes //All these are part of keys

18
• Example: R(A, B, C, D)
• FD={A→B; B→C; C→D}
• Find candidate key?
• CK={}

19
• Example: R(A, B, C, D)
• FD={A→B; B→C; C→D;D-> A}
• Find candidate key?

20
• Example: R(A, B, C, D,E)
• FD={A→B; BC→D; E→C;D-> A}
• Find candidate key?

21
• Given the following relation instance.
-------
X Y Z
-------
1 4 2
1 5 3
1 6 3
3 2 2
-------
• Which of the following functional dependencies are satisfied by the
instance?
(a) XY -> Z and Z -> Y
(b) YZ -> X and Y -> Z
(c) YZ -> X and X -> Z
(d) XZ -> Y and Y -> X
• Given the following relation instance.
-------
X Y Z
-------
1 4 2
1 5 3
1 6 3
3 2 2
-------
• Which of the following functional dependencies are satisfied by the
instance?
(a) XY -> Z and Z -> Y
(b) YZ -> X and Y -> Z
(c) YZ -> X and X -> Z
(d) XZ -> Y and Y -> X
• Consider the relation scheme R=(E,F,G,H,I,J,K,L,M,N) and the
set of functional dependencies
{{E,F}→{G},{F}→{I,J},{E,H}→{K,L},{K}→{M},{L}→{N}}
on R. What is the key for R?
(a) {E,F}
(b) {E,F,H}
(c) {E,F,H,K,L}
(d) {E}
• Consider the relation scheme R=(E,F,G,H,I,J,K,L,M,N) and the
set of functional dependencies
{{E,F}→{G},{F}→{I,J},{E,H}→{K,L},{K}→{M},{L}→{N}}
on R. What is the key for R?
(a) {E,F}
(b) {E,F,H}
(c) {E,F,H,K,L}
(d) {E}
• Consider a relation scheme R = (A, B, C, D, E, H) on which the
following functional dependencies hold: {A–>B, BC–>D, E–>C,
D–>A}. What are the candidate keys of R?
(a) AE, BE
(b) AE, BE, DE
(c) AEH, BEH, BCH
(d) AEH, BEH, DEH
• Consider a relation scheme R = (A, B, C, D, E, H) on which the
following functional dependencies hold: {A–>B, BC–>D, E–>C,
D–>A}. What are the candidate keys of R?
(a) AE, BE
(b) AE, BE, DE
(c) AEH, BEH, BCH
(d) AEH, BEH, DEH
Update marks=80 student
where sid=1 and cname=c1;

Normalization

• Normalization is the process of organizing the data in the database.

• Normalization is the process of minimizing redundancy from a relation or


set of relations. Redundancy in relation may cause insertion, deletion and
updation anomalies.

• Normalization divides the larger table into the smaller table and links them
using relationship.

• The normal form is used to reduce redundancy from the database table.

28
Other advantages of normalization
• This technique makes it possible to make your databases more logical
and natural, reducing their size and simplifying the structure to make
product data easier to locate, contrast and retrieve.

• Increased security

29
When not to use normalization:

• When organization use NoSQL databases.

• Whether organization don’t have the right resources (since it’s a demanding
process that will need full time dedication for a while).

• If organization work with very complex designs which don’t fit well with a
standard normalization process.

30
Objectives of database normalization
• To correct duplicate data and database anomalies.

• To avoid creating and updating any unwanted data connections and


dependencies.

• To prevent unwanted deletions of data.

• To optimize storage space.

• To reduce the delay and complexity of checking databases when new types of
data need to be introduced.

• To facilitate the access and interpretation of data to users and applications


that make use of the databases.

31
Database Normal Forms
• List of Normal Forms
– 1NF (First Normal Form)
– 2NF (Second Normal Form)
– 3NF (Third Normal Form)
– BCNF (Boyce-Codd Normal Form)
– 4NF (Fourth Normal Form)
– 5NF (Fifth Normal Form)

32
First Normal Form (1NF )
• If a relation contain composite or multi-valued attribute, it violates first
normal form.

• A relation is in first normal form if every attribute in that relation is singled


valued attribute.

• Rules:
– Each table cell should contain a single value.
– Each record needs to be unique.

33
Employee Table:

EMP_ID EMP_NAME EMP_PHONE

10458 Krishna 8798754515, 9064738238

10913 Praveen 9440615975


15612 Vani 7755143215, 9988451231

34
Employee Table:
EMP_ID EMP_NAME EMP_PHONE

10458 Krishna 8798754515, 9064738238

10913 Praveen 9440615975


15612 Vani 7755143215, 9988451231

EMP_ID EMP_NAME EMP_PHONE

10458 Krishna 8798754515

10458 Krishna 9064738238

10913 Praveen 9440615975


15612 Vani 7755143215
15612 Vani 9988451231
35
Ex-2: Student

ID Name Courses
16014451 Ramakanth C1, C2
16014452 Sneha C3
16014453 Sowmya C2, c3

ID Name Courses
16014451 Ramakanth C1
16014451 Ramakanth C2
16014452 Sneha C3
16014453 Sowmya C2
16014453 Sowmya C3
36
Second Normal Form (2NF )
• For a table to be in the Second Normal Form, it must satisfy two
conditions:
– The table should be in the First Normal Form.
– There should be no Partial Dependency.
• (OR) all non-key attributes are fully functional dependent on the primary key

37
What is Dependency?

• Student

student_id name branch address

38
What is Partial Dependency?
• For a simple table like Student.
– student_id
– name
– branch
– address

39
What is Partial Dependency?
• Let's assume another table for Subject.
– subject_id
– subject_name

• Lets assume Student table


– student_id
– subject_id
– Marks
– teacher

40
student_id subject_id marks teacher

student_id + subject_id forms a Candidate Key

41
Student_id, subject_id → marks
Student_id, subject_id → teacher
subject_id → teacher

student_id subject_id marks teacher


10 1 70 RSK
10 2 75 DSY
11 1 80 RSK

subject_id subject_name
1 Java
2 C++
3 Php

42
• How to remove Partial Dependency?

subject_id subject_name teacher

student_id subject_id marks

43
• R(A,B,C,D,E,F)
• FD:{A—>B, B-> C, C->D, D->E}
• Check whether given table is in 2nd NF?
Ans: ={AFBCDE}

CK={AF}
Prime attributes = {A, F}
Non-prime attributes = {B, C, D, E}

Partial dependency: There should be no proper subset of CK ➔ non-prime attributes


• AF→B; AF->C; AF->D; AF->E
• FD:{A—>B, B-> C, C->D, D->E}
• A—>B
• B is dependent on A and not dependent on F(, which is part of our CK{AF}).
• Then we can say that given table is not in 2NF, because B is partially dependent on the
CK

44
• R(A,B,C,D)
• FD:{A—>B, B-> C}

AD-> B; AD->C

Original FD which is violating 2NF: A->B

45
• R(A,B,C,D)
• FD:{A—>B, C-> D}
• AC+ = {ACBD}
• CK={AC}
• FD{AC->B,AC->D}

46
3NF(Third normal forms)

• A relation is in third normal form, if there is no transitive dependency for


non-prime attributes as well as it is in second normal form.

• A relation is in 3NF if at least one of the following condition holds in every


non-trivial function dependency X –> Y
• X is a super key.
• Y is a prime attribute (each element of Y is part of some candidate key).

47
SID SNAME S_City S_State Age

SID-> SNAME; SID->S_CITY; SID->Age; S_City->S_state

• S_City->S_state A relation is in 3NF if at least one


(X is a super key.; of the following condition
Y is a prime attribute ) holds in every non-trivial
function dependency X –> Y
SID+ = {SID,SNAME, S_CITY, AGE, S_State} *** • X is a super key.
SNAME + = {SNAME} • Y is a prime attribute (each
S_CITY + = {S_CITY, S_state} element of Y is part of some
S_state + = {S_state} candidate key).
Age + = {Age}

CK={SID}
Prime attributes={SID}
48
Non-Prime attributes={SNAME,S_City,S_state,Age}
• R(A,B,C,D)
• FD:{A—>B, B-> C,C->D}
• CK={A}
• Prime Attribute{A}
• Non-prime attributes={B,C,D}
FD’s Verification Valid?
A—>B X –> Y Yes
• X is a super key.
• Y is a prime attribute
B-> C X –> Y • Not valid, there is TD
• X is a super key.
• Y is a prime attribute
C->D X –> Y • Not valid, there is TD
• X is a super key.
• Y is a prime attribute 49
• R(A,B,C,D)
• FD:{A—>B, B-> C,C->D}
• CK={A}
FD’s Verification Valid?

FD1: X –> Y CK={B} Yes


B-> CD • X is a super key.
• Y is a prime attribute
FD1 : X –> Y CK={B} Not
• X is a super key. satsisfy
C->D • Y is a prime attribute ing
FD2 : X –> Y CK={C} Yes
• X is a super key.
C->D • Y is a prime attribute
FD3 : X –> Y CK={A} Yes
• X is a super key.
A->B • Y is a prime attribute

50
• R(A,B,C,D) FD’s Verification Valid? Tables
• FD:{A—>B, B-> C,C->D} FD11: X –> Y CK={C} Yes R11 =
C->D • X is a super key. {C,D}
• CK={A} • Y is a prime attribute
FD’s Verification Valid? FD12 : X –> Y CK={B} Yes R12 =
• X is a super key. {B,C}
X –> Y CK={B} Yes B->C • Y is a prime attribute
FD1:
B-> CD • X is a super key.
FD2 : X –> Y CK={C} Yes R2 =
• Y is a prime attribute
• X is a super key. {C,D}
X –> Y CK={B} Not C->D • Y is a prime attribute
FD1 :
• X is a super key. satisfyi
C->D • Y is a prime attribute ng FD3 : X –> Y CK={A} Yes R3 =
• X is a super key. {A,B}
A->B • Y is a prime attribute

51
• R(A,B,C,D) FD’s Verification Valid? Tables
• FD:{A—>B, B-> C,C->D} FD11: X –> Y CK={C} Yes R11 =
C->D • X is a super key. {C,D}
• CK={A} • Y is a prime attribute
FD12 : X –> Y CK={B} Yes R12 =
• X is a super key. {B,C}
B->C • Y is a prime attribute
• R12 = {B,C} FD12 :B->C FD2 : X –> Y CK={C} Yes R2 =
• X is a super key. {C,D}
• R2 = {C,D} FD2 : C->D C->D • Y is a prime attribute
• R3 = {A,B} FD3 : A->B FD3 : X –> Y CK={A} Yes R3 =
• X is a super key. {A,B}
A->B • Y is a prime attribute

FD12 U FD2 U FD3 = FD

52
• Boyce-Codd Normal Form (BCNF)
• Boyce-Codd Normal Form or BCNF is an extension to the third normal
form, and is also known as 3.5 Normal Form.

• Rules for BCNF


• For a table to satisfy the Boyce-Codd Normal Form, it should satisfy the
following two conditions:
– It should be in the Third Normal Form.
– And, for any dependency A → B
• A should be a super key.

53
• Ex-1: R (A,B,C)
• FD:{A-> B; B->C;C->A}
• CK={A, C, B}
FD Rules Valid?
A-> B Any dependency A → B Yes
(CK={A, C, B})
A should be a super key.
B->C Any dependency A → B Yes
(CK={A, C, B})
A should be a super key.
C->A Any dependency A → B Yes
(CK={A, C, B})
54
A should be a super key.
• Ex-2: R=(A,B,C,D,E) ; FD={A->BCDE; BC->ACE; D->E} What is the highest NF?
• A+ ={ABCDE}
1NF 2NF 3NF BCNF
• +
BC ={BCACED}
N
• CK={A,BC}
• Prime attributes={A, B, C}

FD Rules for BCNF Valid?


A-> BCDE Any dependency A → B Yes
(CK={A,BC})
A should be a super key.
BC->ACE Any dependency A → B Yes
(CK={A,BC})
A should be a super key.
D->E Any dependency A → B No
(CK={A,BC})
A should be a super key. 55
• Ex-2: R=(A,B,C,D,E) ; FD={A->BCDE; BC->ACE; D->E} What is the highest NF?
• A+ ={ABCDE}
1NF 2NF 3NF BCNF
• +
BC ={BCACED}
N N
• CK={A,BC}
• Prime attributes={A, B, C}

FD Rules for 3NF Valid?


A-> BCDE X –> Y CK={A,BC} Yes
• X is a super key.
• Y is a prime attribute
BC->ACE X –> Y CK={A,BC} Yes
• X is a super key.
• Y is a prime attribute
D->E X –> Y CK={A,BC} No
• X is a super key.
• Y is a prime attribute

56
• Ex-2: R=(A,B,C,D,E) ; FD={A->BCDE; BC->ACE; D->E} What is the highest NF?
• A+ ={ABCDE}
1NF 2NF 3NF BCNF
• +
BC ={BCACED}
Y N N
• CK={A,BC}
• Prime attributes={A, B, C}

FD Rules for 2NF Valid?


A-> BCDE There should be no Partial Yes
Dependency.
BC->ACE There should be no Partial Yes
Dependency.
D->E There should be no Partial Yes
Dependency.

57
• Ex-3: How to decompose into BCNF?
• FD{AB->C, A->DE, B->F, F->GH, D->IJ}, R(A,B,C,D,E,F,G,H,I)
• AB+ ={ABCDEFGHIJ}
• A+ ={ADEIJ} D+ ={DIJ} B+ ={BFGH} F+ ={FGH}
ABCDEFGHIJ

R1 {ABC} R2 {DEFCGHIJ}

R21 {ADEIJ} R22{BFGH}

R211{ DIJ} R212{ AED} R221{FGH} R222{BF}

58
• Ex-3: How to decompose into BCNF?
• FD{AB->C, A->DE, B->F, F->GH, D->IJ}, R(A,B,C,D,E,F,G,H,I)
• AB+ ={ABCDEFGHIJ}
• A+ ={ADEIJ} D+ ={DIJ} B+ ={BFGH} F+ ={FGH}
ABCDEFGHIJ

R1 {ABC} R2 {DEFCGHIJ}
FD1:{AB->C}
R21 {ADEIJ} R22{BFGH}

R211{ DIJ} R212{ AED} R221{FGH} R222{BF}


FD211{D->IJ} FD212{A->DE} FD221{F->GH} FD222{B->F}

59
• Ex-3: How to decompose into BCNF?
• FD{AB->C, A->DE, B->F, F->GH, D->IJ}, R(A,B,C,D,E,F,G,H,I)
ABCDEFGHIJ

R1 {ABC} R2 {DEFCGHIJ}
FD1:{AB->C}
R21 {ADEIJ} R22{BFGH}

R211{ DIJ} R212{ AED} R221{FGH} R222{BF}


FD211{D->IJ} FD212{A->DE} FD221{F->GH} FD222{B->F}

• FD1 U FD211 U FD212 U FD221 U FD222 = FD


• Hence above decomposition satisfies 3NF and preserves lossless
decomposition and FD 60
1NF

2NF

3 NF

BCNF

61

You might also like