A key is a field or set of fields that uniquely identifies each record in a table. A candidate key is a field or set of fields that could serve as the primary key. The primary key is the candidate key that is chosen to uniquely identify each record. When no single field can uniquely identify records, a compound key that combines multiple fields can be used. A surrogate key is a unique identifier created specifically to identify records when the natural candidate keys do not adequately distinguish between records. Foreign keys are used to link records between tables based on matching primary key values.
1 of 15
Downloaded 337 times
More Related Content
Database Keys
2. 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
3. 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
4. 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
5. 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
6. 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.
7. 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.
8. 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.
9. 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.
10. 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.
11. 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.
12. Key Notation
We represent primary keys by underlining them.
We represent foreign keys with an asterisk.
13. 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*)
14. 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)
15. 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)