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

后端 未结 7 2060
独厮守ぢ
独厮守ぢ 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 04:57

    I don't see any advantage in using ENUMS.

    They are harder to maintain and don't offer anything that a regular lookup table with proper foreign keys wouldn't allow you to do.

    0 讨论(0)
  • 2021-02-07 04:59

    Well, you don't see, because usually developers are using enums in programming languages such as Java, and the don't have their counterparts in database design.

    In database such enums are usually text or integer fields, with no constraints. Database enums will not be translated into Java/C#/etc. enums, so the developers see no gain in this.

    There are very many very good database features which are rarely used because most ORM tools are too primitive to support them.

    0 讨论(0)
  • 2021-02-07 05:06

    Generally speaking, enum is better for things that don't change much, and it uses slightly fewer resources, since there's no FK checks or anything like to execute on insert etc.

    Using a lookup table is more elegant and or traditional and it's much easier to add and remove options than an enum. It's also easier to mass change the values than an enum.

    0 讨论(0)
  • 2021-02-07 05:10

    A small advantage may lie in the fact, that you have a sort of UDT when creating an ENUM. A user defined type can be reused formally in many other database objects, e.g. in views, other tables, other types, stored procedures (in other RDBMS), etc.

    Another advantage is for documentation of the allowed values of a field. Examples:

    • A yes/no field
    • A male/female field
    • A mr/mrs/ms/dr field

    Probably a matter of taste. I prefer ENUMs for these kinds of fields, rather than foreign keys to lookup tables for such simple concepts.

    Yet another advantage may be that when you use code generation or ORMs like jOOQ in Java, you can use that ENUM to generate a Java enum class from it, instead of joining the lookup table, or working with the ENUM literal's ID

    It's a fact, though, that only few RDBMS support a formal ENUM type. I only know of Postgres and MySQL. Oracle or DB2 don't have it.

    0 讨论(0)
  • 2021-02-07 05:12

    Advantages:

    • Type safety for stored procedures: will raise a type error if argument can not be coerced into the type. Like: select court_martial('3LT') would raise a type error automatically.

    • Custom coalition order: In your example, officers could be sorted without a ranking id.

    0 讨论(0)
  • 2021-02-07 05:13

    A disadvantage of using something like an ENUM is that you can't get a list of all the available values if they don't happen to exist in your data table, unless you hard-code the list of available values somewhere. For example, if in your OFFICERS table you don't happen to have an MG on post there's no way to know the rank exists. Thus, when BG Blowhard is relieved by MG Marjorie-Banks you'll have no way to enter the new officer's rank - which is a shame, as he is the very model of a modern Major General. :-) And what happens when a General of the Army (five-star general) shows up?

    For simple types which will not change I've used domains successfully. For example, in one of my databases I've got a yes_no_domain defined as follows:

    CREATE DOMAIN yes_no_dom
      AS character(1)
      DEFAULT 'N'::bpchar
      NOT NULL
       CONSTRAINT yes_no_dom_check
         CHECK ((VALUE = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])));
    

    Share and enjoy.

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