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 ( CREATE TYPE enum_name AS ENUM ('value_1','value_2'., 'value_n' )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |