Postgres ENUM 数据类型还是检查约束? ?

我一直在将一个 MySQL db 迁移到 Pg (9.1) ,并通过在 Pg 中创建一个新的数据类型来模拟 MySQL ENUM 数据类型,然后使用该数据类型作为列定义。我的问题是——我可以,而且是否使用检查约束来代替?实现 MySQLENUM 类型是为了在行中强制执行特定的值条目。这可以通过支票限制来实现吗?如果答案是肯定的,情况会更好(还是更糟) ?

96216 次浏览

PostgreSQL has enum types, works as it should. I don't know if an enum is "better" than a constraint, they just both work.

I'm hoping somebody will chime in with a good answer from the PostgreSQL database side as to why one might be preferable to the other.

From a software developer point of view, I have a slight preference for using check constraints, since PostgreSQL enum's require a cast in your SQL to do an update/insert, such as:

INSERT INTO table1 (colA, colB) VALUES('foo', 'bar'::myenum)

where "myenum" is the enum type you specified in PostgreSQL.

This certainly makes the SQL non-portable (which may not be a big deal for most people), but also is just yet another thing you have to deal with while developing applications, so I prefer having VARCHARs (or other typical primitives) with check constraints.

As a side note, I've noticed that MySQL enums do not require this type of cast, so this is something particular to PostgreSQL in my experience.

Based on the comments and answers here, and some rudimentary research, I have the following summary to offer for comments from the Postgres-erati. Will really appreciate your input.

There are three ways to restrict entries in a Postgres database table column. Consider a table to store "colors" where you want only 'red', 'green', or 'blue' to be valid entries.

  1. Enumerated data type

    CREATE TYPE valid_colors AS ENUM ('red', 'green', 'blue');
    
    
    CREATE TABLE t (
    color VALID_COLORS
    );
    

    Advantages are that the type can be defined once and then reused in as many tables as needed. A standard query can list all the values for an ENUM type, and can be used to make application form widgets.

    SELECT  n.nspname AS enum_schema,
    t.typname AS enum_name,
    e.enumlabel AS enum_value
    FROM    pg_type t JOIN
    pg_enum e ON t.oid = e.enumtypid JOIN
    pg_catalog.pg_namespace n ON n.oid = t.typnamespace
    WHERE   t.typname = 'valid_colors'
    
    
    enum_schema | enum_name     | enum_value
    -------------+---------------+------------
    public      | valid_colors  | red
    public      | valid_colors  | green
    public      | valid_colors  | blue
    

    Disadvantages are, the ENUM type is stored in system catalogs, so a query as above is required to view its definition. These values are not apparent when viewing the table definition. And, since an ENUM type is actually a data type separate from the built in NUMERIC and TEXT data types, the regular numeric and string operators and functions don't work on it. So, one can't do a query like

    SELECT FROM t WHERE color LIKE 'bl%';
    
  2. Check constraints

    CREATE TABLE t (
    colors TEXT CHECK (colors IN ('red', 'green', 'blue'))
    );
    

    Two advantage are that, one, "what you see is what you get," that is, the valid values for the column are recorded right in the table definition, and two, all native string or numeric operators work.

  3. Foreign keys

    CREATE TABLE valid_colors (
    id SERIAL PRIMARY KEY NOT NULL,
    color TEXT
    );
    
    
    INSERT INTO valid_colors (color) VALUES
    ('red'),
    ('green'),
    ('blue');
    
    
    CREATE TABLE t (
    color_id INTEGER REFERENCES valid_colors (id)
    );
    

    Essentially the same as creating an ENUM type, except, the native numeric or string operators work, and one doesn't have to query system catalogs to discover the valid values. A join is required to link the color_id to the desired text value.

One of the big disadvantages of Foreign keys vs Check constraints is that any reporting or UI displays will have to perform a join to resolve the id to the text.

In a small system this is not a big deal but if you are working on a HR or similar system with very many small lookup tables then this can be a very big deal with lots of joins taking place just to get the text.

My recommendation would be that if the values are few and rarely changing, then use a constraint on a text field otherwise use a lookup table against an integer id field.

As other answers point out, check constraints have flexibility issues, but setting a foreign key on an integer id requires joining during lookups. Why not just use the allowed values as natural keys in the reference table?

To adapt the schema from punkish's answer:

CREATE TABLE valid_colors (
color TEXT PRIMARY KEY
);


INSERT INTO valid_colors (color) VALUES
('red'),
('green'),
('blue');


CREATE TABLE t (
color TEXT REFERENCES valid_colors (color) ON UPDATE CASCADE
);

Values are stored inline as in the check constraint case, so there are no joins, but new valid value options can be easily added and existing values instances can be updated via ON UPDATE CASCADE (e.g. if it's decided "red" should actually be "Red", update valid_colors accordingly and the change propagates automatically).

From my point of view, given a same set of values

  1. Enum is a better solution if you will use it on multiple column
  2. If you want to limit the values of only one column in your application, a check constraint is a better solution.

Of course, there is a whole lot of other parameters which could creep in your decision process (typically, the fact that builtin operators are not available), but I think these two are the most prevalent ones.