Simple clear advice in plain English

Using compound primary keys

Discover what compound primary keys can do for your and your data

A primary key provides a value that is guaranteed to be unique for every record in a table. This is often a number in a single field. For example, if we sell books, we could give each book a unique ProductID.

It is a good idea to use a simple primary key like this because it’s easy to understand and queries will be fast.

Access’ AutoNumber data type is often a good choice. However, it is possible to use two (or even more) fields for the primary key; this is called a ‘compound’ or ‘joint’ primary key.

If you do this, it is fine for two records to have the same value in one of the fields as long as the other contains a different value. For example, the screenshot of the table’s primary key is made up of ProductID and ProductID2.

We can now have duplicate values ProductID as long as the value in ProductID2 is different (and vice versa). As the screen shows, if we tell Access these two form the primary key, it will ensure we never duplicate the values in both fields between two rows.

So, a good question, and one that PCW reader Tom Boyd asked, is: under what circumstances is it worth accepting the added complexity of a compound primary key?

One answer is that the table above is a good example of bad practice. All I have done is to make the table more complex for no gain.

However, when used correctly, compound primary keys are wonderful. More than that, they are common because we use them to solve a common problem.

Databases are constructed to model the behaviour of the real world. In that world, we find real-world objects that have a many-to-many relationship between them.

Imagine a situation where we have many products for sale and many orders being placed by our customers. Each product can appear on many different orders and each order can have many different products on it.

Our database needs to be able to store this relationship, so we need a many-to-many relationship between the Order table and the Product table. We do this by creating a table that sits between Order and Product ­ I’ve called it OrderDetail.

Following the joins, we can see that Order number 1 was for two different products: three copies of ‘Gordon the Wonder Land Rover’ and two copies of ‘EF goes to France’. Order number 2 was for three products.

Suppose the customer who placed order number 1 rings back and adds four more copies of ‘Gordon the Wonder Land Rover’, we simply amend the Number field from 3 to 7.

We actively don’t want more than one row in OrderDetails that points to the same order and the same product; it would just be confusing. If we declare these two fields to be the primary key of OrderDetail, Access ensures that we can never do this.

There is an important point here. When people (myself included) first use a relational database, they often find that it seems obstructive. No matter what you try to do, the database engine interferes. You can’t change this, you mustn’t do that.

But, if the database is designed properly, it never gets in the way unless you try to do something foolish (such as adding the same product to an order twice).

Unnatural primary keys
Tom also mentioned my brief coverage of natural primary keys, which use a value that already exists in the real world and is guaranteed to be unique, such as the registration number of a car.

Even if you find a natural key, it’s always worth considering whether it is the best solution; just because there is a natural key doesn’t mean it’s definitely the best in every situation.

For instance, where you have a table of employees, each one with a National Insurance number, it’s tempting to use the NI number as the primary key.

However, NI numbers contain a mix of characters and numbers and will make queries slower. This is unlikely to be a problem in a small business, but it could become so with a very large table.

You may also have to cope with rare instances where employees don’t have a number. Store NI numbers by all means, but consider using a more compact numerical value/AutoNumber as the primary key.

Article tags

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Free typing tutor screenshot

Learn to type like a professional

Pecking away at the keyboard can be slow and offputting. We describe the ideal workstation and what software is available to get you up to speed as a touch typist

Zyxel Ethernet switch

How to upgrade a home network

It’s not just your computer that can benefit from an upgrade. Spending a few pounds can transform your network, making it faster and extending its range

Remote control

Which universal remote control?

A universal remote lets you control all your home entertainment from one place. Here's how to choose the right one

Question & Answer

Q.How do I stop Windows 7 search?

> Read the answer

Q.Is it a genuine call from Microsoft?

> Read the answer

Q.How can I turn Autoplay back on?

> Read the answer

Best deals on the web

img

Samsung 300E5A-A01DX

£449.99- Buy it now

img

Sony Vaio VPCF23P1E/B

£679.98- Buy it now

img

Acer Aspire 5733Z-P624G75Mikk (LX.RJW02.080)

£349.99- Buy it now

Great benefits for subscribers!

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

Bios

Basic Input Output System. Essential software built into every PC that connects the vital components....

Great shopping deals from Computeractive