Current File : //usr/share/doc/postgresql-9.2.24/html/sql-createdomain.html |
<!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.2.24 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 EXTENSION"
HREF="sql-createextension.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="2017-11-06T22:43:11"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.2.24 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 EXTENSION"
HREF="sql-createextension.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="AEN68072"
></A
><H2
>Name</H2
>CREATE DOMAIN -- define a new domain</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN68077"
></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="AEN68088"
></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="AEN68097"
></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 normally prevented from being null.
However, it is still possible for a domain with this constraint
to take a null value if it is assigned a matching domain type
that has become null, e.g. via a LEFT OUTER JOIN, or
<TT
CLASS="COMMAND"
>INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM
tab WHERE false))</TT
>.
</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.
</P
><P
> Currently, <TT
CLASS="LITERAL"
>CHECK</TT
> expressions cannot contain
subqueries nor refer to variables other than <TT
CLASS="LITERAL"
>VALUE</TT
>.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN68153"
></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-createextension.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 EXTENSION</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>