为什么ENUM在MySQL中不存储多个值? [英] Why ENUM does not store multiple values in MySQL?
问题描述
我想通过 MySQL 在表中使用 ENUM 功能.
I want to use ENUM feature in table using MySQL.
我创建了一个表 tbl_test ,其中以 id 作为主键,并且将 enum_col 字段作为 ENUM 数据类型.
I have created a table tbl_test having id as primary key and enum_col field as ENUM data type.
CREATE TABLE tbl_test(
id INT NOT NULL AUTO_INCREMENT,
enum_col ENUM('a','b','c') NOT NULL,
PRIMARY KEY ( id )
);
当我尝试存储单个枚举值时,它被插入,但是当我尝试存储多个枚举值时,它会抛出 SQL错误
When I try to store single enum value, it got inserted but when I try to store multiple enum values then it throws SQL error.
错误:
Data truncated for column 'enum_col' at row 1
单个ENUM值(正确):
INSERT INTO tbl_test(id, enum_col) values(1, 'a');
多个ENUM值(失败):
INSERT INTO tbl_test(id, enum_col) values(2, 'a,b');
是否有任何想法要在 ENUM 数据类型中存储多个值?
Any idea to store multiple values in ENUM data type ?
推荐答案
这是因为您只能在其中存储一个值,而实际上您绝对应该在任何类型的列中仅存储一个值.
That is because you can only store one value in it and in fact you absolutely should store only one value in whatever type of column.
使用单独的表.然后,您可以使用多个记录存储尽可能多的值.示例:
Use a seperate table. Then you can store as much values as you like with multiple records. Example:
tbl_test
--------
id | name
1 | test_X
2 | test_Y
3 | test_Z
tbl_test_enums
--------------
test_id | enum_value
1 | a
1 | b
2 | a
3 | c
这篇关于为什么ENUM在MySQL中不存储多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!