]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-alterdomain.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / sql-alterdomain.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>ALTER DOMAIN</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES" /><link rel="next" href="sql-altereventtrigger.html" title="ALTER EVENT TRIGGER" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">ALTER DOMAIN</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-altereventtrigger.html" title="ALTER EVENT TRIGGER">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-ALTERDOMAIN"><div class="titlepage"></div><a id="id-1.9.3.9.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER DOMAIN</span></h2><p>ALTER DOMAIN — 
3    change the definition of a domain
4   </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
5 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
6     { SET DEFAULT <em class="replaceable"><code>expression</code></em> | DROP DEFAULT }
7 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
8     { SET | DROP } NOT NULL
9 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
10     ADD <em class="replaceable"><code>domain_constraint</code></em> [ NOT VALID ]
11 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
12     DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]
13 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
14      RENAME CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> TO <em class="replaceable"><code>new_constraint_name</code></em>
15 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
16     VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>
17 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
18     OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
19 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
20     RENAME TO <em class="replaceable"><code>new_name</code></em>
21 ALTER DOMAIN <em class="replaceable"><code>name</code></em>
22     SET SCHEMA <em class="replaceable"><code>new_schema</code></em>
23
24 <span class="phrase">where <em class="replaceable"><code>domain_constraint</code></em> is:</span>
25
26 [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
27 { NOT NULL | CHECK (<em class="replaceable"><code>expression</code></em>) }
28 </pre></div><div class="refsect1" id="id-1.9.3.9.5"><h2>Description</h2><p>
29    <code class="command">ALTER DOMAIN</code> changes the definition of an existing domain.
30    There are several sub-forms:
31   </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP DEFAULT</code></span></dt><dd><p>
32       These forms set or remove the default value for a domain. Note
33       that defaults only apply to subsequent <code class="command">INSERT</code>
34       commands; they do not affect rows already in a table using the domain.
35      </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP NOT NULL</code></span></dt><dd><p>
36       These forms change whether a domain is marked to allow NULL
37       values or to reject NULL values.  You can only <code class="literal">SET NOT NULL</code>
38       when the columns using the domain contain no null values.
39      </p></dd><dt><span class="term"><code class="literal">ADD <em class="replaceable"><code>domain_constraint</code></em> [ NOT VALID ]</code></span></dt><dd><p>
40       This form adds a new constraint to a domain.
41       When a new constraint is added to a domain, all columns using that
42       domain will be checked against the newly added constraint.  These
43       checks can be suppressed by adding the new constraint using the
44       <code class="literal">NOT VALID</code> option; the constraint can later be made
45       valid using <code class="command">ALTER DOMAIN ... VALIDATE CONSTRAINT</code>.
46       Newly inserted or updated rows are always checked against all
47       constraints, even those marked <code class="literal">NOT VALID</code>.
48       <code class="literal">NOT VALID</code> is only accepted for <code class="literal">CHECK</code> constraints.
49      </p></dd><dt><span class="term"><code class="literal">DROP CONSTRAINT [ IF EXISTS ]</code></span></dt><dd><p>
50       This form drops constraints on a domain.
51       If <code class="literal">IF EXISTS</code> is specified and the constraint
52       does not exist, no error is thrown. In this case a notice is issued instead.
53      </p></dd><dt><span class="term"><code class="literal">RENAME CONSTRAINT</code></span></dt><dd><p>
54       This form changes the name of a constraint on a domain.
55      </p></dd><dt><span class="term"><code class="literal">VALIDATE CONSTRAINT</code></span></dt><dd><p>
56       This form validates a constraint previously added as
57       <code class="literal">NOT VALID</code>, that is, it verifies that all values in
58       table columns of the domain type satisfy the specified constraint.
59      </p></dd><dt><span class="term"><code class="literal">OWNER</code></span></dt><dd><p>
60       This form changes the owner of the domain to the specified user.
61      </p></dd><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>
62       This form changes the name of the domain.
63      </p></dd><dt><span class="term"><code class="literal">SET SCHEMA</code></span></dt><dd><p>
64       This form changes the schema of the domain.  Any constraints
65       associated with the domain are moved into the new schema as well.
66      </p></dd></dl></div><p>
67    You must own the domain to use <code class="command">ALTER DOMAIN</code>.
68    To change the schema of a domain, you must also have
69    <code class="literal">CREATE</code> privilege on the new schema.
70    To alter the owner, you must be able to <code class="literal">SET ROLE</code> to the
71    new owning role, and that role must have <code class="literal">CREATE</code> privilege
72    on the domain's schema.  (These restrictions enforce that altering the owner
73    doesn't do anything you couldn't do by dropping and recreating the domain.
74    However, a superuser can alter ownership of any domain anyway.)
75   </p></div><div class="refsect1" id="id-1.9.3.9.6"><h2>Parameters</h2><p>
76     </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
77         The name (possibly schema-qualified) of an existing domain to
78         alter.
79        </p></dd><dt><span class="term"><em class="replaceable"><code>domain_constraint</code></em></span></dt><dd><p>
80         New domain constraint for the domain.
81        </p></dd><dt><span class="term"><em class="replaceable"><code>constraint_name</code></em></span></dt><dd><p>
82         Name of an existing constraint to drop or rename.
83        </p></dd><dt><span class="term"><code class="literal">NOT VALID</code></span></dt><dd><p>
84         Do not verify existing stored data for constraint validity.
85        </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
86         Automatically drop objects that depend on the constraint,
87         and in turn all objects that depend on those objects
88         (see <a class="xref" href="ddl-depend.html" title="5.15. Dependency Tracking">Section 5.15</a>).
89        </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
90         Refuse to drop the constraint if there are any dependent
91         objects. This is the default behavior.
92        </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
93         The new name for the domain.
94        </p></dd><dt><span class="term"><em class="replaceable"><code>new_constraint_name</code></em></span></dt><dd><p>
95         The new name for the constraint.
96        </p></dd><dt><span class="term"><em class="replaceable"><code>new_owner</code></em></span></dt><dd><p>
97         The user name of the new owner of the domain.
98        </p></dd><dt><span class="term"><em class="replaceable"><code>new_schema</code></em></span></dt><dd><p>
99         The new schema for the domain.
100        </p></dd></dl></div><p>
101    </p></div><div class="refsect1" id="id-1.9.3.9.7"><h2>Notes</h2><p>
102    Although <code class="command">ALTER DOMAIN ADD CONSTRAINT</code> attempts to verify
103    that existing stored data satisfies the new constraint, this check is not
104    bulletproof, because the command cannot <span class="quote">“<span class="quote">see</span>”</span> table rows that
105    are newly inserted or updated and not yet committed.  If there is a hazard
106    that concurrent operations might insert bad data, the way to proceed is to
107    add the constraint using the <code class="literal">NOT VALID</code> option, commit
108    that command, wait until all transactions started before that commit have
109    finished, and then issue <code class="command">ALTER DOMAIN VALIDATE
110    CONSTRAINT</code> to search for data violating the constraint.  This
111    method is reliable because once the constraint is committed, all new
112    transactions are guaranteed to enforce it against new values of the domain
113    type.
114   </p><p>
115    Currently, <code class="command">ALTER DOMAIN ADD CONSTRAINT</code>, <code class="command">ALTER
116    DOMAIN VALIDATE CONSTRAINT</code>, and <code class="command">ALTER DOMAIN SET NOT
117    NULL</code> will fail if the named domain or any derived domain is used
118    within a container-type column (a composite, array, or range column) in
119    any table in the database.  They should eventually be improved to be able
120    to verify the new constraint for such nested values.
121   </p></div><div class="refsect1" id="id-1.9.3.9.8"><h2>Examples</h2><p>
122    To add a <code class="literal">NOT NULL</code> constraint to a domain:
123 </p><pre class="programlisting">
124 ALTER DOMAIN zipcode SET NOT NULL;
125 </pre><p>
126    To remove a <code class="literal">NOT NULL</code> constraint from a domain:
127 </p><pre class="programlisting">
128 ALTER DOMAIN zipcode DROP NOT NULL;
129 </pre><p>
130   </p><p>
131    To add a check constraint to a domain:
132 </p><pre class="programlisting">
133 ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
134 </pre><p>
135   </p><p>
136    To remove a check constraint from a domain:
137 </p><pre class="programlisting">
138 ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
139 </pre><p>
140   </p><p>
141    To rename a check constraint on a domain:
142 </p><pre class="programlisting">
143 ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
144 </pre><p>
145   </p><p>
146    To move the domain into a different schema:
147 </p><pre class="programlisting">
148 ALTER DOMAIN zipcode SET SCHEMA customers;
149 </pre></div><div class="refsect1" id="SQL-ALTERDOMAIN-COMPATIBILITY"><h2>Compatibility</h2><p>
150    <code class="command">ALTER DOMAIN</code> conforms to the <acronym class="acronym">SQL</acronym>
151    standard, except for the <code class="literal">OWNER</code>, <code class="literal">RENAME</code>, <code class="literal">SET SCHEMA</code>, and
152    <code class="literal">VALIDATE CONSTRAINT</code> variants, which are
153    <span class="productname">PostgreSQL</span> extensions.  The <code class="literal">NOT VALID</code>
154    clause of the <code class="literal">ADD CONSTRAINT</code> variant is also a
155    <span class="productname">PostgreSQL</span> extension.
156   </p></div><div class="refsect1" id="SQL-ALTERDOMAIN-SEE-ALSO"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createdomain.html" title="CREATE DOMAIN"><span class="refentrytitle">CREATE DOMAIN</span></a>, <a class="xref" href="sql-dropdomain.html" title="DROP DOMAIN"><span class="refentrytitle">DROP DOMAIN</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-altereventtrigger.html" title="ALTER EVENT TRIGGER">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER DEFAULT PRIVILEGES </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> ALTER EVENT TRIGGER</td></tr></table></div></body></html>