Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > 5fea23694c765462b86d6ddf74461eab > files > 799

postgresql9.6-docs-9.6.22-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CREATE DOMAIN</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.6.22 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CREATE DATABASE"
HREF="sql-createdatabase.html"><LINK
REL="NEXT"
TITLE="CREATE EVENT TRIGGER"
HREF="sql-createeventtrigger.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2021-05-18T09:16:10"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="4"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.6.22 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="CREATE DATABASE"
HREF="sql-createdatabase.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="CREATE EVENT TRIGGER"
HREF="sql-createeventtrigger.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CREATEDOMAIN"
></A
>CREATE DOMAIN</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN79718"
></A
><H2
>Name</H2
>CREATE DOMAIN&nbsp;--&nbsp;define a new domain</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN79721"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE DOMAIN <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ AS ] <TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
>
    [ COLLATE <TT
CLASS="REPLACEABLE"
><I
>collation</I
></TT
> ]
    [ DEFAULT <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> ]
    [ <TT
CLASS="REPLACEABLE"
><I
>constraint</I
></TT
> [ ... ] ]

<SPAN
CLASS="phrase"
><SPAN
CLASS="PHRASE"
>where <TT
CLASS="REPLACEABLE"
><I
>constraint</I
></TT
> is:</SPAN
></SPAN
>

[ CONSTRAINT <TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
> ]
{ NOT NULL | NULL | CHECK (<TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
>) }</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79732"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE DOMAIN</TT
> 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.
  </P
><P
>   If a schema name is given (for example, <TT
CLASS="LITERAL"
>CREATE DOMAIN
   myschema.mydomain ...</TT
>) 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.
  </P
><P
>   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.
  </P
><P
>   To be able to create a domain, you must have <TT
CLASS="LITERAL"
>USAGE</TT
>
   privilege on the underlying type.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79741"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>        The name (optionally schema-qualified) of a domain to be created.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
></DT
><DD
><P
>        The underlying data type of the domain. This can include array
        specifiers.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>collation</I
></TT
></DT
><DD
><P
>        An optional collation for the domain.  If no collation is
        specified, the underlying data type's default collation is used.
        The underlying type must be collatable if <TT
CLASS="LITERAL"
>COLLATE</TT
>
        is specified.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>DEFAULT <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
></TT
></DT
><DD
><P
>        The <TT
CLASS="LITERAL"
>DEFAULT</TT
> 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.
       </P
><P
>        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
        default overrides any default value associated with the
        underlying data type.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>CONSTRAINT <TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
></TT
></DT
><DD
><P
>        An optional name for a constraint.  If not specified,
        the system generates a name.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>NOT NULL</TT
></DT
><DD
><P
>        Values of this domain are prevented from being null
        (but see notes below).
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>NULL</TT
></DT
><DD
><P
>        Values of this domain are allowed to be null.  This is the default.
       </P
><P
>        This clause is only intended for compatibility with
        nonstandard SQL databases.  Its use is discouraged in new
        applications.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>CHECK (<TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
>)</TT
></DT
><DD
><P
><TT
CLASS="LITERAL"
>CHECK</TT
> clauses specify integrity constraints or tests
      which values of the domain must satisfy.
      Each constraint must be an expression
      producing a Boolean result.  It should use the key word <TT
CLASS="LITERAL"
>VALUE</TT
>
      to refer to the value being tested.  Expressions evaluating
      to TRUE or UNKNOWN succeed.  If the expression produces a FALSE result,
      an error is reported and the value is not allowed to be converted
      to the domain type.
     </P
><P
>      Currently, <TT
CLASS="LITERAL"
>CHECK</TT
> expressions cannot contain
      subqueries nor refer to variables other than <TT
CLASS="LITERAL"
>VALUE</TT
>.
     </P
><P
>      When a domain has multiple <TT
CLASS="LITERAL"
>CHECK</TT
> constraints,
      they will be tested in alphabetical order by name.
      (<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> versions before 9.5 did not honor any
      particular firing order for <TT
CLASS="LITERAL"
>CHECK</TT
> constraints.)
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79800"
></A
><H2
>Notes</H2
><P
>   Domain constraints, particularly <TT
CLASS="LITERAL"
>NOT NULL</TT
>, are checked when
   converting a value to the domain type.  It is possible for a column that
   is nominally of the domain type to read as null despite there being such
   a constraint.  For example, this can happen in an outer-join query, if
   the domain column is on the nullable side of the outer join.  A more
   subtle example is
</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM tab WHERE false));</PRE
><P>
   The empty scalar sub-SELECT will produce a null value that is considered
   to be of the domain type, so no further constraint checking is applied
   to it, and the insertion will succeed.
  </P
><P
>   It is very difficult to avoid such problems, because of SQL's general
   assumption that a null value is a valid value of every data type.  Best practice
   therefore is to design a domain's constraints so that a null value is allowed,
   and then to apply column <TT
CLASS="LITERAL"
>NOT NULL</TT
> constraints to columns of
   the domain type as needed, rather than directly to the domain type.
  </P
><P
>   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> assumes that
   <TT
CLASS="LITERAL"
>CHECK</TT
> constraints' conditions are immutable, that is,
   they will always give the same result for the same input value.  This
   assumption is what justifies examining <TT
CLASS="LITERAL"
>CHECK</TT
>
   constraints only when a value is first converted to be of a domain type,
   and not at other times.  (This is essentially the same as the treatment
   of table <TT
CLASS="LITERAL"
>CHECK</TT
> constraints, as described in
   <A
HREF="ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS"
>Section 5.3.1</A
>.)
  </P
><P
>   An example of a common way to break this assumption is to reference a
   user-defined function in a <TT
CLASS="LITERAL"
>CHECK</TT
> expression, and then
   change the behavior of that
   function.  <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> does not disallow that,
   but it will not notice if there are stored values of the domain type that
   now violate the <TT
CLASS="LITERAL"
>CHECK</TT
> constraint. That would cause a
   subsequent database dump and reload to fail.  The recommended way to
   handle such a change is to drop the constraint (using <TT
CLASS="COMMAND"
>ALTER
   DOMAIN</TT
>), adjust the function definition, and re-add the
   constraint, thereby rechecking it against stored data.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79818"
></A
><H2
>Examples</H2
><P
>   This example creates the <TT
CLASS="TYPE"
>us_postal_code</TT
> data type and
   then uses the type in a table definition.  A regular expression test
   is used to verify that the value looks like a valid US postal code:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE DOMAIN us_postal_code AS TEXT
CHECK(
   VALUE ~ '^\d{5}$'
OR VALUE ~ '^\d{5}-\d{4}$'
);

CREATE TABLE us_snail_addy (
  address_id SERIAL PRIMARY KEY,
  street1 TEXT NOT NULL,
  street2 TEXT,
  street3 TEXT,
  city TEXT NOT NULL,
  postal us_postal_code NOT NULL
);</PRE
><P></P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-CREATEDOMAIN-COMPATIBILITY"
></A
><H2
>Compatibility</H2
><P
>   The command <TT
CLASS="COMMAND"
>CREATE DOMAIN</TT
> conforms to the SQL
   standard.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-CREATEDOMAIN-SEE-ALSO"
></A
><H2
>See Also</H2
><A
HREF="sql-alterdomain.html"
>ALTER DOMAIN</A
>, <A
HREF="sql-dropdomain.html"
>DROP DOMAIN</A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createdatabase.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createeventtrigger.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CREATE DATABASE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>CREATE EVENT TRIGGER</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>