]> begriffs open source - ai-pg/blob - full-docs/txt/datatype-enum.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / datatype-enum.txt
1
2 8.7. Enumerated Types #
3
4    8.7.1. Declaration of Enumerated Types
5    8.7.2. Ordering
6    8.7.3. Type Safety
7    8.7.4. Implementation Details
8
9    Enumerated (enum) types are data types that comprise a static, ordered
10    set of values. They are equivalent to the enum types supported in a
11    number of programming languages. An example of an enum type might be
12    the days of the week, or a set of status values for a piece of data.
13
14 8.7.1. Declaration of Enumerated Types #
15
16    Enum types are created using the CREATE TYPE command, for example:
17 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
18
19    Once created, the enum type can be used in table and function
20    definitions much like any other type:
21 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
22 CREATE TABLE person (
23     name text,
24     current_mood mood
25 );
26 INSERT INTO person VALUES ('Moe', 'happy');
27 SELECT * FROM person WHERE current_mood = 'happy';
28  name | current_mood
29 ------+--------------
30  Moe  | happy
31 (1 row)
32
33 8.7.2. Ordering #
34
35    The ordering of the values in an enum type is the order in which the
36    values were listed when the type was created. All standard comparison
37    operators and related aggregate functions are supported for enums. For
38    example:
39 INSERT INTO person VALUES ('Larry', 'sad');
40 INSERT INTO person VALUES ('Curly', 'ok');
41 SELECT * FROM person WHERE current_mood > 'sad';
42  name  | current_mood
43 -------+--------------
44  Moe   | happy
45  Curly | ok
46 (2 rows)
47
48 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
49  name  | current_mood
50 -------+--------------
51  Curly | ok
52  Moe   | happy
53 (2 rows)
54
55 SELECT name
56 FROM person
57 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
58  name
59 -------
60  Larry
61 (1 row)
62
63 8.7.3. Type Safety #
64
65    Each enumerated data type is separate and cannot be compared with other
66    enumerated types. See this example:
67 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
68 CREATE TABLE holidays (
69     num_weeks integer,
70     happiness happiness
71 );
72 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
73 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
74 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
75 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
76 ERROR:  invalid input value for enum happiness: "sad"
77 SELECT person.name, holidays.num_weeks FROM person, holidays
78   WHERE person.current_mood = holidays.happiness;
79 ERROR:  operator does not exist: mood = happiness
80
81    If you really need to do something like that, you can either write a
82    custom operator or add explicit casts to your query:
83 SELECT person.name, holidays.num_weeks FROM person, holidays
84   WHERE person.current_mood::text = holidays.happiness::text;
85  name | num_weeks
86 ------+-----------
87  Moe  |         4
88 (1 row)
89
90
91 8.7.4. Implementation Details #
92
93    Enum labels are case sensitive, so 'happy' is not the same as 'HAPPY'.
94    White space in the labels is significant too.
95
96    Although enum types are primarily intended for static sets of values,
97    there is support for adding new values to an existing enum type, and
98    for renaming values (see ALTER TYPE). Existing values cannot be removed
99    from an enum type, nor can the sort ordering of such values be changed,
100    short of dropping and re-creating the enum type.
101
102    An enum value occupies four bytes on disk. The length of an enum
103    value's textual label is limited by the NAMEDATALEN setting compiled
104    into PostgreSQL; in standard builds this means at most 63 bytes.
105
106    The translations from internal enum values to textual labels are kept
107    in the system catalog pg_enum. Querying this catalog directly can be
108    useful.