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