If there is column with values from some finite set S
, for example S = { alive, dead, unknown }
for a life_state
column in a persons
table, when is it beneficial to add a reference table?
Options:
Storing duplicated static data in a
life_state
columnsimple strings: code on the application layer level would need to exist to get possible data values since
SELECT DISTINCT
could be missing some values.ENUM
: if you want to get all possible data values forlife_state
then you would need to runSHOW COLUMN..
fromINFORMATION_SCHEMA
to get all possible data values and parse that data on the application layer level or have duplicate code on the application layer level. If a new life_state value needed to be added we would need toALTER
the table and lock the whole table up in the process while it is being altered. No additional data could be stored for eachlife_state
value should it be necessary.
Normalized Create a reference table
life_states
and a linking table back topersons
table. The possible values could be derived fromSELECT name from life_states;
which would return 3 rows. Additional joins are now required to get thelife_state
for anypersons
instance. This is not only slower but requires longer queries/more code on the application layer level. Any additional values could easily be added if necessary, though it isn't anticipated. Furthermore, if some other data needed to be added to the life_state type it could be easily added.
Other considerations:
- INT/ENUM indexing might be quicker/take up less space strings.
Related discussions:
enum
support for example.