Functional Dependecy
Functional Dependecy
MANAGEMENT
SYSTEM
Contents
Functional Dependency
Closure of Attribute
Types of Keys
• f(x)y X Y
• 1a a 1
• 1b b 1
• 2a c 3
a 1
FUNCTIONAL DEPENDENCY
XR t1[a]=t4[a] X Y
YR 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.
Formally:
If column A of a table uniquely identifies the column B of same table
then it can represented as AB (Attribute B is functionally
dependent on attribute A)
A B C D
AB
a1 b1 c1 d1
AC
a1 b2 c1 d2
AD a2 b2 c2 d2
BC X a2 b2 c2 d3
BD a3 b3 c2 d4
CD
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:
• AA
• BB
• ABA
• For example:
Consider a table with two columns Student_id and Student_Name.
AND
emp_id)
emp_id emp_address (emp_address is not a subset of emp_id)
Completely non trivial
FD:
If a Functional dependency XY 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 XY then YX
R(A,B,C,D)ABCAB
Example: sname,addressaddress
Augmentation Rule: If XY then
X,ZY,Z
Example:As cidcname then
cid,sidcname,sid
Transitive Rule: If XY and YZ then XZ
Example:sid,cid cid and
cid cname, then sid,cid cname
Union or Additive Rule: If XY and XZ
then XY,Z
Pseudo-transitive Rule: If XY and Y,ZW
then X,ZW
Decomposition or Productive Rule: If
(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.
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}
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
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.
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}
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