views:

69

answers:

2

Two tables in my database are as follows:

[Employee] Table:
 Id (Primary Key, Autoincrement)
 FirstName
 LastName
 Status

[Status] Table:
 Status (Primary Key)

Status is one of the following: "FullTime" "Contractor" "Terminated"

How should [Employee].Status reference [Status].Status as foreign key? I see two ways of doing this:

  1. [Employee].Status points directly to [Status].Status
  2. I add an Id column to [Status] table and make it PK/Autoincrement. Then [Employee].Status points to [Status].Id. This means I have to do a join in order to get the status text.

Other tables may also reference the Status table. Is one of the two methods the 'correct' way of doing things or are both a matter of design?

+4  A: 

It's basically a matter of design, but it's generally better to add an ID field to the Status table. This allows you to make changes to the Status values (spelling corrections, language translation, change of term to clarify meaning, etc) without having to update the data in the tables that reference it.

Also, if you link to the string field, then the linking field needs enough space to store the longest status string. Linking to the ID means you just have to store an integer (or at worst, a GUID) in the linking field.

Andrew Cooper
Perfect, this is exactly what I needed. Can't accept your answer yet -- 5 mins.
Harper
If Status is basically a static lookup table with a small number of values, you might want to use a smaller datatype for its id than you would use in a table like Employee that you would want to allow more rows. One byte would a lot of different status values.
Alex Blakemore
A: 

a third option in more complex tables where you want to both delete records and update names without losing reference would be something like

[Employee] Table:
 Id (Primary Key, Autoincrement)
 FirstName
 LastName
 StatusNumber

[Status] Table:
Id (PK)
Number
Name
Nico
I don't understand why you propose joining from Employee to Status on a field that is not the primary key in the Status table? Seems like a redundant field and a risk of error with no benefit. As is, it would be possible for two rows int he Status table to have the same Number. If you prevent that with a constraint, how is that different than just using the Id?
Alex Blakemore