Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 257
Luke Hsu Avatar asked Oct 25 '13 08:10

Luke Hsu


1 Answers

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.

like image 191
Basil Bourque Avatar answered Sep 28 '22 00:09

Basil Bourque