0% found this document useful (0 votes)
46 views47 pages

Functional Dependecy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views47 pages

Functional Dependecy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

DATABASE

MANAGEMENT

SYSTEM
Contents
 Functional Dependency

 Types of Functional Dependency

 Closure of Attribute

 Equivalence of functional dependencies

 Minimal sets of functional dependency

 Types of Keys

 How we can find the total number of candidate keys?


FUNCTIONAL DEPENDENCY

• Functional dependency is a relationship that exists when one


attribute uniquely determines another attribute. If R is a relation
having attributes X and Y, a functional dependency between the
attributes is represented as XY, which specifies Y is functionally
dependent on X .

• f(x)y X Y
• 1a a 1
• 1b b 1
• 2a c 3
a 1
FUNCTIONAL DEPENDENCY

• Functional dependency is a relationship that exists when one


attribute uniquely determines another attribute. If R is a relation
having attributes X and Y, a functional dependency between the
attributes is represented as XY, which specifies Y is functionally
dependent on X .

XR t1[a]=t4[a] X Y
YR t1[1]=t4[1] t1 a 1
t2 b 1
If t1[X]=t2[X] t3 c 3
Then t1[Y]=t2[Y] t4 a 1
• For example:
• Suppose we have a student table with attributes:
Stu_Id, Stu_Name, Stu_Age.

• Here Stu_Id attribute uniquely identifies the Stu_Name


attribute of student table because if we know the student
id we can tell the student name associated with it.
Functional dependency and can be written as :
Stu_IdStu_Name
we can say Stu_Name is functionally dependent on Stu_Id.

Formally:
If column A of a table uniquely identifies the column B of same table
then it can represented as AB (Attribute B is functionally
dependent on attribute A)
A B C D
AB 
a1 b1 c1 d1
AC 
a1 b2 c1 d2
AD  a2 b2 c2 d2
BC X a2 b2 c2 d3
BD  a3 b3 c2 d4
CD 
TYPES OF FUNCTIONAL
DEPENDENCIES
• Trivial functional dependency

• Non-trivial functional
dependency
TRIVIAL FUNCTIONAL
DEPENDENCY
• The dependency of an attribute on a set of attributes is known
as trivial functional dependency if the set of attributes includes
that attribute.

Symbolically:
• A B is trivial functional dependency if B is a subset of A.
• The following dependencies are also trivial:
• AA
• BB
• ABA
• For example:
Consider a table with two columns Student_id and Student_Name.

{Student_Id, Student_Name}  Student_Id is a trivial functional


dependency as Student_Id is a subset of {Student_Id,
Student_Name}.

• Also, Student_Id  Student_Id

AND

• Student_Name  Student_Name are trivial dependencies too.


NON-TRIVIAL FUNCTIONAL
DEPENDENCY

• If a functional dependency XY holds true where Y


is not a subset of X then this dependency is called
non trivial Functional dependency.
Example :
An employee table with three attributes: emp_id,
emp_name, emp_address.

The following functional dependencies are non-trivial:

emp_id  emp_name (emp_name is not a subset of

emp_id)
emp_id  emp_address (emp_address is not a subset of emp_id)
Completely non trivial
FD:
If a Functional dependency XY holds true where X intersection Y is Null then this
dependency is said to be completely non trivial function dependency.
FUNCTIONAL DEPENDENCIES CONT.
 We’d expect the following functional
dependencies to hold in our Student
database
 sid  sname,address
 cid  cname
 sid,cid  grade
 A functional dependency X  Y is simply a
pair of sets (of field names)
 Note:the sloppy notation A,B  C,D rather than
{A,B}  {C,D}
INFERENCE RULES
 Reflexive Rule: If XY then YX
 R(A,B,C,D)ABCAB
 Example: sname,addressaddress
 Augmentation Rule: If XY then
X,ZY,Z
 Example:As cidcname then
cid,sidcname,sid
 Transitive Rule: If XY and YZ then XZ
 Example:sid,cid  cid and
cid  cname, then sid,cid  cname
 Union or Additive Rule: If XY and XZ
then XY,Z
 Pseudo-transitive Rule: If XY and Y,ZW

then X,ZW
 Decomposition or Productive Rule: If

XYZ XY and X  Z.


CLOSURE OF A SET OF FDS
 Closure of attribute ( X+ ) is a set of attributes
which can be logically determined using X.
 R(ABCDE)

 F = { ABC, CDE, BD, EA}


 B+ = BD
 A+ = ABCDE = R
 CD+ = CDEAB = R
 E+ = EABCD = R
EXAMPLE OF CLOSURE

Consider R(ABCDEFGH) and following functional


dependency set then check whether BCDH will
belongs to the set or not?
F= { ABC, CDE, EC, DAEH, ABHBD
DHBC }

(BCD)+ = BCDEAH
EQUIVALENCE OF FUNCTIONAL DEPENDENCIES
COVER : A SET OF FUNCTIONAL
DEPENDENCIES F IS SAID TO COVER
ANOTHER SET OF DEPENDENCIES E IF EVERY
FD IN E IS ALSO IN F +.
Or we can say that E is covered by F.

Two sets of functional dependencies E and F are


equivalent if E+ = F+.
EXAMPLE
• Consider R(A,B,C,D,E,H) and 2 functional dependency sets F and G over
R.
F: AC G: ACD
ACD EAH
EAD
EH
Now check which is correct option:
(i) F covers G (ii) G covers F (iii) F is equivalent to G (iv) None of these

F+ : A+ = ACD G+ : A+ = ACD
(AC)+ = ACD E+ =
EADHC
E+ = EAHCD

(iii) F is equivalent to G.
EXAMPLE
o Let us take another example to show the
relationship between two FD sets. A relation
R2(A,B,C,D) having two FD sets
o FD1 = {A->B, B->C,A->C} and FD2 = {A->B,
B->C, A->D}

Now check which is correct option:


(i) F covers G (ii) G covers F (iii) F is equivalent
to G
(iv) None of the above
MINIMAL SETS OF FUNCTIONAL
DEPENDENCY
is a set functional dependencies F that satisfies the property
that every dependency in E is in the closure of F+of F.
•A minimal cover of a set of FDs F is a minimal
set of functional dependencies Fmin that is
equivalent to F. There can be many such
minimal covers for a set of functional
dependencies F.
is a set functional dependencies F that satisfies the property
that every dependency in E is in the closure of F+of F.
Simple properties/steps of minimal cover:
1. Right Hand Side (RHS) of all FDs should be single attribute.
2. Remove extraneous attributes.
3. Eliminate redundant functional dependencies.
EXAMPLE
o R (ABCDE) STEP-2:
A+ = ABC STEP-3:
AB A+ = A AB
ABC A+ = AB AB  C
DAC D AE
DE STEP-2:
(AB)+ = ABC
(AB)+ = AB STEP-4:
STEP-1: (AB)+ = ABC
AB A+ = ABC
ABC B+ = B
STEP-2:
DA D+ = DACEB
DC D+ = DCE STEP-5:
DE D+ = DAEBC A  BC
D+ = DABC D AE
PRACTICE QUESTION

Find a minimal cover for the following set of functional dependencies:

Relation R = (A, B, C, D, E, F)

F={A →C
AC → D
E → ADF }
WHY WE HAVE KEYS IN DB?
o A Key is an attribute or a set of attributes in a
relation that identifies a tuple (record) in a
relation.
o The keys are defined in a table to access or
sequence the stored data quickly and
smoothly.
o They are also used to create relationship
between different tables.
TYPES OF KEYS IN DATABASE

o Primary Key
o Candidate Key
o Alternate Key
o Super Key
o Composite Key
o Foreign Key
o Unique Key
PRIMARY KEY
Employee
• Which is Unique & Can’t be have
NULL Value
EmployeeID
EmployeeName
• Is the column you choose to
maintain uniqueness in a table at
SSN
row level.
DeptID
• Here in Employee table we can
DOB
choose either EmployeeID or
SSN
column for a PK.
PRIMARY KEY
o It is a candidate key that is chosen by the
database designer to identify entities with in
an entity set.
o Primary key is the minimal super keys. In the
ER diagram primary key is represented by
underlining the primary key attribute.
o Ideally a primary key is composed of only a
single attribute.
o But it is possible to have a primary key
composed of more than one attribute.
TO DEFINE A FIELD AS PRIMARY KEY,
FOLLOWING CONDITIONS HAD TO BE MET :
o No two rows can have the same primary key value.
o Every row must have a primary key value.
o The primary key field cannot be null.
o Value in a primary key column can never be modified
or updated, if any foreign key refers to that primary key.
CANDIDATE KEY
Employee o Any individual columns in a table that qualifies for
EmployeeID
uniqueness of each row/tuple.
o Here in Employee table EmployeeID & SSN are
EmployeeName
eligible for a Primary Key and thus are Candidate
SSN
keys.
DeptID o Candidate Keys are super keys for which no proper
DOB subset is a super key. In other words candidate keys
are minimal super keys.
ALTERNATE KEY
Employee
EmployeeID
o Candidate column other the Primary
EmployeeName column, like if EmployeeID is chosen
SSN for a PK then SSN would be the
DeptID Alternate key.
DOB
Employee
SUPER KEY
EmployeeID o If you add any other Column / Attribute to
a Primary Key then it become a Super Key,
EmployeeName
like EmployeeID + EmployeeName is a
SSN Super Key.
DeptID o Super key stands for superset of a key.
DOB o A Super Key is a set of one or more
attributes that are taken collectively and
can identify all other attributes uniquely.
COMPOSITE KEY
Employee o If a table do not have a single column that
EmployeeID qualifies for a Candidate key, then you have
to select 2 or more columns to make a row
EmployeeName unique.
o Like if there is no EmployeeID or SSN
SSN
columns, then you can make
DeptID EmployeeName + DateOfBirth (DOB) as
Composite Primary Key. But still there can
DOB
be a narrow chance of duplicate rows.
Employee
FOREIGN KEY
Department
EmployeeID
EmployeeName
DeptID
DeptName
SSN
DeptID
DOB

o Here in above tables DeptID of Department table


is Primary Key where as DeptID of Employee is a
Foreign key.
o It means it has referred to another table. This
concept is also know as Referential Integrity.
UNIQUE KEY
Employee
o Unique key is same as primary key with
EmployeeID
the difference being the existence of null.
EmployeeName

SSN

EmailID
DOB
SUMMARY
o Overall, Super Key is the broadest unique identifier;
Candidate Key is a subset of Super Key; and Primary
Key is a subset of Candidate Key.
o In practice, we would first look for Super Keys. Then we look
for Candidate Keys based on experience and common
sense.
o If there is only one Candidate Key, it naturally will be
designated as the Primary Key.
o If we find more than one Candidate Key, then we can
designate any one of them as Primary Key.
FINDING CANDIDATE KEYS-
We can determine the candidate keys of a given relation using the
following steps-
Step-01:
• Determine all essential attributes of the given relation.

• Essential attributes are those attributes which are not present on


RHS of any functional dependency.
• Essential attributes are always a part of every candidate key.

• This is because they can not be determined by other attributes.

Step-02:
• The remaining attributes of the relation are non-essential
attributes.
• This is because they can be determined by using essential
attributes.
Case-01:

 If all essential attributes together can determine all remaining non-essential attributes,
then-
• The combination of essential attributes is the candidate key.
• It is the only possible candidate key.

Case-02:

 If all essential attributes together can not determine all remaining non-essential
attributes, then-
• The set of essential attributes and some non-essential attributes will be the candidate
key(s).
• In this case, multiple candidate keys are possible.
• To find the candidate keys, we check different combinations of essential and non-
essential attributes.
EXAMPLE
 Let R = (A, B, C, D, E, F) be a relation with the following
dependencies-
C→F
E→A
EC → D
A→B
Which of the following is a key for R?
1. CD

2. EC

3. AE

4. AC
 Step-01: R = (A, B, C, D, E, F)
• Determine all essential attributes of the given relation. C→F
E→A
• Essential attributes of the relation are- C and E. EC → D
• So, attributes C and E will definitely be a part of every candidate key. A→B
 Step-02:
We will check if the essential attributes together can determine all remaining non-essential
attributes.
• To check, we find the closure of CE.
{ CE }+
={C,E}
= { C , E , F } ( Using C → F )
= { A , C , E , F } ( Using E → A )
= { A , C , D , E , F } ( Using EC → D )
= { A , B , C , D , E , F } ( Using A → B ) = R
EXAMPLE
 Consider the relation schema 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}

What is the key for R?


1. { E, F }

2. { E, F, H }

3. { E, F, H, K, L }

4. { E }
 Step-01:
• Determine all essential attributes of the given relation.
• Essential attributes of the relation are- E, F and H.
• So, attributes E, F and H will definitely be a part of every candidate key.
 Step-02:
• We will check if the essential attributes together can determine all remaining non-essential
attributes.
• To check, we find the closure of EFH. R(E, F, G, H, I, J, K, L, M, N)
{ EFH }+
{E,F,H}
{ E, F } → { G }
{F}→{I,J}
= { E , F , G , H } ( Using EF → G )
{ E, H } → { K, L }
= { E , F , G , H , I , J } ( Using F → IJ )
{K}→{M}
= { E , F , G , H , I , J , K , L } ( Using EH → KL )
{L}→{N}
= { E , F , G , H , I , J , K , L , M } ( Using K → M )
= { E , F , G , H , I , J , K , L , M , N } ( Using L → N ) = R

 So, EFH is the only possible candidate key of the relation.


EXAMPLE
 Consider the relation schema R(A, B, C, D, E, H)

and the set of functional dependencies-


A→B
BC → D
E→C
D→A
What are the candidate keys of R?
 Step-01:
• Determine all essential attributes of the given relation.
• Essential attributes of the relation are- E and H.
• So, attributes E and H will definitely be a part of every
candidate key.
 Step-02:
• We will check if the essential attributes together can
determine all remaining non-essential attributes.
R(A, B, C, D, E,
• To check, we find the closure of EH. H)
A→B
BC → D
E→C
R(A, B, C, D, E, H)

(EH)+ = EHC  R A→B
 (EHA)+ = EHABCD = R BC → D
 (EHD)+ = EHDABC = R E→C
 (EHBC)+ = EHBCDA = R = C.K/S.K.

D→A
(EHB)+ = EHBCDA = R = C.K.
 (EHC)+ = EHC  R

C.K. ={ EHA, EHD, EHB }


P.A. = { E,H,A,D,B}
N.P.A = R – P.A.
N.P.A.= {C}
THANKS

You might also like