Keys In DBMS
What are keys?
A set of attributes of a relation, where the values of that set of attributes allow us to uniquely identify a single tuple.
There are 5 different kinds of Keys:
- Super Key : It is a set of one or more attributes in a relation used to uniquely identify a tuple.
Ex : S-No, S-Name
- Candidate Key : A Super Key may contain irrelevant attributes. A super key without irrelevant attributes is called a ‘Candidate Key’.
Ex : S-No
Candidate Key is a Minimal Super Key
- Primary Key : The term ‘Primary Key’ can be used to denote a candidate key that is chosen by the database designer as the principal means, identifying tuples within a relation.
Ex : S-No
- Secondary Key : Any key other than the primary key is called a ‘Secondary Key’.
Ex : S-Name
- Foreign Key : Suppose there are two relations R1 and R2.
Let’s say some of the attributes are common between these two relations. Let K1 be the Primary Key in R1. Let K2 be the Primary Key in R2.
If K2 is also present in R1, it is said to be the ‘Foreign Key’ in R1 because it is the primary key in R2.
Let’s take an example:
R1: Student
S-No | S-Name | D-No |
241 | Alex | 14 |
267 | Max | 14 |
283 | Maggie | 15 |
292 | Ben | 15 |
304 | Trex | 16 |
R2 : Department
D-No | D-name |
14 | CSE |
15 | IT |
15 | IT |
16 | EEE |
17 | AI |
Here, D-No in R1 is said to be ‘Foreign Key’ because D-No in R2 is the Primary Key.
Reference :