Lookup Tables Best Practices: DB Tables… or Enumerations

后端 未结 9 1841
忘掉有多难
忘掉有多难 2020-12-07 09:27

If we have to store the available positions at a company (i.e. Manager, Team Lead, ... etc). What are the best practices for storing it? I have two opinions with comments...

9条回答
  •  误落风尘
    2020-12-07 09:42

    I tend to almost always put this kind of thing in a table. If it's needed a lot I'll cache it. If I need to programmatically refer to some of them I'll then create an enum which has its values set to the key value of the lookup item.

    For example, a surrogate primary key (usually so my data access layer can seemlessly add/update it), a field for the value of the item, plus a "candidate" (candidate being in quotes since it's not required, so it's either unique or null). That way I can use the enum in quotes to refer to specific/important items in the table, but still have the flexibility of letting end users add new items. The specifics of the data model used really depend on your preference (some people may be screaming at me right now for not just using the "candidate" field as the true primary key).

提交回复
热议问题