

If you still have any doubts, just have a look at the result obtained from the order by country_region clause. SELECT country_id, country_name, country_region Let’s try a few more examples on ordering.įind the details of countries from country_list with country_region lesser than ‘North America’. So the moral of the story is that ENUM types are ordered sets and they maintain a sorting sequence as specified at the time of creation. Scroll up and have a look at the “continents” type. Then why did we receive such a result? This is because ‘South America’ and ‘Europe’ are placed after ‘North America’ in the ENUM list. SELECT country_id, country_name, country_regionĪs seen in the data output above, the server returned ‘South America’ and ‘Europe’ as greater than ‘North America’. INSERT INTO untry_list(įind the details of countries from country_list with country_region greater than ‘North America’. We can use the following INSERT statement for this task.

In order to illustrate ordering or sorting in enumerated data types, let us insert a few records in the “country_list” table. So remember this when creating and working with enum data types.

Why did we get an error message? This is because enumerated data types are case-sensitive. Now try this next insert statement which is similar to the previous statement. INSERT INTO untry_list(Ĭountry_id, country_name, country_region) Here is an INSERT statement to insert a new row with a column of continents type. The next task is to insert a few records in the “country_list” table to understand how values are inserted in enum type. The CREATE TABLE statement has been successfully executed. Follow the given sql statement for creating this table. This table has a field “country_region” that is of continents type a.k.a the enum data type we just created. Here, we are creating a database table known as “country_list”. We have successfully created our first enum type “continents”. CREATE TYPE continents AS ENUM ('Asia', 'Africa', 'North America', 'South America', 'Antarctica', 'Europe','Australia') Creating Enum Data TypeĬreate an enumerated data type called “continents” that contains a list of seven continents on Earth. In order to illustrate working with ENUM data types, let us try a few examples based on it. Note: Enum values are case-sensitive, by default ergo ‘RED’ is not similar to ‘red’ or ‘Red’.For example, Rainbow can only have Violet, Indigo, Blue, Green, Yellow, Orange, and Red as colours. This set is user-defined, you can mention as many values as you want. (‘value_1′,’value_2’,…, ‘value_n’ ) : Values that are acceptable as a part of this enumerated data type.enum_name: Name of the enumerated data type.The parameters used in the above-mentioned syntaxes are as follows : The syntax for incorporating ENUM data type in a database table is as follows : CREATE TABLE table_name ( MySQL maps these enumeration member to a numeric index where small=1, medium=2, large=3, and x-large=4 respectively.CREATE TYPE enum_name AS ENUM ('value_1','value_2'., 'value_n' ) The size column uses the ENUM data type and has small, medium, large, and x-large sizes.

Here, we are going to create a table named " shirts" that contains three columns: id, name, and size. Let us understand how ENUM data type works in MySQL with the following illustration. ENUM data type in My SQL includes the DEFAULT values as NULL or an empty string (''). The DEFAULT expression does not allow to insert function. In other words, if the INSERT statement does not provide a value for this column, then the default value will be inserted. NULL: It is a synonym for DEFAULT NULL, and its index value is always NULL.ĭEFAULT: When a value is not specified in the column, the ENUM data type inserts the default value. If we do not want to allow the NULL values, it is required to use the NOT NULL property while specifying the ENUM column. NOT NULL: By default, the ENUM data type is NULL. MySQL allows us to define the ENUM data type with the following attributes: Here, we have to make sure that the enumeration values should always keep inside the quoted string literal. In the above syntax, we have defined only three ENUM values, but it can be increased according to our needs.
