1CREATE DOMAIN() SQL Commands CREATE DOMAIN()
2
3
4
6 CREATE DOMAIN - define a new domain
7
8
10 CREATE DOMAIN name [ AS ] data_type
11 [ DEFAULT expression ]
12 [ constraint [ ... ] ]
13
14 where constraint is:
15
16 [ CONSTRAINT constraint_name ]
17 { NOT NULL | NULL | CHECK (expression) }
18
19
21 CREATE DOMAIN creates a new domain. A domain is essentially a data type
22 with optional constraints (restrictions on the allowed set of values).
23 The user who defines a domain becomes its owner.
24
25 If a schema name is given (for example, CREATE DOMAIN myschema.mydomain
26 ...) then the domain is created in the specified schema. Otherwise it
27 is created in the current schema. The domain name must be unique among
28 the types and domains existing in its schema.
29
30 Domains are useful for abstracting common constraints on fields into a
31 single location for maintenance. For example, several tables might con‐
32 tain email address columns, all requiring the same CHECK constraint to
33 verify the address syntax. Define a domain rather than setting up each
34 table's constraint individually.
35
37 name The name (optionally schema-qualified) of a domain to be cre‐
38 ated.
39
40 data_type
41 The underlying data type of the domain. This may include array
42 specifiers.
43
44 DEFAULT expression
45 The DEFAULT clause specifies a default value for columns of the
46 domain data type. The value is any variable-free expression (but
47 subqueries are not allowed). The data type of the default
48 expression must match the data type of the domain. If no default
49 value is specified, then the default value is the null value.
50
51 The default expression will be used in any insert operation that
52 does not specify a value for the column. If a default value is
53 defined for a particular column, it overrides any default asso‐
54 ciated with the domain. In turn, the domain default overrides
55 any default value associated with the underlying data type.
56
57 CONSTRAINT constraint_name
58 An optional name for a constraint. If not specified, the system
59 generates a name.
60
61 NOT NULL
62 Values of this domain are not allowed to be null.
63
64 NULL Values of this domain are allowed to be null. This is the
65 default.
66
67 This clause is only intended for compatibility with nonstandard
68 SQL databases. Its use is discouraged in new applications.
69
70 CHECK (expression)
71 CHECK clauses specify integrity constraints or tests which val‐
72 ues of the domain must satisfy. Each constraint must be an
73 expression producing a Boolean result. It should use the key
74 word VALUE to refer to the value being tested.
75
76 Currently, CHECK expressions cannot contain subqueries nor refer
77 to variables other than VALUE.
78
80 This example creates the us_postal_code data type and then uses the
81 type in a table definition. A regular expression test is used to verify
82 that the value looks like a valid US postal code.
83
84 CREATE DOMAIN us_postal_code AS TEXT
85 CHECK(
86 VALUE ~ '^\\d{5}$'
87 OR VALUE ~ '^\\d{5}-\\d{4}$'
88 );
89
90 CREATE TABLE us_snail_addy (
91 address_id SERIAL PRIMARY KEY,
92 street1 TEXT NOT NULL,
93 street2 TEXT,
94 street3 TEXT,
95 city TEXT NOT NULL,
96 postal us_postal_code NOT NULL
97 );
98
99
101 The command CREATE DOMAIN conforms to the SQL standard.
102
104 ALTER DOMAIN [alter_domain(7)], DROP DOMAIN [drop_domain(l)]
105
106
107
108SQL - Language Statements 2008-06-08 CREATE DOMAIN()