

So, this will work too: SELECT * from people It used the index to specify the country. In point #4, you saw an example with a WHERE clause to filter with an ENUM column. Filter MySQL Enumeration by Index or String Value Having a separate table is more flexible and requires nothing from the developer when data is live. And to make a friendlier output like in Figure 2, you will also need a JOIN. More data with an indefinite number of rows and more attributes requires a table. So, should you use MySQL ENUM to avoid JOINs altogether? Definitely not! This is good for a small but fixed list. ,CASE WHEN p.gender = 'M' THEN 'Male' ELSE 'Female' END AS gender Meanwhile, the same output can be achieved when using a separate table and a join. Note: The data you see was generated using dbForge Studio for MySQL’s data generating tool. The friendly output of gender and country columns without a JOIN The error message below occurred because South Korea is not on the enumerated list.įigure 2. VALUES ('Choi', 'Seungcheol', '','Male','South Korea') So, if you try to do this, the “Data truncated for column ‘country'” error will be thrown: INSERT INTO people (lastname, firstname, middlename, gender, country) Under strict mode, the country column in our example earlier will only accept 12 possible values. MySQL ENUM Limits the Possible Values in a Column

To avoid errors, always use a strict server mode. Then, the insert will succeed, but the value is an empty string with an index of zero.You insert a value that is not on the list of permitted values.The server mode for your MySQL is not strict.This happens in the following situations: So, aside from indexes 1 to 12, NULL is another possible index with a NULL value. In our example, the country column accepts NULL. Note: this index doesn’t refer to the table indexes that we use for fast searches.īesides these numbers from 1 to 65,535, ENUM columns can also be NULL or zero. It starts with the United States with an index of 1 and ends with New Zealand with an index of 12. This means that key indexes start with 1.įrom this simple example, you can identify the index for the country column. The index for Male is 1, and Female is 2. Let me start with the gender column that contains 2 values: Male and Female. There are 2 MySQL ENUM values here: gender and country. Modifieddate datetime NOT NULL DEFAULT NOW() 'Japan','Philippines','Thailand', 'Australia','New Zealand') 'United Kingdom','Poland','Ukraine', 'Lithuania', Gender enum('Male','Female') NOT NULL DEFAULT 'Female',Ĭountry enum('United States', 'Canada', 'Brazil', Id int NOT NULL PRIMARY KEY AUTO_INCREMENT, MySQL ENUM Index Starts with 1 But Can Be NULL or Zero Otherwise, you need a table.īut how are these indexes numbered? 2. Think of 20 or fewer items and values that will never have further attributes.

That is a fixed list that will never expand.

So, whether it’s about a shortlist of colors, customer types, salutations, or payment methods, numbers will be assigned. MySQL automatically assigns numbers as they appear on your list. Values are strings, and keys are index numbers. MySQL ENUM is a Key/String Value Pair Type Like any other thing in the world, it’s not always a happily ever after.Īfter reading the following 12 key facts about MySQL ENUM, you can decide if it is good for your next database or table in MySQL.įor this article, the MySQL version is 8.0.23, and the storage engine is InnoDB. Under strict server mode, this means you can’t force a wrong entry. You set these permitted values during the table creation as shown below: CREATE TABLE ProductĬolor enum('blue','red','yellow','black','white') NOT NULL DEFAULT 'blue'įor starters, data validation is instant without another table and a foreign key. MySQL ENUM data is a string data type with a value chosen from the list of permitted values.
