How important are lookup tables?

后端 未结 7 2026
你的背包
你的背包 2020-12-08 02:51

A lot of the applications I write make use of lookup tables, since that was just the way I was taught (normalization and such). The problem is that the queries I make are of

7条回答
  •  广开言路
    2020-12-08 03:24

    For look-up tables I use a sensible primary key -- usually just a CHAR(1) that makes sense in the domain with an additional Title (VARCHAR) field. This can maintain relationship enforcement while "keeping the SQL simple". The key to remember here is the look-up table does not "contain data". It contains identities. Some other identities might be time-zone names or assigned IOC country codes.

    For instance gender:

    ID Label
    M  Male
    F  Female
    N  Neutral
    
    select * from people where gender = 'M'
    

    Alternatively, an ORM could be used and manual SQL generation might never have to be done -- in this case the standard "int" surrogate key approach is fine because something else deals with it :-)

    Happy coding.

提交回复
热议问题