4 ALTER DOMAIN — change the definition of a domain
9 { SET DEFAULT expression | DROP DEFAULT }
11 { SET | DROP } NOT NULL
13 ADD domain_constraint [ NOT VALID ]
15 DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
17 RENAME CONSTRAINT constraint_name TO new_constraint_name
19 VALIDATE CONSTRAINT constraint_name
21 OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
27 where domain_constraint is:
29 [ CONSTRAINT constraint_name ]
30 { NOT NULL | CHECK (expression) }
34 ALTER DOMAIN changes the definition of an existing domain. There are
38 These forms set or remove the default value for a domain. Note
39 that defaults only apply to subsequent INSERT commands; they do
40 not affect rows already in a table using the domain.
43 These forms change whether a domain is marked to allow NULL
44 values or to reject NULL values. You can only SET NOT NULL when
45 the columns using the domain contain no null values.
47 ADD domain_constraint [ NOT VALID ]
48 This form adds a new constraint to a domain. When a new
49 constraint is added to a domain, all columns using that domain
50 will be checked against the newly added constraint. These checks
51 can be suppressed by adding the new constraint using the NOT
52 VALID option; the constraint can later be made valid using ALTER
53 DOMAIN ... VALIDATE CONSTRAINT. Newly inserted or updated rows
54 are always checked against all constraints, even those marked
55 NOT VALID. NOT VALID is only accepted for CHECK constraints.
57 DROP CONSTRAINT [ IF EXISTS ]
58 This form drops constraints on a domain. If IF EXISTS is
59 specified and the constraint does not exist, no error is thrown.
60 In this case a notice is issued instead.
63 This form changes the name of a constraint on a domain.
66 This form validates a constraint previously added as NOT VALID,
67 that is, it verifies that all values in table columns of the
68 domain type satisfy the specified constraint.
71 This form changes the owner of the domain to the specified user.
74 This form changes the name of the domain.
77 This form changes the schema of the domain. Any constraints
78 associated with the domain are moved into the new schema as
81 You must own the domain to use ALTER DOMAIN. To change the schema of a
82 domain, you must also have CREATE privilege on the new schema. To alter
83 the owner, you must be able to SET ROLE to the new owning role, and
84 that role must have CREATE privilege on the domain's schema. (These
85 restrictions enforce that altering the owner doesn't do anything you
86 couldn't do by dropping and recreating the domain. However, a superuser
87 can alter ownership of any domain anyway.)
92 The name (possibly schema-qualified) of an existing domain to
96 New domain constraint for the domain.
99 Name of an existing constraint to drop or rename.
102 Do not verify existing stored data for constraint validity.
105 Automatically drop objects that depend on the constraint, and in
106 turn all objects that depend on those objects (see
110 Refuse to drop the constraint if there are any dependent
111 objects. This is the default behavior.
114 The new name for the domain.
117 The new name for the constraint.
120 The user name of the new owner of the domain.
123 The new schema for the domain.
127 Although ALTER DOMAIN ADD CONSTRAINT attempts to verify that existing
128 stored data satisfies the new constraint, this check is not
129 bulletproof, because the command cannot “see” table rows that are newly
130 inserted or updated and not yet committed. If there is a hazard that
131 concurrent operations might insert bad data, the way to proceed is to
132 add the constraint using the NOT VALID option, commit that command,
133 wait until all transactions started before that commit have finished,
134 and then issue ALTER DOMAIN VALIDATE CONSTRAINT to search for data
135 violating the constraint. This method is reliable because once the
136 constraint is committed, all new transactions are guaranteed to enforce
137 it against new values of the domain type.
139 Currently, ALTER DOMAIN ADD CONSTRAINT, ALTER DOMAIN VALIDATE
140 CONSTRAINT, and ALTER DOMAIN SET NOT NULL will fail if the named domain
141 or any derived domain is used within a container-type column (a
142 composite, array, or range column) in any table in the database. They
143 should eventually be improved to be able to verify the new constraint
144 for such nested values.
148 To add a NOT NULL constraint to a domain:
149 ALTER DOMAIN zipcode SET NOT NULL;
151 To remove a NOT NULL constraint from a domain:
152 ALTER DOMAIN zipcode DROP NOT NULL;
154 To add a check constraint to a domain:
155 ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
157 To remove a check constraint from a domain:
158 ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
160 To rename a check constraint on a domain:
161 ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
163 To move the domain into a different schema:
164 ALTER DOMAIN zipcode SET SCHEMA customers;
168 ALTER DOMAIN conforms to the SQL standard, except for the OWNER,
169 RENAME, SET SCHEMA, and VALIDATE CONSTRAINT variants, which are
170 PostgreSQL extensions. The NOT VALID clause of the ADD CONSTRAINT
171 variant is also a PostgreSQL extension.
175 CREATE DOMAIN, DROP DOMAIN