简体   繁体   English

在MySQL中更改枚举类型

[英]Changing an enum type in mysql

I'm working with a mysql database table that stores a 6 bit value as an enum. 我正在使用将6位值存储为枚举的mysql数据库表。 Only 20 or so of the values were specified, with the remainder having names like "type_30". 仅指定了20个左右的值,其余的名称则类似“ type_30”。 I'm now changing the column to add descriptive text to some of the values using alter TABLE table_name modify COLUMN column_name enum( 'text', ... ); 我现在正在更改列,以使用alter TABLE table_name modify COLUMN column_name enum( 'text', ... );向某些值添加描述性文本alter TABLE table_name modify COLUMN column_name enum( 'text', ... ); The table is not all that big (about 13e6 rows), but the alter command has been running for quite some time now. 该表不是很大(大约13e6行),但是alter命令已经运行了一段时间。 I'm a bit surprised, since it would seem that changing the enumeration type should only require modifying metadata for the table (so the number of rows in the table should be irrelevant.) 我有点惊讶,因为似乎更改枚举类型只需要修改表的元数据(因此表中的行数应该无关紧要)。

Why does the number of rows in the column matter for this type of operation? 为什么列中的行数对这种类型的操作很重要? Is there a simple way for me to quickly change the labels of the enum? 我有一种简单的方法可以快速更改枚举的标签吗?

From MySQL documentation : MySQL文档

Changing the definition of an ENUM or SET column by adding new enumeration or set members to the end of the list of valid member values, as long as the storage side of the data type does not change. 只要在数据类型的存储端不变,就可以通过在有效成员值列表的末尾添加新的枚举或set成员来更改ENUM或SET列的定义。 For example, adding a member to a SET column that has 8 members changes the required storage per value from 1 byte to 2 bytes; 例如,将成员添加到具有8个成员的SET列中,将每个值所需的存储从1字节更改为2字节; this will require a table copy. 这将需要一个表副本。 Adding members in the middle of the list causes renumbering of existing members, which requires a table copy. 在列表中间添加成员会导致对现有成员重新编号,这需要表副本。

Enums are stored as an integer. 枚举存储为整数。 For example, if your enum was ('yes', 'no), 'yes' will be 1 and 'no' will be 2. 例如,如果您的枚举是('yes','no),则'yes'将为1,而'no'将为2。

Inserting new items in the middle requires renumbering, so MySQL will create a new table and copy rows over to change the data. 在中间插入新项目需要重新编号,因此MySQL将创建一个新表并复制行以更改数据。 It's not simply a change to the metadata. 这不仅仅是对元数据的更改。

There is no quick way to change the existing labels, since it affects the data. 由于会影响数据,因此无法快速更改现有标签。 However, if you only add values to the end of the enum, only a metadata change is required. 但是,如果仅将值添加到枚举的末尾,则仅需要更改元数据。

There is no simple way to quickly change the labels for an enum column. 没有简单的方法可以快速更改枚举列的标签。 Running MODIFY COLUMN like you did is not updating the labels, it's actually trying to remap all of the existing data to fit your updated enum values. 像您一样运行MODIFY COLUMN 并不更新标签,它实际上是在尝试重新映射所有现有数据以适合您更新的枚举值。

For example, if your column was ENUM('blue','red','yellow') before then you would have the following index values: 例如,如果之前的列为ENUM('blue','red','yellow'),则将具有以下索引值:

1 = blue
2 = red
3 = yellow

If you ran alter TABLE table_name modify COLUMN column_name enum( 'red','yellow','blue' ); 如果您运行alter TABLE table_name modify COLUMN column_name enum( 'red','yellow','blue' ); it would update the index values as follows: 它将更新索引值,如下所示:

1 = red
2 = yellow
3 = blue

Which is what you want. 你想要哪一个。 But it would also go back and update all of the existing rows of the table to remap the index values: 但是它也会返回并更新表的所有现有行以重新映射索引值:

1 -> 3
2 -> 1
3 -> 2

Which you do not want. 你不想要的。

If you simply want to update the labels without modifying the existing row data, then you will need to modify the FRM file. 如果只想更新标签而不修改现有行数据,则需要修改FRM文件。 Here's one description of how to do that: 这是有关如何执行此操作的说明:

https://dba.stackexchange.com/questions/6547/can-i-rename-the-values-in-a-mysql-enum-column-in-one-query https://dba.stackexchange.com/questions/6547/can-i-rename-the-values-in-a-mysql-enum-column-in-one-query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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