Professional Documents
Culture Documents
National University of Modern Languages - NUML (Department of Computer Science)
National University of Modern Languages - NUML (Department of Computer Science)
National University of Modern Languages - NUML (Department of Computer Science)
Languages - NUML
(Department of Computer Science)
Mr. Muhammad Nouman Farooq
BSC-H (Computer Science)
MS (Telecomm. and Networks)
Honors:
Magna Cumm Laude Honors Degree
Gold Medalist!
Relational Keys
Lecture#3
Lecture 3: Relational Keys
Relational Keys
3
Relational Keys
4
Relational Keys
6
Super Key
7
Super Key
8
Continued…
9
Continued…
Now, Which attribute can we use that can uniquely identify any
instance of STUDENT entity?
Name?
Father’s Name?
Address?
Registration Number?
Phone No.?
Class?
Admission date?
10
Continued…
11
Continued…
Once you identified the instance, you have all its attributes
available, name, father name, everything.
The entity type EMPLOYEE and its attributes are shown graphically
in the figure 1 below, with its super key “regNo” underlined.
12
Continued…
13
Candidate Key
14
Candidate Key
It means that there are two conditions for the candidate key, One; It
identifies the entity instances uniquely, as is required in case of
super key, Secondly; It should be minimum, that is, no proper subset
of candidate key is a key.
15
Continued…
So, If we have a simple super key, that is, that consists of single/simple
attribute, it is definitely a candidate key, 100%.
However, if we have a composite super key and if we take any attribute out
of it and remaining part is not a super key anymore then that composite
super key is also a candidate key since it is minimal super key.
For example, one of the super keys that we identified from the entity
STUDENT is “regNo, name”, this super key is not a candidate key, since if
we remove the regNo attribute from this combination, name attribute
alone is not able to identify the entity instances uniquely.
16
Continued…
On the other hand, If we remove the attribute name from this composite
key then the regNo alone is sufficient to identify the instances uniquely, so
“regNo, name” does have a proper subset (regNo) that can act as a super
key; violation of second condition.
So, the composite key “regNo, name” is a super key but it is not a candidate
key.
From here we can also establish a fact that every candidate key is a super
key but not the other way round.
17
Primary Key
18
Primary Key
An entity type may have more than one candidate keys, in that case
the database designer has to designate one of them as primary key,
since there is always only a single primary key in an entity type.
If there is just one candidate key then obviously the same will be
declared as primary key. The primary key can also be defined as the
successful candidate key.
19
Continued…
20
Continued…
The one that we choose will be declared as Primary Key, other will
be the alternate key. Any of the candidate keys can be selected as
primary key, it mainly depends on the database designer which
choice he/she makes.
21
Continued…
There are certain things that are generally considered while making
this decision, like the candidate key that is shorter, easier to
remember, to type and is more meaningful is selected as primary
key.
22
Continued…
The relation that holds between super and candidate keys also
holds between candidate and primary keys, that is, every primary
key (PK) is a candidate key and every candidate key is a super key.
23
Unique Key
24
Unique Key
A candidate key which can return a Record uniquely but may store a
NULL value is called as Unique Key.
25
Alternate Key
26
Alternate Key
Candidate keys which are not chosen as the primary key are known as
alternate keys.
27
Foreign Key
28
Foreign Key
29
Continued…
31
Continued…
As the figure shows, there may well be some students who are not
referenced from Enrolled
(e.g., the student with sId=50000)
However, every sId value that appears in the instance of the Enrolled
table appears in the primary key column of a row in the Students
table.
32
Continued…
If we try to insert the tuple (55555, Art104, A) into E1, the rule is
violated because there is no tuple in S1 with the id 55555; the
database system should reject such an insertion.
The DBMS should disallow the deletion or, perhaps, also delete the
Enrolled tuple that refers to the deleted Students tuple.
33
Continued…
34
Secondary Key
35
Secondary Key
36
Continued…
37
Continued…
Keep one thing in mind here, that a particular access on the value of
a secondary key MAY return a single instance, but that will be
considered as chance.
38
Continued…
39
Surrogate Key
40
Surrogate Key:
A Surrogate Key is any column or set of
columns that can be declared as the primary key instead of more
than two composite Primary keys that jointly makes a Cumbersome
key (CUMBERSOME meaning: Large Set).
43
Natural Key:
A Natural Key is Unique and Not Null able
key that is formed/made by Nature.
46
Class Activity (Solution)
47
Recommended Readings
Chapter 5 from:
Chapter 3 from:
48
Summary of Lecture
➦ Lecture 3
Relational Keys
49
END OF LECTURE 3
50