SQL: Advantages of an ENUM vs. a one-to-many relationship?

后端 未结 7 2071
独厮守ぢ
独厮守ぢ 2021-02-07 04:14

I very rarely see ENUM datatypes used in the wild; a developer almost always just uses a secondary table that looks like this:

CREATE TABLE officer_ranks (
id in         


        
相关标签:
7条回答
  • 2021-02-07 05:21

    Example shown using PostgreSQL, but other RDBMS's have similar syntax

    That's incorrect. It is not an ISO/IEC/ANSI SQL requirement, so the commercial databases do not provide it (you are supposed to provide Lookup tables). The small end of town implement various "extras", but do not implement the stricter requirements, or the grunt, of the big end of town.

    We do not have ENUMs as part of a DataType either, that is absurd.

    The first disadvantage of ENUMs is that is it non-standard and therefore not portable.

    The second big disadvantage of ENUMs is, that the database is Closed. The hundreds of Report Tools that can be used on a database (independent of the app), cannot find them, and therefore cannot project the names/meanings. If you had a normal Standard SQL Lookup table, that problem is eliminated.

    The third is, when you change the values, you have to change DDL. In a Normal Standard SQL database, you simply Insert/Update/Delete a row in the Lookup table.

    Last, you cannot easily get a list of the content of the ENUM; you can with a Lookup table. More important, you have a vector to perform any Dimension-Fact queries with, eliminating the need for selecting from the large Fact table and GROUP BY.

    0 讨论(0)
提交回复
热议问题