~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/man7/CREATE_DOMAIN.7

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'\" t
 
2
.\"     Title: CREATE DOMAIN
 
3
.\"    Author: The PostgreSQL Global Development Group
 
4
.\" Generator: DocBook XSL Stylesheets v1.75.1 <http://docbook.sf.net/>
 
5
.\"      Date: 2011-04-27
 
6
.\"    Manual: PostgreSQL 9.1beta1 Documentation
 
7
.\"    Source: PostgreSQL 9.1beta1
 
8
.\"  Language: English
 
9
.\"
 
10
.TH "CREATE DOMAIN" "7" "2011-04-27" "PostgreSQL 9.1beta1" "PostgreSQL 9.1beta1 Documentation"
 
11
.\" -----------------------------------------------------------------
 
12
.\" * set default formatting
 
13
.\" -----------------------------------------------------------------
 
14
.\" disable hyphenation
 
15
.nh
 
16
.\" disable justification (adjust text to left margin only)
 
17
.ad l
 
18
.\" -----------------------------------------------------------------
 
19
.\" * MAIN CONTENT STARTS HERE *
 
20
.\" -----------------------------------------------------------------
 
21
.SH "NAME"
 
22
CREATE_DOMAIN \- define a new domain
 
23
.\" CREATE DOMAIN
 
24
.SH "SYNOPSIS"
 
25
.sp
 
26
.nf
 
27
CREATE DOMAIN \fIname\fR [ AS ] \fIdata_type\fR
 
28
    [ COLLATE \fIcollation\fR ]
 
29
    [ DEFAULT \fIexpression\fR ]
 
30
    [ \fIconstraint\fR [ \&.\&.\&. ] ]
 
31
 
 
32
where \fIconstraint\fR is:
 
33
 
 
34
[ CONSTRAINT \fIconstraint_name\fR ]
 
35
{ NOT NULL | NULL | CHECK (\fIexpression\fR) }
 
36
.fi
 
37
.SH "DESCRIPTION"
 
38
.PP
 
39
CREATE DOMAIN
 
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\&.
 
41
.PP
 
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\&.
 
44
.PP
 
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\&.
 
46
.SH "PARAMETERS"
 
47
.PP
 
48
\fIname\fR
 
49
.RS 4
 
50
The name (optionally schema\-qualified) of a domain to be created\&.
 
51
.RE
 
52
.PP
 
53
\fIdata_type\fR
 
54
.RS 4
 
55
The underlying data type of the domain\&. This can include array specifiers\&.
 
56
.RE
 
57
.PP
 
58
\fIcollation\fR
 
59
.RS 4
 
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
 
61
COLLATE
 
62
is specified\&.
 
63
.RE
 
64
.PP
 
65
DEFAULT \fIexpression\fR
 
66
.RS 4
 
67
The
 
68
DEFAULT
 
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\&.
 
70
.sp
 
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\&.
 
72
.RE
 
73
.PP
 
74
CONSTRAINT \fIconstraint_name\fR
 
75
.RS 4
 
76
An optional name for a constraint\&. If not specified, the system generates a name\&.
 
77
.RE
 
78
.PP
 
79
NOT NULL
 
80
.RS 4
 
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))\&.
 
83
.RE
 
84
.PP
 
85
NULL
 
86
.RS 4
 
87
Values of this domain are allowed to be null\&. This is the default\&.
 
88
.sp
 
89
This clause is only intended for compatibility with nonstandard SQL databases\&. Its use is discouraged in new applications\&.
 
90
.RE
 
91
.PP
 
92
CHECK (\fIexpression\fR)
 
93
.RS 4
 
94
CHECK
 
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
 
96
VALUE
 
97
to refer to the value being tested\&.
 
98
.sp
 
99
Currently,
 
100
CHECK
 
101
expressions cannot contain subqueries nor refer to variables other than
 
102
VALUE\&.
 
103
.RE
 
104
.SH "EXAMPLES"
 
105
.PP
 
106
This example creates the
 
107
us_postal_code
 
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:
 
109
.sp
 
110
.if n \{\
 
111
.RS 4
 
112
.\}
 
113
.nf
 
114
CREATE DOMAIN us_postal_code AS TEXT
 
115
CHECK(
 
116
   VALUE ~ \(aq^\e\ed{5}$\(aq
 
117
OR VALUE ~ \(aq^\e\ed{5}\-\e\ed{4}$\(aq
 
118
);
 
119
 
 
120
CREATE TABLE us_snail_addy (
 
121
  address_id SERIAL PRIMARY KEY,
 
122
  street1 TEXT NOT NULL,
 
123
  street2 TEXT,
 
124
  street3 TEXT,
 
125
  city TEXT NOT NULL,
 
126
  postal us_postal_code NOT NULL
 
127
);
 
128
.fi
 
129
.if n \{\
 
130
.RE
 
131
.\}
 
132
.SH "COMPATIBILITY"
 
133
.PP
 
134
The command
 
135
CREATE DOMAIN
 
136
conforms to the SQL standard\&.
 
137
.SH "SEE ALSO"
 
138
ALTER DOMAIN (\fBALTER_DOMAIN\fR(7)), DROP DOMAIN (\fBDROP_DOMAIN\fR(7))