如何向 MySQL 中的 ENUM 类型列添加更多成员?
MySQL 参考手册没有提供关于如何执行此操作的明确示例。
我有一个 ENUM 类型的国家/地区名称列,我需要向其中添加更多国家/地区。实现此目的的正确 MySQL 语法是什么?
这是我的尝试:
ALTER TABLE carmake CHANGE country country ENUM('Sweden','Malaysia');
我收到的错误是:ERROR 1265 (01000): Data truncated for columns 'country' at row 1.
country
列是 ENUM 类型列在上述声明中。
显示创建表输出:
mysql> SHOW CREATE TABLE carmake;
+---------+---------------------------------------------------------------------+
| Table | Create Table
+---------+---------------------------------------------------------------------+
| carmake | CREATE TABLE `carmake` (
`carmake_id` tinyint(4) NOT NULL AUTO_INCREMENT,
`name` tinytext,
`country` enum('Japan','USA','England','Australia','Germany','France','Italy','Spain','Czech Republic','China','South Korea','India') DEFAULT NULL,
PRIMARY KEY (`carmake_id`),
KEY `name` (`name`(3))
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1 |
+---------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
从carmake中选择不同的国家输出:
+----------------+
| country |
+----------------+
| Italy |
| Germany |
| England |
| USA |
| France |
| South Korea |
| NULL |
| Australia |
| Spain |
| Czech Republic |
+----------------+
The MySQL reference manual does not provide a clearcut example on how to do this.
I have an ENUM-type column of country names that I need to add more countries to. What is the correct MySQL syntax to achieve this?
Here's my attempt:
ALTER TABLE carmake CHANGE country country ENUM('Sweden','Malaysia');
The error I get is: ERROR 1265 (01000): Data truncated for column 'country' at row 1.
The country
column is the ENUM-type column in the above-statement.
SHOW CREATE TABLE OUTPUT:
mysql> SHOW CREATE TABLE carmake;
+---------+---------------------------------------------------------------------+
| Table | Create Table
+---------+---------------------------------------------------------------------+
| carmake | CREATE TABLE `carmake` (
`carmake_id` tinyint(4) NOT NULL AUTO_INCREMENT,
`name` tinytext,
`country` enum('Japan','USA','England','Australia','Germany','France','Italy','Spain','Czech Republic','China','South Korea','India') DEFAULT NULL,
PRIMARY KEY (`carmake_id`),
KEY `name` (`name`(3))
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1 |
+---------+---------------------------------------------------------------------+
1 row in set (0.00 sec)
SELECT DISTINCT country FROM carmake OUTPUT:
+----------------+
| country |
+----------------+
| Italy |
| Germany |
| England |
| USA |
| France |
| South Korea |
| NULL |
| Australia |
| Spain |
| Czech Republic |
+----------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的代码对我有用。这是我的测试用例:
您看到什么错误?
FWIW 这也可以:
我实际上会推荐一个国家/地区表而不是枚举列。您可能有数百个国家/地区,这将构成一个相当大且尴尬的枚举。
编辑:现在我可以看到您的错误消息:
我怀疑您的国家/地区列中有一些值未出现在您的
ENUM
中。以下命令的输出是什么?另一个编辑:以下命令的输出是什么?
是
STRICT_TRANS_TABLES
还是STRICT_ALL_TABLES
?这可能会导致错误,而不是 MySQL 在这种情况下通常会给出的警告。另一种编辑:好的,我现在看到表中肯定有新
ENUM
中没有的值。新的ENUM
定义仅允许'Sweden'
和'Malaysia'
。该表包含'USA'
、'India'
和其他几个。最后编辑(也许):我认为你正在尝试这样做:
Your code works for me. Here is my test case:
What error are you seeing?
FWIW this would also work:
I would actually recommend a country table rather than enum column. You may have hundreds of countries which would make for a rather large and awkward enum.
EDIT: Now that I can see your error message:
I suspect you have some values in your country column that do not appear in your
ENUM
. What is the output of the following command?ANOTHER EDIT: What is the output of the following command?
Is it
STRICT_TRANS_TABLES
orSTRICT_ALL_TABLES
? That could lead to an error, rather than the usual warning MySQL would give you in this situation.YET ANOTHER EDIT: Ok, I now see that you definitely have values in the table that are not in the new
ENUM
. The newENUM
definition only allows'Sweden'
and'Malaysia'
. The table has'USA'
,'India'
and several others.LAST EDIT (MAYBE): I think you're trying to do this:
重要提示:这是一个遗留答案
我与 Asaph 的讨论可能不太清楚,因为我们来回了很多次。
我想我可以为将来可能面临类似情况的其他人澄清我们的讨论结果,并从中受益:
ENUM
类型的列是非常难以操作的野兽。我想将两个国家/地区(马来西亚和瑞典)添加到我的 ENUM 中现有的国家/地区集中。看来MySQL 5.1(我正在运行的)除了我想要的之外只能通过重新定义现有的集合来更新ENUM:
这不起作用:
原因是MySQL语句正在用另一个包含的语句替换现有的ENUM仅条目
'Malaysia'
和'Sweden'
。 MySQL 抛出错误,因为carmake
表已经包含'England'
和'USA'
等值,这些值不属于新的一部分>ENUM
的定义。令人惊讶的是,以下方法也不起作用:
事实证明,在向其中添加新成员时,甚至现有
ENUM
的元素顺序也需要保留。因此,如果我现有的ENUM
看起来像ENUM('England','USA')
,那么我的新ENUM
必须定义为 < code>ENUM('英格兰','美国','瑞典','马来西亚') 而不是ENUM('美国','英格兰','瑞典','马来西亚')
>。仅当现有表中存在使用'USA'
或'England'
值的记录时,此问题才会变得明显。底线:
仅当您不希望成员集定义后发生更改时才使用
ENUM
。否则,查找表更容易更新和修改。Important note: this is a legacy answer
The discussion I had with Asaph may be unclear to follow as we went back and forth quite a bit.
I thought that I might clarify the upshot of our discourse for others who might face similar situations in the future to benefit from:
ENUM
-type columns are very difficult beasts to manipulate. I wanted to add two countries (Malaysia & Sweden) to the existing set of countries in my ENUM.It seems that MySQL 5.1 (which is what I am running) can only update the ENUM by redefining the existing set in addition to what I want:
This did not work:
The reason was that the MySQL statement was replacing the existing ENUM with another containing the entries
'Malaysia'
and'Sweden'
only. MySQL threw up an error because thecarmake
table already had values like'England'
and'USA'
which were not part of the newENUM
's definition.Surprisingly, the following did not work either:
It turns out that even the order of elements of the existing
ENUM
needs to be preserved while adding new members to it. So if my existingENUM
looks something likeENUM('England','USA')
, then my newENUM
has to be defined asENUM('England','USA','Sweden','Malaysia')
and notENUM('USA','England','Sweden','Malaysia')
. This problem only becomes manifest when there are records in the existing table that use'USA'
or'England'
values.BOTTOM LINE:
Only use
ENUM
s when you do not expect your set of members to change once defined. Otherwise, lookup tables are much easier to update and modify.在 MYSQL 服务器版本:5.0.27 我尝试了这个,它对我来说工作得很好,检查你的版本
In MYSQL server version: 5.0.27 i tried this and it worked fine for me check in your version
这是另一种方法...
它将“other”添加到表“firmas”的列“rtipo”的枚举定义中。
Here is another way...
It adds "others" to the enum definition of the column "rtipo" of the table "firmas".
仅供参考:一个有用的模拟工具 - phpMyAdmin 与 Wampserver 3.0.6 - 预览 SQL:我使用“预览 SQL”来查看在保存对 ENUM 进行更改的列之前将生成的 SQL 代码。 预览 SQL
在上面您可以看到我已将“Ford”、“Toyota”输入到 ENUM 中,但是我收到语法 ENUM(0),它生成语法错误 查询错误 1064#
然后我复制粘贴并更改 SQL 并通过 SQL 运行它并得到积极的结果。
SQL 已更改
这是我经常使用的快速修复,也可以用于现有的 ENUM 值需要改变。认为这可能有用。
FYI: A useful simulation tool - phpMyAdmin with Wampserver 3.0.6 - Preview SQL: I use 'Preview SQL' to see the SQL code that would be generated before you save the column with the change to ENUM. Preview SQL
Above you see that I have entered 'Ford','Toyota' into the ENUM but I am getting syntax ENUM(0) which is generating syntax error Query error 1064#
I then copy and paste and alter the SQL and run it through SQL with a positive result.
SQL changed
This is a quickfix that I use often and can also be used on existing ENUM values that need to be altered. Thought this might be useful.
只要你相信,就有可能。呵呵。试试这个代码。
添加新值之前
添加新值后
It's possible if you believe. Hehe. try this code.
Before adding new value
After adding new value