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

后端 未结 7 2072
独厮守ぢ
独厮守ぢ 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: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.

提交回复
热议问题