1

I am designing a project based database for the first time and need some advice on how best to determine whether to use a single primary key or composite primary key?

We have many existing separate project based databases but have never combined them into one, even though they follow the same structure.

All tables are based on 2 main fields

1) Project code

2) Item number

These always go together and are present in all tables. Both the project code and the item code are very meaningful to every user because they will be describing physical objects which are identified by their project code and item number (actually written on them).

The project code is always unique whereas the item number ranges from 1 to n (where n is any number but never repeated). Start a new project and the item number starts at 1 again.

So the basis of the database is as follows.

Table: Project List

Fields: Project Code (PK), other fields...

Table: Item list

Fields: Project code (FK), Item number (PK?), other fields...

Table: any other table

Fields: Project code (FK), Item number (FK?), other fields...

It makes sense to me to have the Project code as a primary key However the item code can't be a primary key as it is not unique. Item numbers 1-n will be repeated every project but is the main reference to everyone.

My first thought would be to create a surrogate primary key called Item_ID which auto increments in the item list table and then use that as a foreign key in all other tables,

so... it would look like

Table: Project List

Fields: Project Code (PK), other fields...

Table: Item list

Fields: Project code (FK), Item_ID (PK), Item number, other fields...

Table: any other tables

Fields: Project code (FK), Item_ID (FK), Item number, other fields...

However the item_ID key would be meaningless to everyone using it and they would have to keep asking what ID number is my item number in order to link it to its parent table.

My second thought was to make the item number unique by adding the project code to the item number in the formula PROJECT CODE_ITEM NUMBER E.G. if the project code is "Project1" then the item number could be written "Project1_24". That would make item number unique and therefore can be used as a primary key.

(Just assume the child tables have their own PK's).

So they would look like this:

Table: Project List

Fields: Project Code (PK), other fields...

Table: Item list

Fields: Project code (FK), Item number (PK), other fields...

Table: any other tables

Fields: Project code (FK), Item number (FK), other fields...

This works as it still retains the item number which is meaningful to every user. But it seems a bit long winded as they would have to enter PROJECT CODE_ITEM NUMBER each time to make the correct reference. They are used to just writing the item number.

..maybe there is some way of automating this process so they can still write in the number but the database adds the project code behind it..I don't know?

My third idea is to make Project code and item number a composite key in the item list table and create a surrogate key for the project code in the project list table.

So it looks like this:

Table: Project List

Fields: Project_ID (PK), other fields...

Table: Item list

Fields: Project_ID (FK), Project code (CPK), Item number, (CPK), other fields...

Table: any other table

Fields: Project_ID (FK), Project code (CFK), Item number, (CFK), other fields...

This would be ok as entering the project ID would be fairly straight forward and project code and item number makes a natural key in when used in combination. They will always go together in any record and be unique.

But which method should I use?

  • Whether to use natural keys or a surrogate key is something that people argue about all the time. I'd say go with the composite key, but you should search for "Surrogate keys" on this site and you'll get reasons on both sidfes of the argument. e.g. http://stackoverflow.com/questions/63090/surrogate-vs-natural-business-keys – Darius X. May 31 '13 at 19:35

1 Answers1

2

For your item list table I would think you'd want a composite primary key, because you will have multiple projects listed in this database and since the item code is not unique you will need to combine it with the project code to uniquely identify it.

If you don't have the composite key and you try to map to the time code only which is not unique, you will end up with what is known as a Cartesian product - rows and rows and rows of nonsense when you query the database.

Data modeling is a lot of fun and a well-modeled database is well worth the time to do right. There are books and probably tutorials on the web that will help you with this and won't take too long to read.

Hope this helps.

Deborah Smith
  • 41
  • 1
  • 3