Understand the purpose of a primary key and identify one
📚 Databases: Primary Keys
What is a Primary Key?
A primary key is a special column (or set of columns) in a database table that uniquely identifies each row. Think of it as a unique ID card for every record.
- It must be unique – no two rows can have the same primary key value.
- It cannot be NULL – every row must have a value.
- It is often used as a reference in other tables (foreign keys).
🔑 Example: In a table of students, the student_id column could be the primary key.
Analogy: Library Books
Imagine a library where each book has a unique ISBN. Even if two books have the same title, their ISBNs are different, so you can always find the exact book you want. The ISBN is like a primary key for books.
Identifying a Primary Key in a Table
When you look at a table definition, the primary key is usually highlighted or marked with PRIMARY KEY. If it’s not obvious, look for a column that:
- Has a unique value for every row.
- Is not nullable.
- Often has a name like
id,student_id, ororder_id.
Composite Primary Key
Sometimes a single column isn’t enough to guarantee uniqueness. In that case, two or more columns together form a primary key.
📌 Example: In a table that records student_id and course_id for enrolments, the combination of both columns is unique.
| student_id | name | |
|---|---|---|
| 1001 | Alice Smith | alice@example.com |
| 1002 | Bob Jones | bob@example.com |
Exam Tips
- Look for a column marked
PRIMARY KEYor namedid. - Check that the column is unique and not null.
- Remember that a composite key is a combination of two or more columns.
- When answering “Identify the primary key”, write the column name(s) exactly as shown in the table.
Revision
Log in to practice.