简体   繁体   中英

Do we need to drop index when updating an Enum indexed column

We have a MySQL table that contains a column of type Enum with an index on it.

We wish to update the Enum to add another value. Will we need to drop the the index and create it again?

If you use ALTER TABLE... MODIFY COLUMN... , you can add a new option(s).

If you add them on the end of the list, the ALTER will be 'instant', regards of table size. Both the data and the existing index(es) are still valid.

If you rearrange the list in any way, it will trigger an automatic rebuild of the index(es).

Think of it this way: ENUM('a', 'b', 'x') is implemented in a byte where 'a' is represented as 0 , b as 1, x as 2. Modifying it to ENUM('a', 'b', 'x', 'z') is like saying "Now this column might have the number 3 (representing 'z') in it.

If you JOIN two tables ON the ENUM , be aware that the JOIN will use the underlying numeric values (0...) for comparison. So all tables should be ALTERed 'simultaneously'.

(This discussion probably applies to all Engines in MySQL/MariaDB, but may not apply to non-MySQL/MariaDB vendors.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM