Information Technology Topic

Keys in a Database

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. A key is a field, or a group of fields, used to identify records: the primary key is the one chosen to identify each record uniquely, and a table has only one
  2. A candidate key could be the primary key, an alternate key is a candidate that was not chosen, a composite key uses two or more fields together, and a secondary key is used to search and need not be unique
  3. A foreign key is a field whose values must match the primary key of another table, which is how tables are linked

Section 1Why tables need keys

In a table of 8 students, two of them may have the same surname and the same first name. Two students named Kemar Brown can both be in the school. If a record is found by name alone, you might pick the wrong one. A key is a field, or a group of fields, used to identify records. Some keys must be different in every record. Others are used only for searching and linking. There are six key words to know: primary, secondary, composite, candidate, alternate and foreign. The notes below take them one at a time, using two small school tables.

Section 2The primary key

The Student table (StudentID, Surname, FirstName, Form, SchoolEmail) holds: S101, Brown, Kemar, 4A, kbrown@school.example; S102, Campbell, Shanice, 4B, scampbell@school.example; S103, Reid, Kemar, 4A, kreid@school.example; S104, Brown, Kemar, 4C, kbrown2@school.example; S105, Gordon, Tanya, 4B, tgordon@school.example; S106, Reid, Shanice, 4C, sreid@school.example; S107, Francis, Andre, 4A, afrancis@school.example; S108, Allen, Tanya, 4B, tallen@school.example. The primary key is the field chosen to identify each record uniquely. Two rules apply. Its values can never repeat, and they can never be empty. A table can have only one primary key. In the Student table, StudentID is the primary key. Every student has a different ID, so S104 points to exactly one record. Here's the trick: ask 'if I know this value, can I find exactly one record?' If yes, it can be a primary key.

Section 3Candidate and alternate keys

A candidate key is any field (or group of fields) that could identify every record uniquely. It is a candidate to be the primary key. A group of fields counts only if every field in it is needed: if you can drop a field and the rest is still unique, use the smaller group. In the Student table two fields can do the job: StudentID and SchoolEmail. Each student has their own school e-mail address. Both are candidate keys. Only one candidate becomes the primary key. Here StudentID is chosen. The candidate that was not chosen, SchoolEmail, is called the alternate key. It is still unique in every record, but it is not the main identifier. Watch out: a field is a candidate key because of what it means, not only because it is unique in today's data. Surname and FirstName together may look like a possible key, but two students can easily share both names. Kemar Brown appears twice here.

Section 4The composite key

The ClassResult table (StudentID, SubjectCode, Mark) holds: S101, MAT, 72; S101, ENG, 72; S102, MAT, 88; S102, ENG, 79; S103, MAT, 72; S104, ENG, 58; S105, MAT, 91; S105, ENG, 84. Sometimes no single field is different in every record. In the ClassResult table, StudentID repeats (S101 has two results) and SubjectCode repeats (MAT appears four times). A mark can repeat too. But the pair of fields StudentID and SubjectCode is different in every record, because a student has only one result for each subject. A primary key made of two or more fields together is a composite key. The rule is the same: the combination must be unique and never empty. S101 with MAT, and S101 with ENG, are different combinations.

Section 5The foreign key

A foreign key is a field in one table whose values must match the primary key of another table. It is how tables are linked. In ClassResult, the StudentID field is a foreign key. Each value in it must match a StudentID in the Student table. A result for S120 would be rejected, because there is no student S120. The same field can play two roles. In ClassResult, StudentID is part of the composite primary key and it is also a foreign key pointing to the Student table. Easy to mix up: in the Student table StudentID is the primary key. In ClassResult the same field is a foreign key. Which one it is depends on which table you are looking at. A primary key value does not have to be used. Students S106, S107 and S108 have no results yet, and that is not a problem.

Section 6The secondary key

A secondary key is a field used to search or sort records. It does not have to be unique. Textbooks do not all use this term in exactly the same way, so in this topic it always means a search field that may repeat. If your teacher words it differently, follow your teacher for this one term. A form teacher looking up all the 4B students uses Form as a secondary key. The search finds three records, S102, S105 and S108. Searching by Surname finds two Browns. So the main difference is simple. A primary key finds one record. A secondary key may find several. Here's the trick: unique and used to identify means primary, candidate or alternate. Repeats allowed and used to search means secondary.

Section 7Choosing and checking keys

Follow these steps for a table. 1. List the fields. Look for ones that are different in every record and cannot be empty. These are the candidate keys. 2. Pick one candidate as the primary key. The others are alternate keys. 3. If no single field is unique, try two or more fields together as a composite key. 4. For fields that point at another table, check every value matches a primary key value there. These are foreign keys. 5. Fields you search by, such as Form, are secondary keys. Test each choice by asking if it would still be unique for any student the school might enrol later.

Examples

The Student table (StudentID, Surname, FirstName, Form, SchoolEmail) holds: S101, Brown, Kemar, 4A, kbrown@school.example; S102, Campbell, Shanice, 4B, scampbell@school.example; S103, Reid, Kemar, 4A, kreid@school.example; S104, Brown, Kemar, 4C, kbrown2@school.example; S105, Gordon, Tanya, 4B, tgordon@school.example; S106, Reid, Shanice, 4C, sreid@school.example; S107, Francis, Andre, 4A, afrancis@school.example; S108, Allen, Tanya, 4B, tallen@school.example. (a) Which single fields have a different value in every record? (b) Which one would you choose as the primary key? (c) What is the other one called? (d) Why can Surname not be the primary key? (e) A form teacher searches the table for all the students in 4B. What kind of key is Form?

(a) StudentID and SchoolEmail. Surname repeats (Brown and Reid each appear twice), FirstName repeats (Kemar appears three times) and Form repeats. (b) StudentID. It is unique and it is the school's own identifier for each student. (c) SchoolEmail is an alternate key. It is a candidate key that was not chosen as the primary key. (d) Brown appears twice (S101 and S104), so a surname value can point to more than one record. (e) A secondary key. The search for 4B finds three records (S102, S105 and S108), so Form does not identify one record.

The Student table (StudentID, Surname, FirstName, Form, SchoolEmail) holds: S101, Brown, Kemar, 4A, kbrown@school.example; S102, Campbell, Shanice, 4B, scampbell@school.example; S103, Reid, Kemar, 4A, kreid@school.example; S104, Brown, Kemar, 4C, kbrown2@school.example; S105, Gordon, Tanya, 4B, tgordon@school.example; S106, Reid, Shanice, 4C, sreid@school.example; S107, Francis, Andre, 4A, afrancis@school.example; S108, Allen, Tanya, 4B, tallen@school.example. The ClassResult table (StudentID, SubjectCode, Mark) holds: S101, MAT, 72; S101, ENG, 72; S102, MAT, 88; S102, ENG, 79; S103, MAT, 72; S104, ENG, 58; S105, MAT, 91; S105, ENG, 84. (a) Why can StudentID not be the primary key of ClassResult on its own? (b) Which fields make up the composite primary key of ClassResult? (c) Which field of ClassResult is a foreign key, and which table does it point to? (d) Four new ClassResult records are offered: S101, MAT, 80; S120, ENG, 70; S106, MAT, 66; S102, ENG, 90. Which one can be added? (e) Is it a problem that S106, S107 and S108 have no results?

(a) S101 appears twice (MAT and ENG), and S102 and S105 do as well, so a StudentID does not identify one record. (b) StudentID and SubjectCode together. Each pair is different: for example S101, MAT and S101, ENG are two different pairs. (c) StudentID is the foreign key. It points to the StudentID primary key in the Student table. (d) Only S106, MAT, 66. S101, MAT already exists, so the pair would repeat. S120 has no matching student. S102, ENG already exists too. (e) No. A foreign key value must match a primary key, but not every primary key value has to be used.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from