2
.\" Title: CREATE DOMAIN
3
.\" Author: The PostgreSQL Global Development Group
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
6
.\" Manual: PostgreSQL 9.1beta1 Documentation
7
.\" Source: PostgreSQL 9.1beta1
10
.TH "CREATE DOMAIN" "7" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
11
.\" -----------------------------------------------------------------
12
.\" * set default formatting
13
.\" -----------------------------------------------------------------
14
.\" disable hyphenation
16
.\" disable justification (adjust text to left margin only)
18
.\" -----------------------------------------------------------------
19
.\" * MAIN CONTENT STARTS HERE *
20
.\" -----------------------------------------------------------------
22
CREATE_DOMAIN \- define a new domain
27
CREATE DOMAIN \fIname\fR [ AS ] \fIdata_type\fR
28
[ COLLATE \fIcollation\fR ]
29
[ DEFAULT \fIexpression\fR ]
30
[ \fIconstraint\fR [ \&.\&.\&. ] ]
32
where \fIconstraint\fR is:
34
[ CONSTRAINT \fIconstraint_name\fR ]
35
{ NOT NULL | NULL | CHECK (\fIexpression\fR) }
40
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\&.
42
If a schema name is given (for example,
43
CREATE DOMAIN myschema\&.mydomain \&.\&.\&.) 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\&.
45
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\(aqs constraint individually\&.
50
The name (optionally schema\-qualified) of a domain to be created\&.
55
The underlying data type of the domain\&. This can include array specifiers\&.
60
An optional collation for the domain\&. If no collation is specified, the underlying data type\(aqs default collation is used\&. The underlying type must be collatable if
65
DEFAULT \fIexpression\fR
69
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\&.
71
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\&.
74
CONSTRAINT \fIconstraint_name\fR
76
An optional name for a constraint\&. If not specified, the system generates a name\&.
81
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
82
INSERT INTO tab (domcol) VALUES ((SELECT domcol FROM tab WHERE false))\&.
87
Values of this domain are allowed to be null\&. This is the default\&.
89
This clause is only intended for compatibility with nonstandard SQL databases\&. Its use is discouraged in new applications\&.
92
CHECK (\fIexpression\fR)
95
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
97
to refer to the value being tested\&.
101
expressions cannot contain subqueries nor refer to variables other than
106
This example creates the
108
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:
114
CREATE DOMAIN us_postal_code AS TEXT
116
VALUE ~ \(aq^\e\ed{5}$\(aq
117
OR VALUE ~ \(aq^\e\ed{5}\-\e\ed{4}$\(aq
120
CREATE TABLE us_snail_addy (
121
address_id SERIAL PRIMARY KEY,
122
street1 TEXT NOT NULL,
126
postal us_postal_code NOT NULL
136
conforms to the SQL standard\&.
138
ALTER DOMAIN (\fBALTER_DOMAIN\fR(7)), DROP DOMAIN (\fBDROP_DOMAIN\fR(7))