forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Reviewed-by: Rechenburg, Matthias <matthias.rechenburg@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
35 lines
7.0 KiB
HTML
35 lines
7.0 KiB
HTML
<a name="EN-US_TOPIC_0000001584706597"></a><a name="EN-US_TOPIC_0000001584706597"></a>
|
|
|
|
<h1 class="topictitle1">What Are the Differences Between Unique Constraints and Unique Indexes?</h1>
|
|
<div id="body0000001584706597"><ul id="EN-US_TOPIC_0000001584706597__ul1146312292615"><li id="EN-US_TOPIC_0000001584706597__li2302181032816">The concepts of a unique constraint and a unique index are different.<p id="EN-US_TOPIC_0000001584706597__p1273514114288"><a name="EN-US_TOPIC_0000001584706597__li2302181032816"></a><a name="li2302181032816"></a>A unique constraint specifies that the values in a column or a group of columns are all unique. If <strong id="EN-US_TOPIC_0000001584706597__b119271137158">DISTRIBUTE BY REPLICATION</strong> is not specified, the column table that contains only unique values must contain distribution columns.</p>
|
|
<p id="EN-US_TOPIC_0000001584706597__p1551657132910">A unique index is used to ensure the uniqueness of a field value or the value combination of multiple fields. <strong id="EN-US_TOPIC_0000001584706597__b17606183311299">CREATE UNIQUE INDEX</strong> creates a unique index.</p>
|
|
</li><li id="EN-US_TOPIC_0000001584706597__li105971141284">The functions of a unique constraint and a unique index are different.<p id="EN-US_TOPIC_0000001584706597__p456617172819"><a name="EN-US_TOPIC_0000001584706597__li105971141284"></a><a name="li105971141284"></a>Constraints are used to ensure data integrity, and indexes are used to facilitate query.</p>
|
|
</li><li id="EN-US_TOPIC_0000001584706597__li1620492110263">The usages of a unique constraint and a unique index are different.<ol id="EN-US_TOPIC_0000001584706597__ol11416152582218"><li id="EN-US_TOPIC_0000001584706597__li64161825142217">Both unique constraints and unique indexes can be used to ensure the uniqueness of column values which can be NULL.</li><li id="EN-US_TOPIC_0000001584706597__li15880154010269">When a unique constraint is created, a unique index with the same name is automatically created. The index cannot be deleted separately. When the constraint is deleted, the index is automatically deleted. A unique constraint uses a unique index to ensure data uniqueness. GaussDB(DWS) row-store tables support unique constraints, but column-store tables do not.</li><li id="EN-US_TOPIC_0000001584706597__li16361579276">A created unique index is independent and can be deleted separately. Currently in GaussDB(DWS), unique indexes can only be created using B-Tree.</li><li id="EN-US_TOPIC_0000001584706597__li162442191272">If you want to have both a unique constraint and a unique index on a column, and they can be deleted separately, you can create a unique index and then a unique constraint with the same name.</li><li id="EN-US_TOPIC_0000001584706597__li03302029192711">If a field in a table is to be used as a foreign key of another table, the field must have a unique constraint (or it is a primary key). If the field has only a unique index, an error is reported.</li></ol>
|
|
</li></ul>
|
|
<p id="EN-US_TOPIC_0000001584706597__p1021517332111">Example: Create a composite index for two columns, which is not required to be a unique index.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001584706597__screen108004455119"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">(</span><span class="n">n1</span><span class="w"> </span><span class="nb">number</span><span class="p">,</span><span class="n">n2</span><span class="w"> </span><span class="nb">number</span><span class="p">,</span><span class="n">n3</span><span class="w"> </span><span class="nb">number</span><span class="p">,</span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="p">(</span><span class="n">n3</span><span class="p">));</span>
|
|
<span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">t_idx</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">t</span><span class="p">(</span><span class="n">n1</span><span class="p">,</span><span class="n">n2</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001584706597__p143255259254">GaussDB (DWS) supports multiple unique indexes for a table.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001584706597__screen13386121012517"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">UNIQUE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">u_index</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">t</span><span class="p">(</span><span class="n">n3</span><span class="p">);</span>
|
|
<span class="k">CREATE</span><span class="w"> </span><span class="k">UNIQUE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">u_index1</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">t</span><span class="p">(</span><span class="n">n3</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001584706597__p02151133131110">You can use the index <strong id="EN-US_TOPIC_0000001584706597__b598152045">t_idx</strong> created in the example above to create a unique constraint <strong id="EN-US_TOPIC_0000001584706597__b66761565416">t_uk</strong>, which is unique only on column <strong id="EN-US_TOPIC_0000001584706597__b13377189415">n1</strong>. A unique constraint is stricter than a unique index.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001584706597__screen104831945101219"><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">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="k">ADD</span><span class="w"> </span><span class="k">CONSTRAINT</span><span class="w"> </span><span class="n">t_uk</span><span class="w"> </span><span class="k">UNIQUE</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">u_index</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_03_0065.html">Database Usage</a></div>
|
|
</div>
|
|
</div>
|
|
|