What is a primary key?
A primary key is a field (or combination of fields) whose value is unique for every record in a table, so each record can be identified. It cannot be empty. Often an ID field such as studentID is used because names and dates can repeat. Example: A Student table has the fields firstName, surname, dateOfBirth and studentID. Which should be the primary key? firstName and surname can repeat. dateOfBirth can repeat. studentID is unique to each student. Answer: studentID is the primary key.
In a relational database, the primary key uniquely identifies each record in a table. In SQA National 5 Computing Science, you identify primary keys and foreign keys when designing linked tables. A foreign key is a field in one table that matches the primary key of another table, creating a relationship.
Worked example
A Student table has firstName, surname, dateOfBirth and studentID. Choose the primary key and justify it.
- Check firstName: two students can share a first name, so it is not unique.
- Check surname and dateOfBirth: these can also repeat.
- Check studentID: each student is given a different ID.
- A primary key must be unique and not empty, so studentID fits.
Answer: studentID is the primary key because it uniquely identifies each student.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre