A database key can be defined as an attribute or a collection of attributes that are used to uniquely identify rows (tuples/records) in a relation (table). In relational databases, keys are useful to uniquely determine the relationships between different tables.
In this article, you will learn the following.
Here's an example using a Student Table.
Student_id | Student_Name | Student_Phone |
0303 | Kadian Davis | 8767406534 |
0102 | Shani Reid | 8769955678 |
0798 | Abi Watson | 8764259855 |
The super keys are listed as follows.
Note well - the Student_Name attribute cannot be assigned as a super key because we cannot guarantee a unique value. For instance, the name Abi can have multiple occurrences in the Student Table. However, we see the Student_Name attribute being included to form the composite key along with Student_id or Student_Phone or both.
Note well as mentioned in this video.
Revisiting our Student Table example.
Student_id | Student_Name | Student_Phone |
0303 | Kadian Davis | 8767406534 |
0102 | Shani Reid | 8769955678 |
0798 | Abi Watson | 8764259855 |
Both Student_id and Student_Phone are candidate keys, which will help us to uniquely identify the student record in the table.
Going back to our Student Table, the following attributes are composite keys.
Revisiting our Student Table example.
Student_id | Student_Name | Student_Phone |
0303 | Kadian Davis | 8767406534 |
0102 | Shani Reid | 8769955678 |
0798 | Abi Watson | 8764259855 |
In this example, both Student_id and Student Phone can be set as the primary keys. However, as a database designer, it would be more logical to set Student_id as the primary key.
Let's consider the following example using the Student Table, Course Table, and Instructor Tables
Student_id | Student_Name | Student_Phone | Course_id |
0303 | Kadian Davis | 8767406534 | CS22Q |
0102 | Shani Reid | 8769955678 | CS28A |
0798 | Abi Watson | 8764259855 | CS20A |
Course_id | Course_Name | Course_Semester | Instructor_id |
CS20A | Data Structures and Algorithms | Fall | 00247 |
CS22Q | Software Engineering | Spring | 00767 |
CS28A | Unix Systems Programming | Summer | 00848 |
Instructor_id | Instructor_Name | Instructor_Email |
CS20A | Daniel | d_m@uwimona.edu.jm |
CS22Q | Gunjan | g_y@uwimona.edu.jm |
CS28A | Eyton | e_r@uwimona.edu.jm |
In the Student Table - The Primary key is Student_id, while the Foreign Key is Course_id.
In the Course Table - The Primary key is Course_id, while the Foreign Key is Instructor_id.
In the Instructor Table - The Primary key is Instructor_id, and there are no Foreign Keys.
As mentioned earlier, foreign keys maintain referential identity. So now you might be wondering what exactly is referential integrity.
In the context of relational databases, it requires that if a value of one attribute of a relation references a value of another attribute, then the referenced value must exist. - Wikipedia
Therefore, referential integrity ensures that every foreign key in a child table references a valid and existing primary key in the parent table, which ultimately leads to the accuracy and consistency of the data.
Note well that there are instances where the natural primary key cannot be integrated with other records due to incompatibility of the data.
Considering the Course Table for the Computer Science Department the Course ids (Course_id) are displayed as alphanumeric characters but what happens if we were to add courses from the Mathematics Department, which only contains numeric characters? Then, we would need a surrogate key.
Sur_key | Course_id | Course_Name | Course_Semester |
1 | CS20A | Data Structures and Algorithms | Fall |
2 | CS22Q | Software Engineering | Spring |
3 | CS28A | Unix Systems Programming | Summer |
4 | 220 | Introduction to College Algebra | Fall |
5 | 250 | Fundamentals of Statistics | Summer |
6 | 260 | Introduction to Actuarial Science | Spring |
With the help of surrogate keys, we are now able to avoid incompatibility in our datasets and support better integration of the data.
In this article, we have explored the following.
What is a key in DBMS? - A key is an attribute or a collection of attributes that are used to uniquely identify rows (tuples/records) in a relation (table).
Why are keys important in databases?
In addition, we have explored with examples, the notion of super keys, candidate keys, composite keys, primary keys, foreign keys, and surrogate keys.
If you have been empowered by the information shared in this article, then please clap using the clap emoticon. Please feel free to ask any questions or to share your comments using the comment emoticon.
FOLLOW Kadian:
Instagram: https://www.instagram.com/kadiandavisowusu/
Linktree: https://linktr.ee/kadiandavisowusu
Created by
Kadian has a background in Computer Science and pursued her PhD and post-doctoral studies in the fields of Design for Social Interaction and Design for Health. She has taught a number of interaction design courses at the university level including the University of the West Indies, the University of the Commonwealth Caribbean (UCC) in Jamaica, and the Delft University of Technology in The Netherlands. Kadian also serves as the Founder and Lead UX Designer for TeachSomebody and is the host of the ExpertsConnect video podcast. In this function, Kadian serves to bridge the learning gap by delivering high-quality content tailored to meet your learning needs. Moreover, through expert collaboration, top-quality experts are equipped with a unique channel to create public awareness and establish thought leadership in their related domains. Additionally, she lectures on ICT-related courses at Fontys University of Applied Sciences.