Lesson 14 of 50 – Primary Key in MS Access
28%

Primary Key in MS Access

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.

1. What Is a Primary Key?

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.

2. Why Is a Primary Key Important?

  • Uniquely identifies each record.
  • Prevents duplicate primary-key values.
  • Does not allow Null values in primary-key fields.
  • Helps establish relationships between tables.
  • Makes it easier to locate a particular record.
  • Supports data integrity in a relational database.

For example, if two students have the same name, their different StudentID values help distinguish their records.

3. Rules of a Primary Key

  1. Unique values: Each record must have a different primary-key value.
  2. No Null values: A primary-key field cannot be Null.
  3. One primary key per table: A table can have only one primary-key definition.
  4. Multiple fields are possible: A primary key can contain more than one field.
  5. Stable values: Choose values that are unlikely to change.

4. Types of Primary Keys

1. Simple Primary Key

A simple primary key uses a single field to identify each record.

Example: StudentID

2. Composite Primary Key

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.

5. How to Set a Primary Key in MS Access

  1. Open your database in Microsoft Access.
  2. Open the required table in Design View.
  3. Select the field you want to use as the primary key.
  4. Click the Primary Key command on the Ribbon.
  5. Look for the key symbol beside the selected field.
  6. Save the table using Ctrl + S.

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.

6. Using AutoNumber as a Primary Key

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
  • Access generates StudentID automatically.
  • You normally do not type the AutoNumber value yourself.
  • AutoNumber values may contain gaps.
  • Use AutoNumber when you need a unique internal identifier.

7. Primary Key vs. Foreign Key

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.

8. What Happens If You Do Not Use a Primary Key?

  • Duplicate records may be harder to distinguish.
  • Identifying a particular record can become difficult.
  • Creating relationships may be more complicated.
  • Data consistency can be harder to maintain.

A primary key is strongly recommended for most tables that represent individual entities or transactions.

9. Removing or Changing a Primary Key

You can change the primary-key definition in Design View when your database design requires it.

  1. Open the table in Design View.
  2. Select the current primary-key field.
  3. Click the Primary Key command to remove the designation.
  4. Select the new field or fields.
  5. Set the new primary key.
  6. Save the table.

Important: Relationships may depend on the existing key. Check related tables and relationships before changing or removing a primary key.

10. Practical Assignment

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. Create the Students table in Design View.
  2. Set StudentID as the primary key.
  3. Save the table.
  4. Enter at least five student records.
  5. Try entering a duplicate StudentID if possible and observe the result.
  6. Create a Payments table with a PaymentID primary key and a StudentID field.
  7. Explore how the StudentID fields could connect the two tables.

11. Revision Questions

  1. What is a primary key?
  2. Why is a primary key important?
  3. Write four rules of a primary key.
  4. What is a simple primary key?
  5. What is a composite primary key?
  6. How do you set a primary key in MS Access?
  7. Why is AutoNumber commonly used for primary keys?
  8. What is the difference between a primary key and a foreign key?
  9. Can a primary key contain Null values?
  10. What should you check before changing a primary key?

🧠 Quick Quiz

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?