Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookup Tables Best Practices: DB Tables... or Enumerations

Generally you should only use enumeration where there is a clear set of items that will not change, e.g. primary colours, or continent names. Otherwise lookup tables with appropriately implemented foreign keys are pretty much always the best option.

There is a possible variation on the lookup table option where you potentially have a large number of lookup tables for simple id/value relationships. A domain/lookup table pair can dramatically reduce this the number of tables required, albeit with some additional coding complexity. In this case you'd have a domain table

DomainID int identity
Domain   varchar(255)

and a key/value table

DomainID int
ID       int identity
Value    varchar(255)

Hence a row is added to the Domain table corresponding to each lookup table that you would otherwise use, and all (key-domain)/value pairs added to the value table. Apart from simplifying the database structure this approach also has the advantage that 'lookup tables' can be created in the application code dynamically, which in some applications can be extremely useful.


Option 1: Storing it as DB table with columns ID and Name, and deal with it using queries and joins is the minimum that you should do.

From "Five Simple Database Design Errors You Should Avoid":

While application-enforced integrity is sometimes favored by developers, it remains true that the DBMS must still be the centralized enforcer of all integrity.

The recommended best practice is to implement your database as if it knows nothing about the user interface. That is, the database should enforce all rules pertaining to the data; in this case, the database should be enforcing what values are appropriate. To enumerate what values are appropriate, a lookup table for each type of lookup value is usually the way to go. Many times, applications come and go, yet the database remains and is reused.

If you want to enforce enumerations in the application, you can certainly do so, but whatever you do, make sure the database does its job as a database and maintains the integrity of the data. If it's troublesome, go through the trouble; you are laying the groundwork. In "Database Design and the Leaning Tower of Pisa," the writer emphasizes why laying the groundwork properly in a database is so very important.

I hope this helps.


I always go for the database option as that has several advantages, one of the main ones being you can change the names/descriptions of items in the look-up lists without having to change any code. Also agree with having a single table as opposed to lots of little tables, that way you code one single routine to retrieve the values from the database which cuts down on maintenance costs. Having a single routine means you can invest more effort in making it perform well.

Further to Cruachan's reply above, you can get away with one table if you have a parent-child relationship where the rows with no parent describe the domain. All the records belonging to a domain have the domain row as their parent.

ID           int autonumber -- integer primary key for performance
DomainID     int            -- null for domains
Name         varchar(100)   -- Name of the item
Description  varchar(255)

So for example a list of currencies could contain:

 ID    DomainID   Name              Description

 100   NULL       ISO Currencies    List of ISO Currency codes
 101   100        BHD               Bahrain Dinar
 102   100        GBP               British Pound
 103   100        EUR               Euro  
 104   100        USD               US Dollar

I tend to go for the database option as this enables easy querying with meaningful data (i.e. names rather than ID's).

If I am fairly confident that the values will not change then I will enumerate them in the application as wellas it makes development much easier when you do not have to remember the ID of an item and also makes the code much more readable.

This approach allows me to choose whether to include the lookup table in queries or not. For example I would include it in a report query where I want to display the lookup value but may not include it when loading data in my application if I can infer it from the enumeration instead.

Obviously if the values are subject to change or modification enumeration may not be possible.

Only you can judge the impact of UI culture, I am 100% certain of the culture of my users so do not have to worry about it too much :).