Image of compound primary key
Compound keys can be very useful
R E L A T E D   C O N T E N T
ADVERTISEMENT

Using compound primary keys

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

Mark Whitehorn, Personal Computer World 10 Jul 2009
ADVERTISEMENT

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.


All Software Developer
Tags: Compound-primary-keys, Databases

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
| Computer People
Job Title: PHP Developer/Web Developer Location: Suffolk Notice: Immediate Salary Expectations: £35,000 A personable team player with PHP and web development skills is being offered an excellent opportunity to join an organisation based in the ... more >
| Computer People
Job Title: Senior Java Technical Architect Location: Sheffield Salary Expectations: £70,000 - £80,000 plus Share options A rare opportunity has recently been registered for a Senior Java Technical Architect to join what will be a ... more >
| Computer People
Job Title: Test Team Leader Location: Cambridge Salary Expectations: £20,000 - £45,000 An exciting and expanding consultancy in the Cambridge area is actively seeking to recruit a Test Team Leader who will take responsibility for ... more >
| Computer People
Job Title: Autonomy Search Database Developer Location: North Cambridgeshire Salary: £28,000 - £30,000 My client is a large, North Cambridgeshire based organisation with nearly 6000 users based from multiple sites. With on site parking and ... more >
More job opportunities