A primary key is an important part of a database table. It uniquely identifies each record and helps maintain data accuracy. In this lesson, you will learn how primary keys work and how to create one in Microsoft Access.
A primary key is a field, or combination of fields, that uniquely identifies every record in a table.
Example:
| StudentID | StudentName | Course |
|---|---|---|
| 101 | Rahul Kumar | ADCA |
| 102 | Priya Kumari | Tally Prime |
| 103 | Amit Kumar | Web Development |
In this table, StudentID can be the primary key because each student has a unique ID.
For example, if two students have the same name, their different StudentID values help distinguish their records.
A simple primary key uses a single field to identify each record.
Example: StudentID
A composite primary key uses two or more fields together to uniquely identify a record.
Example: StudentCourses Table
| StudentID | CourseID | EnrollmentDate |
|---|---|---|
| 101 | C01 | 01/04/2026 |
| 101 | C02 | 05/04/2026 |
| 102 | C01 | 08/04/2026 |
If each student can enroll in a particular course only once, StudentID and CourseID together can form a composite primary key.
Neither field needs to be unique by itself; the combination must be unique.
Example: Open the Students table in Design View, select StudentID, and click Primary Key.
If the table contains duplicate or Null values in the selected field, Access may not allow you to set it as the primary key until the data is corrected.
AutoNumber is commonly used for primary keys because Access generates a value automatically for each new record.
Example:
| StudentID | StudentName |
|---|---|
| 1 | Rahul |
| 2 | Priya |
| 3 | Amit |
| Primary Key | Foreign Key |
|---|---|
| Uniquely identifies records in its table. | References a key in another table, or sometimes the same table. |
| Must be unique and cannot be Null. | May contain repeated values. |
| One primary-key definition per table. | A table can have multiple foreign keys. |
| Example: Students.StudentID | Example: Payments.StudentID |
A foreign key helps connect related tables. For example, a Payments table can store the StudentID of the student who made each payment.
A primary key is strongly recommended for most tables that represent individual entities or transactions.
You can change the primary-key definition in Design View when your database design requires it.
Important: Relationships may depend on the existing key. Check related tables and relationships before changing or removing a primary key.
Create a Students table with the following fields:
| Field Name | Data Type |
|---|---|
| StudentID | AutoNumber |
| StudentName | Short Text |
| Course | Short Text |
| Mobile | Short Text |
Tasks:
1. What is the main purpose of a primary key?
2. Can a primary-key field contain duplicate values?
3. Which data type is commonly used for an automatically generated primary key?