Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres ENUM data type or CHECK CONSTRAINT?

I have been migrating a MySQL db to Pg (9.1), and have been emulating MySQL ENUM data types by creating a new data type in Pg, and then using that as the column definition. My question -- could I, and would it be better to, use a CHECK CONSTRAINT instead? The MySQL ENUM types are implemented to enforce specific values entries in the rows. Could that be done with a CHECK CONSTRAINT? and, if yes, would it be better (or worse)?

like image 822
punkish Avatar asked Jun 06 '12 22:06

punkish


People also ask

What is enum data type in PostgreSQL?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.

Should you use enum Postgres?

The advantages of enums are: Performance is better. You can just display what you get out of the core table instead of either having a separate lookup table that translates a code to a value or having app logic that translates a code to a value. This can be especially useful in datawarehouse applications.

How are enum stored in PostgreSQL?

When using PostgreSQL, each ENUM type is registered in the system catalogs and can be used anywhere PostgreSQL expects a type name. Internally, the ENUM values are stored as integers. It is important to realize that each ENUM type in PostgreSQL is registered in the system catalogs.

Is enum a constraint?

Unless strict mode is disabled (not recommended, but see Section 5.1. 11, “Server SQL Modes”), the definition of a ENUM or SET column acts as a constraint on values entered into the column.


1 Answers

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.

like image 131
punkish Avatar answered Oct 08 '22 12:10

punkish