forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
38 lines
7.9 KiB
HTML
38 lines
7.9 KiB
HTML
<a name="EN-US_TOPIC_0000001188270524"></a><a name="EN-US_TOPIC_0000001188270524"></a>
|
|
|
|
<h1 class="topictitle1">SET CONSTRAINTS</h1>
|
|
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001188270524__s6cd4f95fe13642ca92c206b447874035"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001188270524__p19369105415508"><strong id="EN-US_TOPIC_0000001188270524__b15234976143240">SET CONSTRAINTS</strong> sets the behavior of constraint checking within the current transaction.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__a34d0f1def03142b69ae01526c0273730"><strong id="EN-US_TOPIC_0000001188270524__b2897056143240">IMMEDIATE</strong> constraints are checked at the end of each statement. <strong id="EN-US_TOPIC_0000001188270524__b26073505143240">DEFERRED</strong> constraints are not checked until transaction commit. Each constraint has its own <strong id="EN-US_TOPIC_0000001188270524__b33334956143240">IMMEDIATE</strong> or <strong id="EN-US_TOPIC_0000001188270524__b31579155143240">DEFERRED</strong> mode.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__a31751946ef5640f09afa0e4ef4527aef">Upon creation, a constraint is given one of three characteristics <strong id="EN-US_TOPIC_0000001188270524__b7774749143240">DEFERRABLE INITIALLY DEFERRED</strong>, <strong id="EN-US_TOPIC_0000001188270524__b2863883143240">DEFERRABLE INITIALLY IMMEDIATE</strong>, or <strong id="EN-US_TOPIC_0000001188270524__b25774952143240">NOT DEFERRABLE</strong>. The third class is always <strong id="EN-US_TOPIC_0000001188270524__b30647981143240">IMMEDIATE</strong> and is not affected by the <strong id="EN-US_TOPIC_0000001188270524__b7396380143240">SET CONSTRAINTS</strong> command. The first two classes start every transaction in specified modes, but its behaviors can be changed within a transaction by <strong id="EN-US_TOPIC_0000001188270524__b66567420143240">SET CONSTRAINTS</strong>.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__af833461101bd4f89ae7dcb2580cc75b0"><strong id="EN-US_TOPIC_0000001188270524__b84235270616304">SET CONSTRAINTS</strong> with a list of constraint names changes the mode of just those constraints (which must all be deferrable). If multiple constraints match a name, the name is affected by all of these constraints. <strong id="EN-US_TOPIC_0000001188270524__b7940828143240">SET CONSTRAINTS ALL</strong> changes the modes of all deferrable constraints.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__a7e838d6b764548fe9e74123650098eb8">When <strong id="EN-US_TOPIC_0000001188270524__b39227321143240">SET CONSTRAINTS</strong> changes the mode of a constraint from <strong id="EN-US_TOPIC_0000001188270524__b17501571143240">DEFERRED</strong> to <strong id="EN-US_TOPIC_0000001188270524__b23296414143240">IMMEDIATE</strong>, the new mode takes effect retroactively: any outstanding data modifications that would have been checked at the end of the transaction are instead checked during the execution of the <strong id="EN-US_TOPIC_0000001188270524__b8341140143240">SET CONSTRAINTS</strong> command. If any such constraint is violated, the <strong id="EN-US_TOPIC_0000001188270524__b7961396143240">SET CONSTRAINTS</strong> fails (and does not change the constraint mode). Therefore, <strong id="EN-US_TOPIC_0000001188270524__b4543705143240">SET CONSTRAINTS</strong> can be used to force checking of constraints to occur at a specific point in a transaction.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__a10af138c1961443ea488ba7cb25e9185">Only foreign key constraints are affected by this setting. Check and unique constraints are always checked immediately when a row is inserted or modified.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270524__sc1ab636cd03b4006b1fff98ce85b9560"><h4 class="sectiontitle">Precautions</h4><p id="EN-US_TOPIC_0000001188270524__a4cdbeaff8af9478dad9b5a852b005e70"><strong id="EN-US_TOPIC_0000001188270524__b1515661901813">SET CONSTRAINTS</strong> sets the behavior of constraint checking only within the current transaction. Therefore, if you execute this command outside of a transaction block (<strong id="EN-US_TOPIC_0000001188270524__b101747143240">START TRANSACTION/COMMIT</strong> pair), it will not appear to have any effect.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270524__s5a1312a358fd43daac13d17350d7a888"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270524__s3618a8e3fc1041aa95a6202a3f5a290b"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SET</span><span class="w"> </span><span class="k">CONSTRAINTS</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="k">ALL</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="p">[,</span><span class="w"> </span><span class="p">...]</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="k">DEFERRED</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">IMMEDIATE</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270524__s7a138eb8d5334afe8c6d215ba2fa5f36"><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001188270524__u0652558d22874536a1a98ef661abc0e0"><li id="EN-US_TOPIC_0000001188270524__lda14c31ef0b940e1ba07ab26565c1e70"><strong id="EN-US_TOPIC_0000001188270524__af790a5efaf9a42dd9a513ee70532d8d3">name</strong><p id="EN-US_TOPIC_0000001188270524__af99c0dba9eca4902b18f767d095f20dd">Specifies the constraint name.</p>
|
|
<p id="EN-US_TOPIC_0000001188270524__a62baee53c75a4b3485403e43bffd7670">Value range: an existing constraint name, which can be found in the system catalog <strong id="EN-US_TOPIC_0000001188270524__b157183546562">pg_constraint</strong>.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270524__l4ec2561542f14de9a47ebd445ef88f72"><strong id="EN-US_TOPIC_0000001188270524__aaf31ba930952408aa61ab9c0144f67d4">ALL</strong><p id="EN-US_TOPIC_0000001188270524__a4896d2d6bfb14e74a22321825607eeda">Indicates all constraints.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270524__ld7a653580ff84d5eaa553cf9be5a26a0"><strong id="EN-US_TOPIC_0000001188270524__a375f1496369f4d9786f962fa725792be">DEFERRED</strong><p id="EN-US_TOPIC_0000001188270524__a9a338ea134c54e99a907848a411a46fe">Indicates that constraints are not checked until transaction commit.</p>
|
|
</li><li id="EN-US_TOPIC_0000001188270524__l5458218ab22f413ba065be04da2009d9"><strong id="EN-US_TOPIC_0000001188270524__a2001bd1b04c64467b080a0f0047721d9">IMMEDIATE</strong><p id="EN-US_TOPIC_0000001188270524__a386dbf666fc949338df2b51c5b59a950">Indicates that constraints are checked at the end of each statement.</p>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188270524__sbdb870324aca42b1a4580e8511d1645e"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001188270524__p14170134118016">Set that constraints are checked when a transaction is committed:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188270524__screen1506835116"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SET</span><span class="w"> </span><span class="k">CONSTRAINTS</span><span class="w"> </span><span class="k">ALL</span><span class="w"> </span><span class="k">DEFERRED</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0118.html">DDL Syntax</a></div>
|
|
</div>
|
|
</div>
|
|
|