Key in Dbms
Key in Dbms
Key in Dbms
Employee table
Super keys:
{Emp_SSN}
{Emp_Number}
{Emp_SSN, Emp_Number}
{Emp_SSN, Emp_Name}
{Emp_SSN, Emp_Number, Emp_Name}
{Emp_Number, Emp_Name}
All of the above sets are able to uniquely identify rows of the employee
table.
Candidate key
Candidate keys are selected from the set of super keys, the only thing
we take care while selecting candidate key is: It should not have any
redundant attribute. That’s the reason they are also termed as minimal
super key.Minimal suerkey is known as candidate key.
Minimal super keys with no redundant attributes.
{Emp_SSN}
{Emp_Number}
Only these two sets are candidate keys as all other sets are having redundant
attributes that are not necessary for unique identification.
Primary Key
Example:
Student Table
101 Steve 23
102 John 24
103 Robert 28
104 Carl 22
In the above Student table, the Stu_Id column uniquely identifies each row of
the table.
Foreign keys
Foreign keys are the columns of a table that points to the primary key of another
table. They act as a cross-reference between tables.
For example:
In the below example the Stu_Id column in Course_enrollment table is a foreign key as
it points to the primary key of the Student table.
Course_enrollment table:
Course_Id Stu_Id
C01 101
C02 102
C03 101
C05 102
C06 103
C07 102
Student table:
Note: Practically, the foreign key has nothing to do with the primary key tag of
another table, if it points to a unique column (not necessarily a primary key) of
another table then too, it would be a foreign key. So, a correct definition of
foreign key would be: Foreign keys are the columns of a table that points to the
candidate key of another table.
Composite key
A key that consists of more than one attribute to uniquely identify rows (also
known as records & tuples) in a table is called composite key. It is also known as
compound key.
Out of all candidate keys, only one gets selected as primary key, remaining keys are known as
alternative or secondary keys.
Example: