Uploaded by Ansh Sharma

Keys

advertisement
Database
Management
System
Contents
Minimal sets of functional dependency
Types of Keys
How we can find the total number of candidate keys?
Minimal
sets of dependencies
functionalF dependency
is a set functional
that satisfies the
that
every dependency in E is in the closure of F
property
+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
that
every dependency in E is in the closure of F
property
+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)
AB
ABC
DAC
DE
STEP-1:
AB
ABC
DA
DC
DE
STEP-2:
A+ = ABC
A+ = A
A+ = AB
STEP-2:
(AB)+ = ABC
(AB)+ = AB
STEP-2:
D+ = DACEB
D+ = DCE
D+ = DAEBC
D+ = DABC
STEP-3:
A B
AB  C
D AE
STEP-4:
(AB)+ = ABC
A+ = ABC
B+ = B
STEP-5:
A BC
D AE
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
o
o
o
o
o
o
Primary Key
Candidate Key
Alternate Key
Super Key
Composite Key
Foreign Key
Unique Key
Primary Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
• Which is Unique & Can’t be have NULL
Value
• Is the column you choose to maintain
uniqueness in a table at row level.
• Here in Employee table we can 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
attribute.
single
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
o
o
o
No two rows can have the same primary key value.
Every row must have a primary key value.
The primary key field cannot be null.
Value in a primary key column can never be
modified or updated, if any foreign key refers to that
primary key.
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
Candidate Key
o Are individual columns in a table that qualifies for
uniqueness of each row/tuple.
o Here in Employee table EmployeeID & SSN are eligible
for a Primary Key and thus are Candidate keys.
o Candidate Keys are super keys for which no proper
subset is a super key. In other words candidate keys
are minimal super keys.
Alternate
Key
Employee
EmployeeID
EmployeeName
SSN
DeptID
DOB
o Candidate column other the Primar
y column, like if EmployeeID is
chosen for a PK then SSN would
be the Alternate key.
Employee
EmployeeID
EmployeeNa
me
SSN
DeptID
DOB
Key
o If you add any other Column / Attribute
to a Primary Key then it become a
Super Key,
like EmployeeID + EmployeeName is a
Super Key.
o Super key stands for superset of a key.
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
EmployeeID
EmployeeName
SSN
DeptID
DOB
o If a table do have a single column that qualifies
for a Candidate key, then you have to select 2
or more columns to make a row unique.
o Like if there is no EmployeeID or SSN columns,
then you can make EmployeeName +
DateOfBirth (DOB) as Composite Primary
Key. But still there can be a narrow chance of
duplicate rows.
Key
Employee
EmployeeID
EmployeeName
SSN
Department
DeptID
DeptName
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.
Key
Employee
EmployeeID
EmployeeName
SSN
EmailID
DOB
o Unique key is same as
primary key
with the difference being the existence of
null.
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.
If there is only one Candidate Key, it naturally will be designated as the
Primary Key.
o
o
If we find more than one Candidate Key, then we can designate any one of
them as Primary Key.
Practical Example
o Table R1. Let A,B,C,D,E are the attributes of this
relation.
A→BCDE (This means the attribute 'A' uniquely
determines the• other attributes B,C,D,E.)
BC→ADE (This means the attributes 'BC’
jointly determines all the other attributes A,D,E
Table – R1
in the relation.)
o Find the following:
A
–
–
–
–
Primary Key
Candidate Key
Super Key
Composite Key
B
C
D
E
Answers:
•Primary Key: A
•Candidate Key: A & BC
•Super Key: A, BC, AE, AD
& ABC
•Composite Key: BC
Finding Candidate KeysWe can determine the candidate keys of a given relation using the following
stepsStep-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
dependenciesC→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)
C→F
Determine all essential attributes of the given relation.
E→A
Essential attributes of the relation are- C and E.
EC → D
A→B
So, attributes C and E 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 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 , H } ( Using EF → G )
= { E , F , G , H , I , J } ( Using F → IJ )
= { E , F , G , H , I , J , K , L } ( Using EH → KL )
= { 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.
{ E, F } → { G }
{F}→{I,J}
{ E, H } → { K, L }
{K}→{M}
{L}→{N}
Example
Consider the relation schema R(A, B, C, D, E, H)
and the set of functional dependenciesA→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
R(A, B, C, D, E,
determine all remaining non-essential attributes.
H)
• To check, we find the closure of EH.
A→B
BC → D
E→C
D→A

 (EH)+ = EHC
R
 (EHA)+ = EHABCD = R
 (EHD)+ = EHDABC = R
 (EHBC)+ = EHBCDA = R = C.K/S.K.
 (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}
R(A, B, C, D, E,
H)
A→B
BC → D
E→C
D→A
 Let R = (A, B, C, D, E, F) be a relation scheme with the following
dependenciesC→F
E→A
EC → D
A→B
 Which of the following is a key for R?
1. CD
2. EC
3. AE
4. AC
 Also, determine the total number of candidate keys and super keys.
Thanks
Download