database design: a 'code' table that get referenced by other entities

后端 未结 2 1622
粉色の甜心
粉色の甜心 2021-01-24 00:52

I am building a database as a simple exercise, it could be hosted on any database server, so I am trying to keep things as much standard as possible. Basically what I would like

2条回答
  •  情书的邮戳
    2021-01-24 01:13

    I have worked on systems with a single table for all codes and others with one table per code. I definitely prefer the latter approach.

    The advantages of a table per code are:

    1. Foreign keys. As you have already spotted it is not possible to enforce compliance to permitted values through foreign keys with a single table. Using check constraints is an alternative approach but it has a higher maintenance cost.
    2. Performance. Code lookups are not normally a performance bottle neck, but it undoubtedly helps the optimizer to make sensible decisions about execution paths if it knows it is retrieving records from a table with four rows rather than four hundred.
    3. Code groups. Sometimes we want to organise a code into sub-divisions, usually to make it easier to render complex lists of values. If we have a table per code we have more flexibility when it comes to structure.

    In addition I notice that you want to be able to deploy "on any database server". In that case avoid triggers. Triggers are usually bad news in most scenarios, but they have product-specific syntax.

提交回复
热议问题