Page 1Classification: Restricted
Business Analysis
Training
Database Keys
Page 2Classification: Restricted
Agenda
• Keys
• Candidate Key
• Primary Key
• Compound Key
• Surrogate Key
• Foreign Key
• Key Notation
Page 3Classification: Restricted
Keys
A key is a field or set of fields whose values uniquely identify a record.
In this example, we could use SCN as a key because it uniquely identifies a
pupil record. No other pupil can have the same SCN (Scottish Candidate
Number).
No other fields can uniquely identify each pupil record
Page 4Classification: Restricted
Candidate Key
Any field or set of fields which can be considered as a key is known as a
candidate key
In this example, both Employee Number and Email are candidate keys,
because both can uniquely identify the employee record in the table
Page 5Classification: Restricted
Primary Key
The primary key is the candidate key which is chosen to be used
In the example above, it's possible that the company may change the
structure of staff email addresses. Given this, it would be better to use
Employee Number as a primary key as this is less likely to change.
A primary key consisting of only one field is known as an atomic key
Page 6Classification: Restricted
Compound Key
It is possible in some tables that no one column can act as a primary key, and
that a combination of columns are required to create the key. This is known
as a compound key.
Order ID cannot be a primary key as it does not uniquely identify a record
Product ID cannot be a primary key as it does not uniquely identify a record
Page 7Classification: Restricted
Compound Key
It is possible in some tables that no one column can act as a primary key, and
that a combination of columns are required to create the key. This is known
as a compound key.
A compound key of Order ID and Product ID could be used because this is
unique for each record.
Page 8Classification: Restricted
Surrogate Key
A surrogate key is a special key which has no meaning or purpose other than
to uniquely identify each record. Surrogate keys are often a combination of
letters and numbers. A new column is created to be the primary key.
Page 9Classification: Restricted
Surrogate Key
A surrogate key is a special key which has no meaning or purpose other than
to uniquely identify each record. Surrogate keys are often a combination of
letters and numbers. A new column is created to be the primary key.
In this example, KTV have been interviewing passers-by for the evening news
bulletin. The production team take a note of the interviewee's name and the
duration of the interview.
As these people are not related to KTV in anyway, a surrogate key must be
created in order to uniquely identify them.
Page 10Classification: Restricted
Surrogate Key
A surrogate key is a special key which has no meaning or purpose other than
to uniquely identify each record. Surrogate keys are often a combination of
letters and numbers. A new column is created to be the primary key.
In this example, KTV have been interviewing passers-by for the evening news
bulletin. The production team take a note of the interviewee's name and the
duration of the interview.
As these people are not related to KTV in anyway, a surrogate key must be
created in order to uniquely identify them.
Page 11Classification: Restricted
Foreign Key
A foreign key is a column which is added to one table to create a relationship
with another table.
In this example we have two tables, teachers and departments in a school.
But there is no way to see which teacher works in which department.
Page 12Classification: Restricted
Foreign Key
A foreign key is a column which is added to one table to create a relationship
with another table.
By adding the foreign key Department Code to the Teacher table, we can
create a relationship between the two tables.
Page 13Classification: Restricted
Key Notation
We represent primary keys by underlining them.
We represent foreign keys with an asterisk.
Page 14Classification: Restricted
Key Notation
We represent primary keys by underlining them.
We represent foreign keys with an asterisk.
young scot card (card_num, name, expiry_date, photo)
pupil (candidate_num, forename, surname, dob, card_num*)
Page 15Classification: Restricted
Key Notation
We represent primary keys by underlining them.
We represent foreign keys with an asterisk.
pupil (candidate_num, forename, surname, dob, postcode)
exam (exam_id, subject, level, duration)
result (candidate_num*, exam_id*, grade)
Page 16Classification: Restricted
Key Notation
We represent primary keys by underlining them.
We represent foreign keys with an asterisk.
movie (movie_id, name, certificate, rating)
cinema (cinema_id, city, telephone)
screening (cinema*, movie*, date, time)
Page 17Classification: Restricted
Thank You!

Database Keys

  • 1.
    Page 1Classification: Restricted BusinessAnalysis Training Database Keys
  • 2.
    Page 2Classification: Restricted Agenda •Keys • Candidate Key • Primary Key • Compound Key • Surrogate Key • Foreign Key • Key Notation
  • 3.
    Page 3Classification: Restricted Keys Akey is a field or set of fields whose values uniquely identify a record. In this example, we could use SCN as a key because it uniquely identifies a pupil record. No other pupil can have the same SCN (Scottish Candidate Number). No other fields can uniquely identify each pupil record
  • 4.
    Page 4Classification: Restricted CandidateKey Any field or set of fields which can be considered as a key is known as a candidate key In this example, both Employee Number and Email are candidate keys, because both can uniquely identify the employee record in the table
  • 5.
    Page 5Classification: Restricted PrimaryKey The primary key is the candidate key which is chosen to be used In the example above, it's possible that the company may change the structure of staff email addresses. Given this, it would be better to use Employee Number as a primary key as this is less likely to change. A primary key consisting of only one field is known as an atomic key
  • 6.
    Page 6Classification: Restricted CompoundKey It is possible in some tables that no one column can act as a primary key, and that a combination of columns are required to create the key. This is known as a compound key. Order ID cannot be a primary key as it does not uniquely identify a record Product ID cannot be a primary key as it does not uniquely identify a record
  • 7.
    Page 7Classification: Restricted CompoundKey It is possible in some tables that no one column can act as a primary key, and that a combination of columns are required to create the key. This is known as a compound key. A compound key of Order ID and Product ID could be used because this is unique for each record.
  • 8.
    Page 8Classification: Restricted SurrogateKey A surrogate key is a special key which has no meaning or purpose other than to uniquely identify each record. Surrogate keys are often a combination of letters and numbers. A new column is created to be the primary key.
  • 9.
    Page 9Classification: Restricted SurrogateKey A surrogate key is a special key which has no meaning or purpose other than to uniquely identify each record. Surrogate keys are often a combination of letters and numbers. A new column is created to be the primary key. In this example, KTV have been interviewing passers-by for the evening news bulletin. The production team take a note of the interviewee's name and the duration of the interview. As these people are not related to KTV in anyway, a surrogate key must be created in order to uniquely identify them.
  • 10.
    Page 10Classification: Restricted SurrogateKey A surrogate key is a special key which has no meaning or purpose other than to uniquely identify each record. Surrogate keys are often a combination of letters and numbers. A new column is created to be the primary key. In this example, KTV have been interviewing passers-by for the evening news bulletin. The production team take a note of the interviewee's name and the duration of the interview. As these people are not related to KTV in anyway, a surrogate key must be created in order to uniquely identify them.
  • 11.
    Page 11Classification: Restricted ForeignKey A foreign key is a column which is added to one table to create a relationship with another table. In this example we have two tables, teachers and departments in a school. But there is no way to see which teacher works in which department.
  • 12.
    Page 12Classification: Restricted ForeignKey A foreign key is a column which is added to one table to create a relationship with another table. By adding the foreign key Department Code to the Teacher table, we can create a relationship between the two tables.
  • 13.
    Page 13Classification: Restricted KeyNotation We represent primary keys by underlining them. We represent foreign keys with an asterisk.
  • 14.
    Page 14Classification: Restricted KeyNotation We represent primary keys by underlining them. We represent foreign keys with an asterisk. young scot card (card_num, name, expiry_date, photo) pupil (candidate_num, forename, surname, dob, card_num*)
  • 15.
    Page 15Classification: Restricted KeyNotation We represent primary keys by underlining them. We represent foreign keys with an asterisk. pupil (candidate_num, forename, surname, dob, postcode) exam (exam_id, subject, level, duration) result (candidate_num*, exam_id*, grade)
  • 16.
    Page 16Classification: Restricted KeyNotation We represent primary keys by underlining them. We represent foreign keys with an asterisk. movie (movie_id, name, certificate, rating) cinema (cinema_id, city, telephone) screening (cinema*, movie*, date, time)
  • 17.