简体   繁体   English

MySql-将字段枚举类型更改为tinyint时的奇怪行为

[英]MySql - Strange behavior when altering field enum type to tinyint

I'm updating all of my table fields with enum type to tinyint(1), but I'm seeing some strange behavior. 我正在将所有具有枚举类型的表字段更新为tinyint(1),但看到一些奇怪的行为。

Say field_x type is enum('0','1','2') ; 假设field_x类型为enum('0','1','2') ;

After altering the table ( ALTER TABLE a MODIFY field_x tinyint(1) NOT NULL DEFAULT 1 ), fields with original value 0 now have 1, fields with original value 1 now have 2... 更改表后( ALTER TABLE a MODIFY field_x tinyint(1) NOT NULL DEFAULT 1 ),原始值0的字段现在具有1,原始值1的字段现在具有2 ...

Does anybody know what is causing this? 有人知道是什么原因造成的吗?

The underlying issue is the how enum is stored in mysql 根本问题是如何在mysql中存储枚举

For enum('0','1','2') MySQL stores enum values internally as integer keys. 对于enum('0','1','2') MySQL在内部将枚举值存储为整数键。

So for above indexes are 所以对于以上指标是

0 = 1
1 = 2
2 = 3

So when you alter the colum to tinyint then the saved values which are as string being converted to the corresponding indexes for tinyint and you are getting 因此,当您将colum更改为tinyint然后将保存为字符串的值转换为tinyint的相应索引,您将得到

1 for 0, 2 for 1 ....

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

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