Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/ref/create_domain.sgml`
384bcf077623449025f321ac947c2d561ff8299bd55769be0000000100000fa3
<!--
doc/src/sgml/ref/create_domain.sgml
PostgreSQL documentation
-->

<refentry id="sql-createdomain">
 <indexterm zone="sql-createdomain">
  <primary>CREATE DOMAIN</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>CREATE DOMAIN</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>CREATE DOMAIN</refname>
  <refpurpose>define a new domain</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
CREATE DOMAIN <replaceable class="parameter">name</replaceable> [ AS ] <replaceable class="parameter">data_type</replaceable>
    [ COLLATE <replaceable>collation</replaceable> ]
    [ DEFAULT <replaceable>expression</replaceable> ]
    [ <replaceable class="parameter">domain_constraint</replaceable> [ ... ] ]

<phrase>where <replaceable class="parameter">domain_constraint</replaceable> is:</phrase>

[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ]
{ NOT NULL | NULL | CHECK (<replaceable class="parameter">expression</replaceable>) }
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>CREATE DOMAIN</command> creates a new domain.  A domain is
   essentially a data type with optional constraints (restrictions on
   the allowed set of values).
   The user who defines a domain becomes its owner.
  </para>

  <para>
   If a schema name is given (for example, <literal>CREATE DOMAIN
   myschema.mydomain ...</literal>) then the domain is created in the
   specified schema.  Otherwise it is created in the current schema.
   The domain name must be unique among the types and domains existing
   in its schema.
  </para>

  <para>
   Domains are useful for abstracting common constraints on fields into
   a single location for maintenance.  For example, several tables might
   contain email address columns, all requiring the same CHECK constraint
   to verify the address syntax.
   Define a domain rather than setting up each table's constraint
   individually.
  </para>

  <para>
   To be able to create a domain, you must have <literal>USAGE</literal>
   privilege on the underlying type.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">name</replaceable></term>
      <listitem>
       <para>
        The name (optionally schema-qualified) of a domain to be created.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="parameter">data_type</replaceable></term>
      <listitem>
       <para>
        The underlying data type of the domain. This can include array
        specifiers.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable>collation</replaceable></term>
      <listitem>
       <para>
        An optional collation for the domain.  If no collation is
        specified, the domain has the same collation behavior as its
        underlying data type.
        The underlying type must be collatable if <literal>COLLATE</literal>
        is specified.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><literal>DEFAULT <replaceable>expression</replaceable></literal></term>

      <listitem>
       <para>
        The <literal>DEFAULT</literal> clause specifies a default value for
        columns of the domain data type.  The value is any
        variable-free expression (but subqueries are not allowed).
        The data type of the default expression must match the data
        type of the domain.  If no default value is specified, then
        the default value is the null value.
       </para>

       <para>
        The default expression will be used in any insert operation
        that does not specify a value for the column.  If a default
        value is defined for a particular column, it overrides any
        default associated with the domain.  In turn, the domain

Title: CREATE DOMAIN: Defining New Domains in PostgreSQL
Summary
The CREATE DOMAIN command in PostgreSQL defines a new domain, which is essentially a data type with optional constraints on the allowed values. The user defining the domain becomes its owner. Domains are created within a specified or current schema and must have a unique name. They are useful for abstracting common constraints on fields, like email address syntax, into a single location for maintenance. The command requires USAGE privilege on the underlying type. Parameters include the domain name, data type, collation, and default expression.