MySQL enum field insert by index
There is a simple mysql table 'mytable' with a column called 'fruit', which is a enum fi开发者_开发问答eld contains 'apple', 'orange' and 'mango'.
We can access the index value of the enum with SELECT fruit+0 FROM mytable But how can we insert the enum field by index instead of INSERT INTO mytable fruit VALUE 'apple' WHERE ...
I tried INSERT INTO mytable fruit+0 VALUE '1' WHERE ... but it doesn't work and couldn't find any related topics here or from net.
Thanks.
A simple INSERT INTO mytable(fruit) VALUES(1);
should work.
You don't need any fruit+0
conversion like in select.
enums are specifically designed to remove the need to know index values. By using index values you're specifically going against the reason enums exist.
If you don't want to use the enum values for insertion, then I'd suggest splitting the fruit names off into a seperate table, and make your mytable.fruit a foreign key pointing at this new fruit table. Then you're inserting an ID value instead of 'apple'.
精彩评论