Databases 02c - Keys in RDBMS
Databases 02c - Keys in RDBMS
Databases 02c - Keys in RDBMS
For the purposes of clarity we will refer to combination of fields within the table.
keys in terms of RDBMS tables but the They help enforce integrity and help
same definition, principle and naming identify the relationship between tables.
applies equally to Entity Modelling and There are three main types of keys,
Normalisation. candidate keys, primary keys and foreign
keys. There is also an alternative key or
Keys are, as their name suggests, a key
secondary key that can be used, as the
part of a relational database and a vital
name suggests, as a secondary or
part of the structure of a table. They
alternative key to the primary key
ensure each record within a table can be
uniquely identified by one or a
Super Key
A Super key is any combination of fields within a table that uniquely identifies each record
within that table.
Candidate Key
A candidate is a subset of a super key. A candidate key is a single field or the least
combination of fields that uniquely identifies each record in the table. The least
combination of fields distinguishes a candidate key from a super key. Every table must have
at least one candidate key but at the same time can have several.
Page 1 of 4
As an example we might have a student_id that uniquely identifies the students in a student
table. This would be a candidate key. But in the same table we might have the student’s first
name and last name that also, when combined, uniquely identify the student in a student
table. These would both be candidate keys.
Once your candidate keys have been identified you can now select one to be your primary
key.
Primary Key
A primary key is a candidate key that is most appropriate to be the main reference key for
the table. As its name suggests, it is the primary key of reference for the table and is used
throughout the database to help establish relationships with other tables. As with any
candidate key the primary key must contain unique values, must never be null and uniquely
identify each record in the table.
Important:
Page 2 of 4
Foreign Key
A foreign key is generally a primary key from one table that appears as a field in another
where the first table has a relationship to the second. In other words, if we had a table A
with a primary key X that linked to a table B where X was a field in B, then X would be a
foreign key in B.
An example might be a student table that contains the course_id the student is attending.
Another table lists the courses on offer with course_id being the primary key. The 2 tables
are linked through course_id and as such course_id would be a foreign key in the student
table.
For example in the table showing candidate keys above we identified two candidate keys,
studentId and firstName + lastName. The studentId would be the most appropriate for a
primary key leaving the other candidate key as secondary or alternative key. It should be
noted for the other key to be candidate keys, we are assuming you will never have a person
with the same first and last name combination. As this is unlikely we might consider
fistName+lastName to be a suspect candidate key as it would be restrictive of the data you
might enter. It would seem a shame to not allow John Smith onto a course just because
there was already another John Smith.
Page 3 of 4
Simple Key
Any of the keys described before (ie primary, secondary or foreign) may comprise one or
more fields, for example if firstName and lastName was our key this would be a key of two
fields where as studentId is only one. A simple key consists of a single field to uniquely
identify a record. In addition the field in itself cannot be broken down into other fields, for
example, studentId, which uniquely identifies a particular student, is a single field and
therefore is a simple key. No two students would have the same student number.
Compound Key
A compound key consists of more than one field to uniquely identify a record. A compound
key is distinguished from a composite key because each field, which makes up the primary
key, is also a simple key in its own right. An example might be a table that represents the
modules a student is attending. This table has a studentId and a moduleCode as its primary
key. Each of the fields that make up the primary key are simple keys because each
represents a unique reference when identifying a student in one instance and a module in
the other.
Composite
A composite key consists of more than one field to uniquely identify a record. This differs
from a compound key in that one or more of the attributes, which make up the key, are not
simple keys in their own right. Taking the example from compound key, imagine we
identified a student by their firstName + lastName. In our table representing students on
modules our primary key would now be firstName + lastName + moduleCode. Because
firstName + lastName represent a unique reference to a student, they are not each simple
keys, they have to be combined in order to uniquely identify the student. Therefore the key
for this table is a composite key.
Page 4 of 4