Luke Hsu
Luke Hsu

Reputation: 179

Do I need a primary key for every table in MS Access?

I am new to MSAccess so I'm not sure about this; do I have to have a primary key for every single table in my database? I have one table which looks something like this:

(http://i108.photobucket.com/albums/n32/lurker3345/ACCESSHELP.png?t=1382688844)

In this case, every field/column has a repeating term. I have tried assigning the primary key to every field but it returns with an error saying that there is a repeated field.

How do I go about this?

Upvotes: 5

Views: 10699

Answers (4)

David Petty
David Petty

Reputation: 21

In you use a DataAdapter and a Currency Manager, your tables must have a primary key in order to push updates, additions and deletions back to the database. Otherwise, they will not register and you will receive an error.

I lost one week figuring that one out until I added this to the Try-Catch-End Try block: MsgBox(er.ToString) which mentioned "key". From there, I figured it out. (NB : Having a primary key was not a requisite in VB6)

Upvotes: 1

Basil Bourque
Basil Bourque

Reputation: 338775

Strictly speaking, Yes, every row in a relational database should have a Primary Key (a unique identifier). If doing quick-and-dirty work, you may be able to get away without one.

Internal Tracking ID

Some database generate a primary key under-the-covers if you do not assign one explicitly. Every database needs some way to internally track each row.

Natural Key

A natural key is an existing field with meaningful data that happens to identify each row uniquely. For example, if you were tracking people assigned to teams, you might have an "employee_id" column on the "person" table.

Surrogate Key

A surrogate key is an extra column you add to a table, just to assign an arbitrary value as the unique identifier. You might assign a serial number (1, 2, 3, …), or a UUID if your database (such as Postgres) supports that data type. Assigning a serial number or UUID is so common that nearly every database engine provides a built-in facility to help you automatically create such a value and assign to new rows.

My Advice

In my experience, any serious long-term project should always use a surrogate key because every natural key I've ever been tempted to use eventually changes. People change their names (get married, etc.). Employee IDs change when company gets acquired by another.

If, on the other hand, you are doing a quick-and-dirty job, such as analyzing a single batch of data to produce a chart once and never again, and your data happens to have a natural key then use it. Beware: One-time jobs often have a way of becoming recurring jobs.

Further advice… When importing data from a source outside your control, assign your own identifier even if the import contains a candidate key.

Composite Key

Some database engines offer a composite key feature, also called compound key, where two or more columns in the table are combined to create a single value which once combined should prove unique. For example, in a "person" table, "first_name" and "last_name", and "phone_number" fields might be unique when considered together. Unless two people married and sharing the same home phone number while also happening to each be named "Alex" with a shared last name! Because of such collisions as well as the tendency for meaningful data to change and also the overhead of calculating such combined values, it is advisable to stick with simple (single-column) keys unless you have a special situation.

Upvotes: 5

Benjamin Warren
Benjamin Warren

Reputation: 539

Not having a primary key usually means your data is poorly structured. However, it looks like you're dealing with summary/aggregate data there, so it's probably doesn't matter.

Upvotes: 0

greg84
greg84

Reputation: 7609

If the data doesn't naturally have a unique field to use as the primary key, add an auto-generated integer column called "Id" or similar.

Read the "how to organize my data" section of this page:

http://www.htmlgoodies.com/primers/database/article.php/3478051

This page shows you how to create one (under "add an autonumber primary key"):

http://office.microsoft.com/en-gb/access-help/create-or-remove-a-primary-key-HA010014099.aspx

Upvotes: 2

Related Questions