Configuration data: single-row table vs. name-value-pair table

configurationdata structuresdatabase-design

Let's say you write an application that can be configured by the user. For storing this "configuration data" into a database, two patterns are commonly used.

  1. The single-row table

      CompanyName  |  StartFullScreen  |  RefreshSeconds  |  ...
    ---------------+-------------------+------------------+--------
      ACME Inc.    |        true       |       20         |  ...
    
  2. The name-value-pair table

      ConfigOption   |   Value
    -----------------+-------------
     CompanyName     | ACME Inc.
     StartFullScreen | true (or 1, or Y, ...)
     RefreshSeconds  | 20
     ...             | ...
    

I've seen both options in the wild, and both have obvious advantages and disadvantages, for example:

  • The single-row tables limits the number of configuration options you can have (since the number of columns in a row is usually limited). Every additional configuration option requires a DB schema change.
  • In a name-value-pair table everything is "stringly typed" (you have to encode/decode your Boolean/Date/etc. parameters).
  • (many more)

Is there some consensus within the development community about which option is preferable?

Best Answer

I personally prefer the single-row tables for most things. While it's true that it is less flexible, unless you are expecting dynamic behavior, it's perfectly acceptable to add extra columns later if you need to. In a way, it's the equivalent of using a dictionary/map to hold name-value pairs vs having class members when programming. Granted, it's not a perfect metaphor, but many of the advantages and disadvantages are paralleled when you think about it.

So would you use a dictionary/map over class members? Probably not unless you had reason to think the amount of data to be represented is entirely adaptable, much like having a name-value pair table.

Related Topic