Home Explore Blog CI



postgresql

40th chunk of `doc/src/sgml/datatype.sgml`
c8e235520b2c2a4762be0732937e896ae3ba70bb5761b3d70000000100000fa6
 that <literal>TRUE</literal> and <literal>FALSE</literal> are of
    type <type>boolean</type>, but this is not so
    for <literal>NULL</literal> because that can have any type.
    So in some contexts you might have to cast <literal>NULL</literal>
    to <type>boolean</type> explicitly, for
    example <literal>NULL::boolean</literal>.  Conversely, the cast can be
    omitted from a string-literal Boolean value in contexts where the parser
    can deduce that the literal must be of type <type>boolean</type>.
   </para>
  </sect1>

  <sect1 id="datatype-enum">
   <title>Enumerated Types</title>

   <indexterm zone="datatype-enum">
    <primary>data type</primary>
    <secondary>enumerated (enum)</secondary>
   </indexterm>

   <indexterm zone="datatype-enum">
    <primary>enumerated types</primary>
   </indexterm>

   <para>
    Enumerated (enum) types are data types that
    comprise a static, ordered set of values.
    They are equivalent to the <type>enum</type>
    types supported in a number of programming languages. An example of an enum
    type might be the days of the week, or a set of status values for
    a piece of data.
   </para>

   <sect2 id="datatype-enum-declaration">
    <title>Declaration of Enumerated Types</title>

    <para>
     Enum types are created using the <xref
     linkend="sql-createtype"/> command,
     for example:

<programlisting>
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
</programlisting>

     Once created, the enum type can be used in table and function
     definitions much like any other type:
<programlisting>
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
    name text,
    current_mood mood
);
INSERT INTO person VALUES ('Moe', 'happy');
SELECT * FROM person WHERE current_mood = 'happy';
 name | current_mood
------+--------------
 Moe  | happy
(1 row)
</programlisting>
    </para>
    </sect2>

    <sect2 id="datatype-enum-ordering">
     <title>Ordering</title>

     <para>
      The ordering of the values in an enum type is the
      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:

<programlisting>
INSERT INTO person VALUES ('Larry', 'sad');
INSERT INTO person VALUES ('Curly', 'ok');
SELECT * FROM person WHERE current_mood > 'sad';
 name  | current_mood
-------+--------------
 Moe   | happy
 Curly | ok
(2 rows)

SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
 name  | current_mood
-------+--------------
 Curly | ok
 Moe   | happy
(2 rows)

SELECT name
FROM person
WHERE current_mood = (SELECT MIN(current_mood) FROM person);
 name
-------
 Larry
(1 row)
</programlisting>
     </para>
   </sect2>

   <sect2 id="datatype-enum-type-safety">
    <title>Type Safety</title>

    <para>
     Each enumerated data type is separate and cannot
     be compared with other enumerated types.  See this example:

<programlisting>
CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
CREATE TABLE holidays (
    num_weeks integer,
    happiness happiness
);
INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
ERROR:  invalid input value for enum happiness: "sad"
SELECT person.name, holidays.num_weeks FROM person, holidays
  WHERE person.current_mood = holidays.happiness;
ERROR:  operator does not exist: mood = happiness
</programlisting>
    </para>

    <para>
     If you really need to do something like that, you can either
     write a custom operator or add explicit casts to your query:

<programlisting>
SELECT person.name, holidays.num_weeks FROM person, holidays
  WHERE person.current_mood::text = holidays.happiness::text;
 name | num_weeks
------+-----------
 Moe  |         4
(1 row)

</programlisting>

Title: Enumerated Types in PostgreSQL
Summary
Enumerated types in PostgreSQL are data types that comprise a static, ordered set of values. They can be created using the CREATE TYPE command and used in table and function definitions. Enum types have a defined order, and standard comparison operators and related aggregate functions are supported. Each enumerated data type is separate and cannot be compared with other enumerated types, but explicit casts can be used to compare values from different enum types.