TINYINT as an alternative
suggest changeLet’s say we have
type ENUM('fish','mammal','bird')
An alternative is
type TINYINT UNSIGNED
plus
CREATE TABLE AnimalTypes (
type TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT "('fish','mammal','bird')",
PRIMARY KEY(type),
INDEX(name)
) ENGINE=InnoDB
which is a lookup table, not a many-to-many table.
Comparison, and whether better or worse than ENUM:
- (worse) INSERT: need to lookup the
type - (worse) SELECT: need to JOIN to get the string (ENUM gives you the string with no effort)
- (better) Adding new types: Simply insert into this table. With ENUM, you need to do an ALTER TABLE.
- (same) Either technique (for up to 255 values) takes only 1 byte.
- (mixed)
TINYINTaccepts any integer in range. A non-strictENUMturns an invalid value into''. Strict SQL mode rejects it. A foreign key from theTINYINTto the lookup table rejects unknown codes. The join still has a cost. Foreign keys are not free.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents