How important are lookup tables?

后端 未结 7 2021
你的背包
你的背包 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:28

    The answer depends a little if you are limited to small filing systems in MyNonSQL, or if you are thinking about SQL and large databases.

    In real Databases, where there are many apps using one database, and many users using different report tools (not just the apps) to access the data, standards, normalisation, and open architecture requirements are important.

    Despite the people who attempt to change the definition of "normalisation", etc. to suit the purpose, Normalisation has not changed.

    • if you have "Open" and "Closed" repeated in data tables, that is a simple Normalisation error. If you change those values you may have to update millions of rows, which is very limited design. Such values are commonly normalised into a Reference or Lookup table. It also saves space. The value "Open", "Closed" etc is no longer duplicated.

    • the second point is ease of change, if "Closed" were changed to "Expired", again, one row needs to be changed, and that is reflected in the entire database; whereas in the unnormalised files, millions of rows need to be changed.

    • Adding new values is simply a matter of inserting one row.

    • in Open Architecture terms, the Lookup table is an ordinary table. It exists in the (standard SQL) catalogue; any report tool can find it, as long as the PK::FK relation is defined, the report tool can find that as well.

    • Enum is only for the Non-SQLS. In SQL the Enum is a Lookup table.

    • The next point relates to the meaningfulness of the key. If the Key is meaningless to the user, fine, use an INT or TINYINT or whatever is suitable; number them incrementally; allow "gaps". But if the Key is meaningful to the user, do not use a meaningless number, do use the meaningful key. "M" and "F" for Male and Female, etc.

      • Now some people will get in to tangents re the permanence of PKs. That is a separate point. Yes, of course, always use a stable value for a PK. "M" and "F" are unlikely to change; if you have used {0,1,2,4,5,6}, well don't change it, why would you want to. Those values were supposed to be meaningless, only meaningful Key need to be changed.
        .
    • if you do use meaningful keys, use short alphabetic codes, that both users and developers can readily understand (and infer to long description from).

    • Since PKs are stable, particularly in Lookup tables, you can safely code:

      WHERE status_id = 'O'

      You do not have to join with the Lookup table and examine the Value "Open". That loses the value of the Lookup table in the code segments.

    SQL is a cumbersome language, especially when it comes to joins. But that is all we have, so we need to just accept the encumbrance and deal with it. Your example code is fine. But simpler forms can do the same thing. A report tool would generate:

    SELECT  p.*,
             s.name
        FROM posts p, 
             status s
        WHERE p.status_id = s.status_id 
        AND   p.status_id = 'O'

    • For banking systems, where we use short codes which are meaningful (since they are meaningful, we do not change them with the seasons, we just add to them), given a Lookup table such as (carefully chosen, similar to ISO Country Codes):

      Eq   Equity
      EqCS Equity/Common Share
      O    Over The Counter
      OF   OTC/Future

      Code such as this is common:

      WHERE InstrumentTypeCode LIKE "Eq%"

    And the users would choose the value from a drop-down that displayed "Open", "Closed", etc., not {0,1,2,4,5,6}, not {M, F, U}. Both in the apps, and in the report tool. Without a lookup table, you can't do that.

    Last, If the database was large, and supported BI or DSS or OLAP functions (the highly Normalised databases do), then the Lookup table is actually a Dimension or Vector, in Dimension-Fact analyses. If it was not there, then it would have to be added in, to satisfy the requirements of that software, before such analyses can be mounted.

提交回复
热议问题