Set a primary key in Access
The primary key makes every record unique: no two customers share a CustomerID. Later, tables are linked through it. Here you see how Access enforces it.
In short: the primary key is a field (or several) whose value differs in every row and is never empty. Usually it is an AutoNumber such as CustomerID. Access refuses duplicate values and empty keys with a message.
Before you start: Create a table (fields, data types, Required).
Try it now
In this table the code itself is the key. Build the design and click Run: the test records are entered one after another, as if typed in the datasheet. Access refuses two of them.
Task Create the table PaymentMethods. Here the code itself is the primary key: Code (Short Text, 5, primary key) and Description (Short Text, 30, required). Then look at what happens to the test records.
What Access checks on the primary key
| Case | Message from Access |
|---|---|
| value already exists | “The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. …” |
| key is empty | “Index or primary key cannot contain a Null value.” |
Case does not matter for text: “inv” counts as the same key as “INV”. If a required field is missing as well, or a value breaks a validation rule, Access reports that first and the key after it.
AutoNumber or natural key?
- AutoNumber (surrogate key): Access counts up by itself. It is never duplicate or empty, never changes and says nothing about the content. It is the usual choice for customers, products and orders.
- Natural key: a value that is unique anyway, such as a code, an ISBN or an employee number. It fits when it really never changes.
Names, email addresses or phone numbers are poor keys: two people can share a name, and addresses change.
One more exercise: AutoNumber as key
Here the test records contain no ID at all. Access assigns it, so two locations may even have the same name.
Task Create the table Locations, this time with an AutoNumber key: LocationID (AutoNumber, primary key), Description (Short Text, 30, required) and Shelf (Short Text, 10).
In real Access: set the primary key
- Open the table in Design view.
- Click the row of the field (for several fields, hold Ctrl and click the row selectors on the left).
- On the Table Design tab, click Primary Key. A key icon appears next to the field.
- Save. If the field already holds duplicate or empty values, Access refuses to save.
A new table from Create, Table automatically gets an ID field as AutoNumber primary key. If you save a table without a key, Access asks whether it should create one.
Common mistakes
Key on the name
As soon as the second “Smith” arrives, Access refuses it. Use an AutoNumber or a real number.
“Fixing” AutoNumber gaps
Deleting a record or cancelling an entry leaves a gap in the numbering. That is normal; an AutoNumber only has to be unique, not gapless.
FAQ
Does every table need a primary key?
Access does not insist, but without a key you cannot link the table cleanly to others and can hardly prevent duplicate rows. Give every table one.
What is a composite key?
A key made of several fields whose combination is unique, such as OrderID and ProductID in an order details table. It comes up in level 3 with relationships.
Can I set an AutoNumber value myself?
Not by typing. An append query can do it as long as the value does not exist yet; you will rarely need that.