Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does MySQL Store Enums?

Tags:

If I have a table like this:

CREATE TABLE sizes (     name ENUM('small', 'medium', 'large') ); 

Is MySQL going to store those strings in every row, or internally will it use something smaller like integers to know which enum value to refer to?

I want to use an enum in a table but I'm worried if it's as wasteful as storing a string in every row.

like image 627
Greg Avatar asked Nov 03 '08 18:11

Greg


People also ask

How is enum stored in database?

By default, when an enum is a part of an entity, JPA maps its values into numbers using the ordinal() method. What it means is that without customizations JPA stores enum value as numbers. These numbers are associated with the order in which you define values in the enum.

Does MySQL support enums?

In MySQL, an ENUM is a string object whose value is chosen from a list of permitted values defined at the time of column creation. The ENUM data type provides the following advantages: Compact data storage. MySQL ENUM uses numeric indexes (1, 2, 3, …) to represents string values.

How are enum values stored?

A standard enum is usually implemented as an int32, the compiler will handle your enum as a synonym of int32 . Once a list of values is created for a enumeration those values are stored as literals against their display name(access name given at the time of declaration of enum).

What is enum type in MySQL?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.


2 Answers

It converts them to integers on INSERT / UPDATE and back to strings on SELECT so the internal storage is as integers but you don't get exposed to that.

You can retrieve the integer like SELECT mycolumn + 0.

See ENUMs in MySQL 5

like image 102
Greg Avatar answered Sep 20 '22 20:09

Greg


As explained here, enums are stored as values between 0 (no valid value set) and 65,535 that are associated with an enumeration index. The value that is stored in the table is a 2 byte value, not a string.

like image 25
Robert Gamble Avatar answered Sep 19 '22 20:09

Robert Gamble