-
+
Data Types
Once created, the enum type can be used in table and function
definitions much like any other type:
-
-
-
-
Basic Enum Usage
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
Moe | happy
(1 row)
- >
+ >
order in which the values were listed when the type was created.
All standard comparison operators and related
aggregate functions are supported for enums. For example:
-
-
-
-
Enum Ordering
+
INSERT INTO person VALUES ('Larry', 'sad');
INSERT INTO person VALUES ('Curly', 'ok');
Larry
(1 row)
- >
+ >
Each enumerated data type is separate and cannot
- be compared with other enumerated types.
-
+ be compared with other enumerated types. See this example:
-
-
Lack of Casting
CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
CREATE TABLE holidays (
WHERE person.current_mood = holidays.happiness;
ERROR: operator does not exist: mood = happiness
- example>
+ para>
If you really need to do something like that, you can either
write a custom operator or add explicit casts to your query:
-
-
-
Comparing Different Enums by Casting to Text
SELECT person.name, holidays.num_weeks FROM person, holidays
WHERE person.current_mood::text = holidays.happiness::text;
(1 row)
- example>
+ para>