What are Keys?
A DBMS key is an attribute or set of an attribute which helps us to identify a row(tuple) in a relation(table).
They allow you to find the relation between two tables. Keys help us uniquely identify a row in a table by a
combination of one or more columns in that table.
Super Key
Super Key is defined as a set of attributes within a table that can uniquely identify each record within a
table.
Let's take a simple Student table, with fields student_id, name, phone and age.
In the table defined above super key would include student_id, (student_id, name), phone etc.
Candidate Key
The candidate keys in a table are defined as the set of keys that is minimal and can uniquely identify any
data row in the table.
In our example, student_id and phone both are candidate keys for table Student.
 A candiate key can never be NULL or empty. And its value should be unique.
 There can be more than one candidate keys for a table.
 A candidate key can be a combination of more than one columns(attributes).
Primary Key
Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key
that can uniquely identify each record in a table.
The Primary Key in <Subject> table is {Subject_Number}
Foreign Key
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.
Student ExamResult
Example
The Super Keys in <Student> table are:
{Student_Number}
{Student_Phone}
{Student_Number,Student_Name}
{Student_Number,Student_Phone}
{Student_Number,Subject_Number}
{Student_Phone,Student_Name}
{Student_Phone,Subject_Number}
{Student_Number,Student_Name,Student_Phone}
{Student_Number,Student_Phone,Subject_Number}
{Student_Number,Student_Name,Subject_Number}
{Student_Phone,Student_Name,Subject_Number}
The Candidate Key in <Student> table is {Student_Number} or {Student_Phone}
The Candidate Key in <Student> table is {Student_Number, Subject_Number}
The Primary Key in <Student> table is {Student_Number}
{Subject_Number} is the Foreign Key of <Student> table and Primary key of <Subject> table.
Comparison between Primary key and Foreign Key
BASIS FOR
COMPARISON
PRIMARY KEY FOREIGN KEY
Basic Primary Key is a chosen candidate key
that uniquely defines a tuple in a
relation.
Foreign key in a table refers to the primary
key of other table.
NULL Primary key value can never be NULL. Foreign key accepts NULL value.
Range There can be only one primary key of a
relation.
There can be multiple foreign keys in a
relation.
Insertion We can insert a value to a primary key
attribute, even if the referencing foreign
key does not have that value in its
column.
We can not insert a value to a foreign key,
if that value is not present in the referenced
primary key column.
Deletion Before you delete a primary key value,
make sure that value is not still present
in the referencing foreign key column of
referencing table.
You can delete a value from foreign key
column without bothering, whether that
value is present in referenced primary key
column of referenced relation.
Difference between Super Key and Candidate Key
S.NO SUPER KEY CANDIDATE KEY
1.
Super Key is an attribute (or set of
attributes) that is used to uniquely identifies
all attributes in a relation.
Candidate Key is a proper subset of
a super key.
2. All super keys can’t be candidate keys.
But all candidate keys are super
keys.
3.
Various super keys together makes the
criteria to select the candidate keys.
Various candidate keys together
makes the criteria to select the
primary keys.
4.
In a relation, number of super keys are more
than number of candidate keys.
While in a relation, number of
candidate keys are less than number
of super keys.
5.
Super key’s attributes can contain NULL
values.
Candidate key’s attributes can also
contain NULL values.
A. S. M. Shafi
Lecturer
Department of Computer Science and Engineering
Khwaja Yunus Ali University
Enaytpur, Sirajgonj-6751, Bangladesh

Keys in Database

  • 1.
    What are Keys? ADBMS key is an attribute or set of an attribute which helps us to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables. Keys help us uniquely identify a row in a table by a combination of one or more columns in that table. Super Key Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table. Let's take a simple Student table, with fields student_id, name, phone and age. In the table defined above super key would include student_id, (student_id, name), phone etc. Candidate Key The candidate keys in a table are defined as the set of keys that is minimal and can uniquely identify any data row in the table. In our example, student_id and phone both are candidate keys for table Student.  A candiate key can never be NULL or empty. And its value should be unique.  There can be more than one candidate keys for a table.  A candidate key can be a combination of more than one columns(attributes). Primary Key Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key that can uniquely identify each record in a table. The Primary Key in <Subject> table is {Subject_Number}
  • 2.
    Foreign Key Foreign keysare the columns of a table that points to the primary key of another table. They act as a cross- reference between tables. Student ExamResult
  • 3.
    Example The Super Keysin <Student> table are: {Student_Number} {Student_Phone} {Student_Number,Student_Name} {Student_Number,Student_Phone} {Student_Number,Subject_Number} {Student_Phone,Student_Name} {Student_Phone,Subject_Number} {Student_Number,Student_Name,Student_Phone} {Student_Number,Student_Phone,Subject_Number} {Student_Number,Student_Name,Subject_Number} {Student_Phone,Student_Name,Subject_Number} The Candidate Key in <Student> table is {Student_Number} or {Student_Phone} The Candidate Key in <Student> table is {Student_Number, Subject_Number} The Primary Key in <Student> table is {Student_Number}
  • 4.
    {Subject_Number} is theForeign Key of <Student> table and Primary key of <Subject> table. Comparison between Primary key and Foreign Key BASIS FOR COMPARISON PRIMARY KEY FOREIGN KEY Basic Primary Key is a chosen candidate key that uniquely defines a tuple in a relation. Foreign key in a table refers to the primary key of other table. NULL Primary key value can never be NULL. Foreign key accepts NULL value. Range There can be only one primary key of a relation. There can be multiple foreign keys in a relation. Insertion We can insert a value to a primary key attribute, even if the referencing foreign key does not have that value in its column. We can not insert a value to a foreign key, if that value is not present in the referenced primary key column. Deletion Before you delete a primary key value, make sure that value is not still present in the referencing foreign key column of referencing table. You can delete a value from foreign key column without bothering, whether that value is present in referenced primary key column of referenced relation.
  • 5.
    Difference between SuperKey and Candidate Key S.NO SUPER KEY CANDIDATE KEY 1. Super Key is an attribute (or set of attributes) that is used to uniquely identifies all attributes in a relation. Candidate Key is a proper subset of a super key. 2. All super keys can’t be candidate keys. But all candidate keys are super keys. 3. Various super keys together makes the criteria to select the candidate keys. Various candidate keys together makes the criteria to select the primary keys. 4. In a relation, number of super keys are more than number of candidate keys. While in a relation, number of candidate keys are less than number of super keys. 5. Super key’s attributes can contain NULL values. Candidate key’s attributes can also contain NULL values. A. S. M. Shafi Lecturer Department of Computer Science and Engineering Khwaja Yunus Ali University Enaytpur, Sirajgonj-6751, Bangladesh