Home Explore Blog CI



postgresql

39th chunk of `doc/src/sgml/datatype.sgml`
e3ed28eb7589e0e83b8cbad771eda467442c252400738cc40000000100000fa1
 <productname>PostgreSQL</productname> provides the
    standard <acronym>SQL</acronym> type <type>boolean</type>;
    see <xref linkend="datatype-boolean-table"/>.
    The <type>boolean</type> type can have several states:
    <quote>true</quote>, <quote>false</quote>, and a third state,
    <quote>unknown</quote>, which is represented by the
    <acronym>SQL</acronym> null value.
   </para>

   <table id="datatype-boolean-table">
    <title>Boolean Data Type</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>Name</entry>
       <entry>Storage Size</entry>
       <entry>Description</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry><type>boolean</type></entry>
       <entry>1 byte</entry>
       <entry>state of true or false</entry>
      </row>
     </tbody>
    </tgroup>
   </table>

   <para>
    Boolean constants can be represented in SQL queries by the SQL
    key words <literal>TRUE</literal>, <literal>FALSE</literal>,
    and <literal>NULL</literal>.
   </para>

   <para>
    The datatype input function for type <type>boolean</type> accepts these
    string representations for the <quote>true</quote> state:
    <simplelist>
     <member><literal>true</literal></member>
     <member><literal>yes</literal></member>
     <member><literal>on</literal></member>
     <member><literal>1</literal></member>
    </simplelist>
    and these representations for the <quote>false</quote> state:
    <simplelist>
     <member><literal>false</literal></member>
     <member><literal>no</literal></member>
     <member><literal>off</literal></member>
     <member><literal>0</literal></member>
    </simplelist>
    Unique prefixes of these strings are also accepted, for
    example <literal>t</literal> or <literal>n</literal>.
    Leading or trailing whitespace is ignored, and case does not matter.
   </para>

   <para>
    The datatype output function for type <type>boolean</type> always emits
    either <literal>t</literal> or <literal>f</literal>, as shown in
    <xref linkend="datatype-boolean-example"/>.
   </para>

   <example id="datatype-boolean-example">
    <title>Using the <type>boolean</type> Type</title>

<programlisting>
CREATE TABLE test1 (a boolean, b text);
INSERT INTO test1 VALUES (TRUE, 'sic est');
INSERT INTO test1 VALUES (FALSE, 'non est');
SELECT * FROM test1;
 a |    b
---+---------
 t | sic est
 f | non est

SELECT * FROM test1 WHERE a;
 a |    b
---+---------
 t | sic est
</programlisting>
   </example>

   <para>
    The key words <literal>TRUE</literal> and <literal>FALSE</literal> are
    the preferred (<acronym>SQL</acronym>-compliant) method for writing
    Boolean constants in SQL queries.  But you can also use the string
    representations by following the generic string-literal constant syntax
    described in <xref linkend="sql-syntax-constants-generic"/>, for
    example <literal>'yes'::boolean</literal>.
   </para>

   <para>
    Note that the parser automatically understands
    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

Title: Boolean Data Type in PostgreSQL
Summary
PostgreSQL's boolean data type can have three states: true, false, and unknown, represented by the SQL null value. The type is stored in 1 byte and can be represented in SQL queries using key words TRUE, FALSE, and NULL, as well as string representations like 'yes', 'no', 'on', 'off', '1', and '0'. The datatype input function accepts unique prefixes of these strings, ignoring case and whitespace, and the output function always emits 't' or 'f'. Boolean constants can be written using the SQL-compliant key words TRUE and FALSE, or using string representations with the generic string-literal constant syntax.