1ALTER DOMAIN(7)          PostgreSQL 13.3 Documentation         ALTER DOMAIN(7)
2
3
4

NAME

6       ALTER_DOMAIN - change the definition of a domain
7

SYNOPSIS

9       ALTER DOMAIN name
10           { SET DEFAULT expression | DROP DEFAULT }
11       ALTER DOMAIN name
12           { SET | DROP } NOT NULL
13       ALTER DOMAIN name
14           ADD domain_constraint [ NOT VALID ]
15       ALTER DOMAIN name
16           DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
17       ALTER DOMAIN name
18            RENAME CONSTRAINT constraint_name TO new_constraint_name
19       ALTER DOMAIN name
20           VALIDATE CONSTRAINT constraint_name
21       ALTER DOMAIN name
22           OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
23       ALTER DOMAIN name
24           RENAME TO new_name
25       ALTER DOMAIN name
26           SET SCHEMA new_schema
27

DESCRIPTION

29       ALTER DOMAIN changes the definition of an existing domain. There are
30       several sub-forms:
31
32       SET/DROP DEFAULT
33           These forms set or remove the default value for a domain. Note that
34           defaults only apply to subsequent INSERT commands; they do not
35           affect rows already in a table using the domain.
36
37       SET/DROP NOT NULL
38           These forms change whether a domain is marked to allow NULL values
39           or to reject NULL values. You can only SET NOT NULL when the
40           columns using the domain contain no null values.
41
42       ADD domain_constraint [ NOT VALID ]
43           This form adds a new constraint to a domain using the same syntax
44           as CREATE DOMAIN (CREATE_DOMAIN(7)). When a new constraint is added
45           to a domain, all columns using that domain will be checked against
46           the newly added constraint. These checks can be suppressed by
47           adding the new constraint using the NOT VALID option; the
48           constraint can later be made valid using ALTER DOMAIN ... VALIDATE
49           CONSTRAINT. Newly inserted or updated rows are always checked
50           against all constraints, even those marked NOT VALID.  NOT VALID is
51           only accepted for CHECK constraints.
52
53       DROP CONSTRAINT [ IF EXISTS ]
54           This form drops constraints on a domain. If IF EXISTS is specified
55           and the constraint does not exist, no error is thrown. In this case
56           a notice is issued instead.
57
58       RENAME CONSTRAINT
59           This form changes the name of a constraint on a domain.
60
61       VALIDATE CONSTRAINT
62           This form validates a constraint previously added as NOT VALID,
63           that is, it verifies that all values in table columns of the domain
64           type satisfy the specified constraint.
65
66       OWNER
67           This form changes the owner of the domain to the specified user.
68
69       RENAME
70           This form changes the name of the domain.
71
72       SET SCHEMA
73           This form changes the schema of the domain. Any constraints
74           associated with the domain are moved into the new schema as well.
75
76       You must own the domain to use ALTER DOMAIN. To change the schema of a
77       domain, you must also have CREATE privilege on the new schema. To alter
78       the owner, you must also be a direct or indirect member of the new
79       owning role, and that role must have CREATE privilege on the domain's
80       schema. (These restrictions enforce that altering the owner doesn't do
81       anything you couldn't do by dropping and recreating the domain.
82       However, a superuser can alter ownership of any domain anyway.)
83

PARAMETERS

85       name
86           The name (possibly schema-qualified) of an existing domain to
87           alter.
88
89       domain_constraint
90           New domain constraint for the domain.
91
92       constraint_name
93           Name of an existing constraint to drop or rename.
94
95       NOT VALID
96           Do not verify existing stored data for constraint validity.
97
98       CASCADE
99           Automatically drop objects that depend on the constraint, and in
100           turn all objects that depend on those objects (see Section 5.14).
101
102       RESTRICT
103           Refuse to drop the constraint if there are any dependent objects.
104           This is the default behavior.
105
106       new_name
107           The new name for the domain.
108
109       new_constraint_name
110           The new name for the constraint.
111
112       new_owner
113           The user name of the new owner of the domain.
114
115       new_schema
116           The new schema for the domain.
117

NOTES

119       Although ALTER DOMAIN ADD CONSTRAINT attempts to verify that existing
120       stored data satisfies the new constraint, this check is not
121       bulletproof, because the command cannot “see” table rows that are newly
122       inserted or updated and not yet committed. If there is a hazard that
123       concurrent operations might insert bad data, the way to proceed is to
124       add the constraint using the NOT VALID option, commit that command,
125       wait until all transactions started before that commit have finished,
126       and then issue ALTER DOMAIN VALIDATE CONSTRAINT to search for data
127       violating the constraint. This method is reliable because once the
128       constraint is committed, all new transactions are guaranteed to enforce
129       it against new values of the domain type.
130
131       Currently, ALTER DOMAIN ADD CONSTRAINT, ALTER DOMAIN VALIDATE
132       CONSTRAINT, and ALTER DOMAIN SET NOT NULL will fail if the named domain
133       or any derived domain is used within a container-type column (a
134       composite, array, or range column) in any table in the database. They
135       should eventually be improved to be able to verify the new constraint
136       for such nested values.
137

EXAMPLES

139       To add a NOT NULL constraint to a domain:
140
141           ALTER DOMAIN zipcode SET NOT NULL;
142
143       To remove a NOT NULL constraint from a domain:
144
145           ALTER DOMAIN zipcode DROP NOT NULL;
146
147       To add a check constraint to a domain:
148
149           ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
150
151       To remove a check constraint from a domain:
152
153           ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
154
155       To rename a check constraint on a domain:
156
157           ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
158
159       To move the domain into a different schema:
160
161           ALTER DOMAIN zipcode SET SCHEMA customers;
162

COMPATIBILITY

164       ALTER DOMAIN conforms to the SQL standard, except for the OWNER,
165       RENAME, SET SCHEMA, and VALIDATE CONSTRAINT variants, which are
166       PostgreSQL extensions. The NOT VALID clause of the ADD CONSTRAINT
167       variant is also a PostgreSQL extension.
168

SEE ALSO

170       CREATE DOMAIN (CREATE_DOMAIN(7)), DROP DOMAIN (DROP_DOMAIN(7))
171
172
173
174PostgreSQL 13.3                      2021                      ALTER DOMAIN(7)
Impressum