BCDE103 Keys Class Notes
BCDE103 Keys Class Notes
Introduction
In the previous sessions when talking about how to normalise database tables we referred to the
idea of unique records and attributes that made each record unique. We used unique attributes as
the basis of normalising our tables to 2NF and 3NF. What we were doing in formal database terms
was identifying primary keys, which is the main topic of this session.
A primary key is a set of attributes (columns) that uniquely identifies a record in a database table.
Having unique records in tables is vital for relational databases: no duplication of data.
Surrogate keys
If there is no natural primary key for a table, then we need to create one. This is called a surrogate
key. Once we have a surrogate key it functions in the same way as a primary key, as the identifier for
that record.
Quick exercise
What attributes of a person (that aren’t physical like fingerprints) make them unique?
There is nothing that is guaranteed to be unique, but we can’t have a “99.9% solution”. A table for
people is the standard example of when you need a surrogate key (which is usually an ID number).
Quick exercise
What could we use as a surrogate key for people? In the USA the standard one is the Social Security
Number. What could we use in NZ?
The problem with NZ is that not everyone has an IRD number and there would be privacy issues if
that was shared. In practice, we just need to create a unique identifier of our own, such as a serial
number.
1
Foreign keys
In our previous classes we briefly mentioned linking tables together or referencing other tables.
When we put the primary key of one table into a second table to act as a reference, we are adding a
foreign key. It is called “foreign” because it is not native to that table. It might appear that we are
duplicating data, but this is not the case – we will see how database management systems ensure
the integrity of keys in a later session.
Lab