MySQL ENUM type vs join tables

后端 未结 5 1555
北恋
北恋 2020-11-27 13:04

My requirement

A table needs to maintain a status column.

This column represents one of 5 states.


initial desi

5条回答
  •  感情败类
    2020-11-27 13:24

    • Changing the set of values in an ENUM requires an ALTER TABLE which might cause a table restructure -- an incredibly expensive operation (the table restructure doesn't happen if you simply add one new value to the end of the ENUM definition, but if you delete one, or change the order, it does a table restructure). Whereas Changing the set of values in a lookup table is as simple as INSERT or DELETE.

    • There's no way to associate other attributes with the values in an ENUM, like which ones are retired, and which ones are eligible to be put in a drop-down list in your user interface. However, a lookup table can include additional columns for such attributes.

    • It's very difficult to query an ENUM to get a list of distinct values, basically requiring you to query the data type definition from INFORMATION_SCHEMA, and parsing the list out of the BLOB returned. You could try SELECT DISTINCT status from your table, but that only gets status values currently in use, which might not be all values in the ENUM. However, if you keep values in a lookup table, it's easy to query, sort, etc.

    I'm not a big fan of ENUM, as you can tell. :-)

    The same applies to CHECK constraints that simply compare a column to a fixed set of values. Though MySQL doesn't support CHECK constraints anyway.

    Update: MySQL 8.0.16 now implements CHECK constraints.

提交回复
热议问题