1CREATE DOMAIN(7)        PostgreSQL 9.2.24 Documentation       CREATE DOMAIN(7)
2
3
4

NAME

6       CREATE_DOMAIN - define a new domain
7

SYNOPSIS

9       CREATE DOMAIN name [ AS ] data_type
10           [ COLLATE collation ]
11           [ DEFAULT expression ]
12           [ constraint [ ... ] ]
13
14       where constraint is:
15
16       [ CONSTRAINT constraint_name ]
17       { NOT NULL | NULL | CHECK (expression) }
18

DESCRIPTION

20       CREATE DOMAIN creates a new domain. A domain is essentially a data type
21       with optional constraints (restrictions on the allowed set of values).
22       The user who defines a domain becomes its owner.
23
24       If a schema name is given (for example, CREATE DOMAIN myschema.mydomain
25       ...) then the domain is created in the specified schema. Otherwise it
26       is created in the current schema. The domain name must be unique among
27       the types and domains existing in its schema.
28
29       Domains are useful for abstracting common constraints on fields into a
30       single location for maintenance. For example, several tables might
31       contain email address columns, all requiring the same CHECK constraint
32       to verify the address syntax. Define a domain rather than setting up
33       each table's constraint individually.
34
35       To be able to create a domain, you must have USAGE privilege on the
36       underlying type.
37

PARAMETERS

39       name
40           The name (optionally schema-qualified) of a domain to be created.
41
42       data_type
43           The underlying data type of the domain. This can include array
44           specifiers.
45
46       collation
47           An optional collation for the domain. If no collation is specified,
48           the underlying data type's default collation is used. The
49           underlying type must be collatable if COLLATE is specified.
50
51       DEFAULT expression
52           The DEFAULT clause specifies a default value for columns of the
53           domain data type. The value is any variable-free expression (but
54           subqueries are not allowed). The data type of the default
55           expression must match the data type of the domain. If no default
56           value is specified, then the default value is the null value.
57
58           The default expression will be used in any insert operation that
59           does not specify a value for the column. If a default value is
60           defined for a particular column, it overrides any default
61           associated with the domain. In turn, the domain default overrides
62           any default value associated with the underlying data type.
63
64       CONSTRAINT constraint_name
65           An optional name for a constraint. If not specified, the system
66           generates a name.
67
68       NOT NULL
69           Values of this domain are normally prevented from being null.
70           However, it is still possible for a domain with this constraint to
71           take a null value if it is assigned a matching domain type that has
72           become null, e.g. via a LEFT OUTER JOIN, or INSERT INTO tab
73           (domcol) VALUES ((SELECT domcol FROM tab WHERE false)).
74
75       NULL
76           Values of this domain are allowed to be null. This is the default.
77
78           This clause is only intended for compatibility with nonstandard SQL
79           databases. Its use is discouraged in new applications.
80
81       CHECK (expression)
82           CHECK clauses specify integrity constraints or tests which values
83           of the domain must satisfy. Each constraint must be an expression
84           producing a Boolean result. It should use the key word VALUE to
85           refer to the value being tested.
86
87           Currently, CHECK expressions cannot contain subqueries nor refer to
88           variables other than VALUE.
89

EXAMPLES

91       This example creates the us_postal_code data type and then uses the
92       type in a table definition. A regular expression test is used to verify
93       that the value looks like a valid US postal code:
94
95           CREATE DOMAIN us_postal_code AS TEXT
96           CHECK(
97              VALUE ~ '^\d{5}$'
98           OR VALUE ~ '^\d{5}-\d{4}$'
99           );
100
101           CREATE TABLE us_snail_addy (
102             address_id SERIAL PRIMARY KEY,
103             street1 TEXT NOT NULL,
104             street2 TEXT,
105             street3 TEXT,
106             city TEXT NOT NULL,
107             postal us_postal_code NOT NULL
108           );
109

COMPATIBILITY

111       The command CREATE DOMAIN conforms to the SQL standard.
112

SEE ALSO

114       ALTER DOMAIN (ALTER_DOMAIN(7)), DROP DOMAIN (DROP_DOMAIN(7))
115
116
117
118PostgreSQL 9.2.24                 2017-11-06                  CREATE DOMAIN(7)
Impressum