Add or change a table’s primary key in Access (2024)

A primary key is a field or set of fields with values that are unique throughout a table. Values of the key can be used to refer to entire records, because each record has a different value for the key. Each table can only have one primary key. Access can automatically create a primary key field for you when you create a table, or you can specify the fields that you want to use as the primary key. This article explains how and why to use primary keys.

To set a table’s primary key, open the table in Design view. Select the field (or fields) that you want to use, and then on the ribbon, click Primary Key.

Note:This article is intended for use only with Access desktop databases. Access automatically manages primary keys for new tables in Access web apps and web databases. Although it is possible to override these automated primary keys, we don’t recommend that you do.

In this article

  • Overview of primary keys in Access

  • Set the primary key using fields you already have in Access

  • Remove a primary key

  • Change the primary key in Access

  • Additional information

Overview of primary keys in Access

Access uses primary key fields to quickly associate data from multiple tables and combine that data in a meaningful way. You can include the primary key fields in other tables to refer back to the table that is the source of the primary key. In those other tables, the fields are called foreign keys. For example, a Customer ID field in the Customers table might also appear in the Orders table. In the Customers table, it is the primary key. In the Orders table it is called a foreign key. A foreign key, simply stated, is another table's primary key. For more information, see Database design basics.

Add or change a table’s primary key in Access (1)

1. Primary key

2. Foreign key

If you are moving existing data into a database, you may already have a field that you can use as the primary key. Often, a unique identification number, such as an ID number or a serial number or code, serves as a primary key in a table. For example, you might have a Customers table where each customer has a unique customer ID number. The customer ID field is the primary key.

Access automatically creates an index for the primary key, which helps speed up queries and other operations. Access also ensures that every record has a value in the primary key field, and that it is always unique.

When you create a new table in Datasheet view, Access automatically creates a primary key for you and assigns it a field name of "ID" and the AutoNumber data type.

What makes a good primary key?

A good candidate for a primary key has several characteristics:

  • It uniquely identifies each row

  • It is never empty or null— it always contains a value

  • The values it contains rarely (ideally, never) change

If you can’t identify a good key, create an AutoNumber field to use as the key. An AutoNumber field automatically generates a value for itself when each record is first saved. Therefore, an AutoNumber field meets all three characteristics of a good primary key. For more information on adding an AutoNumber field, see the article Add an AutoNumber field as a primary key.

Add or change a table’s primary key in Access (2)

An AutoNumber field makes a good primary key.

Examples of poor primary keys

Any field that is missing one or more of the characteristics of a good candidate key is a poor choice for a primary key. Here are a few examples of fields that would make poor primary keys for a Contacts table, along with reasons why they would be poor choices.

Poor primary key

Reason

Personal name

Might not be reliably unique, and may change

Phone number

Likely to change.

E-mail address

Likely to change.

Zip code

More than one person may share a ZIP code

Combinations of facts and numbers

The fact portion might change, creating a maintenance burden. Could lead to confusion if the fact portion is repeated as a separate field. For example, combining the city and an incremented number (e.g., NEWYORK0579) would be a poor choice if the city is also stored as a field.

Social Security Numbers

  • Private information and not allowed in government departments and some organizations.

  • Some people don’t have a SSN

  • An individual may have more than one in a lifetime

Composite keys: using multiple fields in combination as a primary key

In some cases, you want to use two or more fields in a table as the primary key. For example, an Order Details table that stores line items for orders might use two fields in its primary key: Order ID and Product ID. A key that has more than one field is called a composite key.

Set the primary key using fields you already have in Access

For a primary key to work well, the field must uniquely identify each row, never contain an empty or null value, and rarely (ideally, never) change. To set the primary key:

  1. Open the database that you want to modify.

  2. In the Navigation Pane, right click the table in which you want to set the primary key and, on the shortcut menu, click Design View.

    Tip:If you don't see the Navigation Pane, press F11 to display it.

  3. Select the field or fields that you want to use as the primary key.

    To select one field, click the row selector for the field you want.

    To select more than one field to create a composite key, hold down CTRL and then click the row selector for each field.

  4. On the Table Design tab, in the Tools group, click Primary Key.

    Add or change a table’s primary key in Access (3)

    A key indicator is added to the left of the field or fields that you specify as the primary key.

Remove a primary key in Access

When you remove the primary key, the field or fields that previously served as primary key will no longer provide the primary means of identifying a record.

Removing the primary key doesn’t delete the fields from your table, but it does remove the index that was created for the primary key.

  1. Before you can remove a primary key, you must make sure that it doesn't participate in any table relationships. If you try to remove a primary key that is part of one or more relationships, Access warns you that you must delete the relationships first.

    To delete a table relationship, complete the following steps:

    1. If the tables that participate in the table relationship are open, close them. You cannot delete a table relationship between open tables.

    2. On the Database Tools tab, in the Relationships group, click Relationships.

      Add or change a table’s primary key in Access (4)

    3. Select Add Tables.

    4. Click the table relationship line for the table relationship that you want to delete (the line becomes bold when it is selected), and then press the DELETE key.

    5. On the Relationships Design tab, in the Relationships group, click Close.

  2. After you delete the relationships, in the Navigation Pane, right click the table from which you want to remove the primary key and then click Design View.

    Tip:If you don't see the Navigation Pane, press F11 to display it.

  3. Click the row selector for the current primary key.

    If the primary key consists of a single field, click the row selector for that field.

    If the primary key consists of multiple fields, click the row selector for any field in the primary key.

  4. On the Table Design tab, in the Tools group, click Primary Key.

    The key indicator is removed from the field or fields that you previously specified as the primary key.

Note:When you save a new table without setting a primary key, Access prompts you to create one. If you choose Yes, Access creates an ID field that uses the AutoNumber data type to provide a unique value for each record. If your table already includes an AutoNumber field, Access uses that field as the primary key.

Change the primary key in Access

If you decide to change a table's primary key, you can do so by following these steps:

  1. Remove the existing primary key using the instructions in the section Remove the primary key.

  2. Set the primary key using the instructions in the section Set the primary key.

Additional information

For more information about database design and choosing an appropriate primary key, see the articles:

  • Database basics

  • Learn the structure of an Access database

  • Create an Access database

  • Create a table

Top of Page

Add or change a table’s primary key in Access (2024)

FAQs

Add or change a table’s primary key in Access? ›

To set a table's primary key, open the table in Design view. Select the field (or fields) that you want to use, and then on the ribbon, click Primary Key.

How do I change the primary key in an Access table? ›

To set a table's primary key, open the table in Design view. Select the field (or fields) that you want to use, and then on the ribbon, click Primary Key.

How do you alter a table and add a primary key? ›

Use SQL Server Management Studio
  1. In Object Explorer, right-click the table to which you want to add a unique constraint, and select Design.
  2. In Table Designer, select the row selector for the database column you want to define as the primary key. ...
  3. Right-click the row selector for the column and select Set Primary Key.
Apr 18, 2024

What are the two ways of setting a primary key in Access? ›

To set the primary key you have to follow the steps given below:Step 1 Select the field that you want to set as a primary key. Step 2 Click Primary Key button from the Design tab. orRight click on the selected field and select Primary Key.

How to create two primary keys in Access? ›

To create a composite primary key on two or more columns (fields), in table design view hold down the Ctrl key and left click on each of the columns, being sure to click on the 'field selector' (the small square to the left of the column name). Then right click and select 'Primary Key' from the shortcut menu.

How can you change the primary key value of a row? ›

Updating a primary key
  1. Repeat the row containing the original primary key value you want to change. ...
  2. Change the primary key in the new row to the required value.
  3. Use the SAVE primary command to verify that there are no other Db2 errors.
  4. Delete the original row.

How do I select a primary key? ›

When selecting a primary key, you'll need to choose a column that contains unique values for all rows. Every single row must have a value in the primary key column, and the values must not change. If you can't find a natural primary key that fits all of the rules, you could create a surrogate key.

Can you add a primary key to an existing table without primary key? ›

Yes you can add primary key into existing table , but you need to make sure that column on which you are creating primary key must have unique and not null values ..

Can I update a table with a primary key? ›

If you know the value of the primary key for the row that you want to update, you can specify the value using the VIA clause. If there is more than one primary key column, the values must be specified in order and separated by commas (,).

How many primary key can be added to a table? ›

The PRIMARY KEY constraint specifies that the constrained columns' values must uniquely identify each row. A table can only have one primary key, but it can have multiple unique constraints.

What is the primary key of a table? ›

A primary key is the column or columns that contain values that uniquely identify each row in a table. A database table must have a primary key for Optim to insert, update, restore, or delete data from a database table. Optim uses primary keys that are defined to the database.

How many ways can you set a primary key? ›

A primary key can be set for a table in one of three ways. Open a table in Design view and: With the field to be used as the primary key selected, click the Primary Key button key in the Tools group. Right-click the appropriate field and choose Primary Key from the shortcut menu.

Can you set the primary key of a table through the table wizard? ›

You will have the option of specifying a primary key or letting Access generate a record ID field as a primary key. If you have other tables in your database, you will be able to set up relationships as part of the Wizard. When your table is complete, and all choices have been made, click Finish.

How to change primary key in Access? ›

In the Navigation Pane, right click a table, and select Design View. Select the field or fields you want to use as the primary key. Select Design > Primary Key.

How do I add another primary key? ›

To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key. Note You must be logged in to the database using a database name before you can add a primary key or conduct any other referential integrity (RI) operation.

How do I make two primary keys in one table? ›

The short answer is no, a table is not allowed to contain multiple primary keys , as that goes against the fundamental principles of relational database design (see: [database normalisation](https://en.wikipedia.org/wiki/Database_normalisation) and [Third normal form](https://en.wikipedia.org/wiki/Third_normal_form) ).

Can you update or delete the primary key value of a table? ›

You can MODIFY a Primary key value you can only RENAME it. NewRec. Insert; There's a reason it "took long time" - it also needs to look up all related records (where Table Relation is pointing to the field you're changing) and modify values there as well.

How to remove primary key from table in Access? ›

In the table grid, right-click the row with the primary key and choose Remove Primary Key to toggle the setting from on to off. To undo this action, close the table without saving the changes. Deleting a primary key cannot be undone without losing all other changes made to the table.

How do I remove a primary key constraint from a table? ›

If you wanted to remove the primary key from this table, you would use the following query: ALTER TABLE users DROP PRIMARY KEY; This query would remove the primary key from the users table. Note that this does not delete the data in the table, it just removes the primary key.

How do I change the default value in an Access table? ›

Set a default value
  1. In the Navigation Pane, right-click the table that you want to change, and then click Design View.
  2. Select the field that you want to change.
  3. On the General tab, type a value in the Default Value property box. ...
  4. Save your changes.

Top Articles
Grant Cardone Says These 10 Steps Can Help You Become a Billionaire
Your Ultimate Guide to Answering the Most Common Interview Questions
Pollen Count Centreville Va
South Park Season 26 Kisscartoon
Health Benefits of Guava
Tx Rrc Drilling Permit Query
How Far Is Chattanooga From Here
Embassy Suites Wisconsin Dells
Craigslist Greenville Craigslist
Jasmine Put A Ring On It Age
The Binding of Isaac
Hca Florida Middleburg Emergency Reviews
Dexter Gomovies
Dc Gas Login
Chic Lash Boutique Highland Village
Colorado mayor, police respond to Trump's claims that Venezuelan gang is 'taking over'
Quest Beyondtrustcloud.com
065106619
WEB.DE Apps zum mailen auf dem SmartPhone, für Ihren Browser und Computer.
Kayky Fifa 22 Potential
Curver wasmanden kopen? | Lage prijs
1989 Chevy Caprice For Sale Craigslist
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
3Movierulz
Cardaras Funeral Homes
Free T33N Leaks
Hrconnect Kp Login
4.231 Rounded To The Nearest Hundred
Craigslist Efficiency For Rent Hialeah
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Himekishi Ga Classmate Raw
Eegees Gift Card Balance
FREE Houses! All You Have to Do Is Move Them. - CIRCA Old Houses
Tmj4 Weather Milwaukee
Salons Open Near Me Today
Pitco Foods San Leandro
Why Gas Prices Are So High (Published 2022)
Sams La Habra Gas Price
Heelyqutii
Thor Majestic 23A Floor Plan
Courses In Touch
6576771660
Why Are The French So Google Feud Answers
Costco The Dalles Or
Sinai Sdn 2023
5103 Liberty Ave, North Bergen, NJ 07047 - MLS 240018284 - Coldwell Banker
Bismarck Mandan Mugshots
Campaign Blacksmith Bench
Optimal Perks Rs3
7 Sites to Identify the Owner of a Phone Number
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5736

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.