<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Basics归档 - dbAnt</title>
	<atom:link href="https://www.dbant.com/oracle/basics/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbant.com/oracle/basics/</link>
	<description>Focus on the database architecture.</description>
	<lastBuildDate>Mon, 02 Sep 2024 10:57:26 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://www.dbant.com/wp-content/uploads/2024/08/ant_2.ico</url>
	<title>Basics归档 - dbAnt</title>
	<link>https://www.dbant.com/oracle/basics/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Unique constraint &#038; Unique index in Oracle 11gR2</title>
		<link>https://www.dbant.com/2017/07/30/unique-constraint-unique-index-in-oracle-11gr2/</link>
		
		<dc:creator><![CDATA[dbAnt]]></dc:creator>
		<pubDate>Sun, 30 Jul 2017 05:25:14 +0000</pubDate>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[Oracle]]></category>
		<guid isPermaLink="false">http://www.dbant.com/?p=118</guid>

					<description><![CDATA[<p>今天探索一下唯一性约束和唯一索引的关系，我们先抛出问题，这两者的效果是否一致的呢？ 官方文档的解释： Uniq [&#8230;]</p>
<p><a href="https://www.dbant.com/2017/07/30/unique-constraint-unique-index-in-oracle-11gr2/">Unique constraint &#038; Unique index in Oracle 11gR2</a>最先出现在<a href="https://www.dbant.com">dbAnt</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>今天探索一下唯一性约束和唯一索引的关系，我们先抛出问题，这两者的效果是否一致的呢？</p>
<p>官方文档的解释：</p>
<h3 id="autoId5" class="sect2">Unique Constraints</h3>
<p><a id="sthref626" name="sthref626"></a><a id="sthref627" name="sthref627"></a><a id="sthref628" name="sthref628"></a>A <span class="bold">unique key constraint</span> requires that every value in a column or set of columns be unique. No rows of a table may have duplicate values in a column (the<span class="bold">unique key</span>) or set of columns (the <span class="bold">composite unique key</span>) with a unique key constraint.</p>
<p class="notep1">Note:</p>
<p>The term <span class="bold">key</span> refers only to the columns defined in the integrity constraint. Because the database enforces a unique constraint by implicitly creating or reusing an <a href="glossary.htm#i432409"><span class="xrefglossterm">index</span></a> on the key columns, the term <span class="bold">unique key</span> is sometimes incorrectly used as a synonym for <span class="bold">unique key constraint</span> or <span class="bold">unique index</span>.</p>
<p>&nbsp;</p>
<h3 id="autoId4" class="sect3">Unique and Nonunique Indexes</h3>
<p><a id="sthref293" name="sthref293"></a><a id="sthref294" name="sthref294"></a><a id="sthref295" name="sthref295"></a><a id="sthref296" name="sthref296"></a>Indexes can be <span class="bold">unique</span> or nonunique. Unique indexes guarantee that no two rows of a table have duplicate values in the key column or column. For example, no two employees can have the same employee ID. Thus, in a unique index, one <a id="sthref297" name="sthref297"></a><a href="glossary.htm#CHDEFIHG">rowid</a> exists for each data value. The data in the leaf blocks is sorted only by key.</p>
<p>Nonunique indexes permit duplicates values in the indexed column or columns. For example, the <em>first_name </em>column of the <em>employees </em>table may contain multiple <em>Mike </em>values. For a nonunique index, the rowid is included in the key in sorted order, so nonunique indexes are sorted by the index key and rowid (ascending).</p>
<p>Oracle Database does not index table rows in which all key columns are <a id="sthref298" name="sthref298"></a><a href="glossary.htm#i432506"><span class="xrefglossterm">null</span></a>, except for bitmap indexes or when the cluster key column value is null.</p>
<p>&nbsp;</p>
<h3>【实验】</h3>
<p>创建表T1并添加唯一性约束：</p>
<pre class="brush: sql; title: ; notranslate">

create table t1 (
id varchar2(20),
name varchar2(30),
constraints t1_id_uk unique(id));

</pre>
<p>创建表T2并创建唯一索引：</p>
<pre class="brush: sql; title: ; notranslate">

create table t2 (
id varchar2(20),
name varchar2(30));

create unique index ind_t2_id on t2(id);

</pre>
<p>查看两个表的索引，发现T1表自动创建了唯一索引：</p>
<pre class="brush: sql; title: ; notranslate">

select OWNER,TABLE_NAME,INDEX_NAME,INDEX_TYPE,UNIQUENESS from dba_indexes where table_name in ('T1','T2');

TABLE_NAME     INDEX_NAME    INDEX_TYPE      UNIQUENES
-------------- ------------- --------------- ---------
T1             T1_ID_UK      NORMAL          UNIQUE
T2             IND_T1_ID     NORMAL          UNIQUE

</pre>
<p>索引列也是ID列</p>
<pre class="brush: sql; title: ; notranslate">
select TABLE_OWNER,TABLE_NAME,INDEX_NAME,COLUMN_NAME,COLUMN_POSITION from dba_ind_columns where table_name in ('T1','T2');

TABLE_NAME      INDEX_NAME      COLUMN_NAME     COLUMN_POSITION
--------------- --------------- --------------- ---------------
T1              T1_ID_UK        ID                            1
T2              IND_T1_ID       ID                            1

</pre>
<p>检查两个表的约束，只有T1表存在唯一性约束：</p>
<pre class="brush: sql; title: ; notranslate">
select OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE, STATUS from dba_constraints where table_name in ('T1','T2');

TABLE_NAME      CONSTRAINT_NAME      C STATUS
--------------- -------------------- - --------
T1              T1_ID_UK             U ENABLED

</pre>
<p>T1表插入数据：</p>
<pre class="brush: sql; title: ; notranslate">

insert into t1 values('A0001','TOM');
insert into t1 values('A0001','JACK');
commit;

kamner@TESTDB&gt; insert into t1 values('A0001','JACK');
insert into t1 values('A0001','JACK')
*
ERROR at line 1:
&lt;strong&gt;ORA-00001: unique constraint (KAMNER.T1_ID_UK) violated&lt;/strong&gt;

</pre>
<p>T2表出入数据</p>
<pre class="brush: sql; title: ; notranslate">

insert into t2 values('A0001','TOM');
insert into t2 values('A0001','JACK');
commit;

kamner@TESTDB&gt; insert into t2 values('A0001','JACK');
insert into t2 values('A0001','JACK')
*
ERROR at line 1:
&lt;strong&gt;ORA-00001: unique constraint (KAMNER.IND_T1_ID) violated&lt;/strong&gt;

</pre>
<p>可以看到，T1、T2表都报唯一性约束冲突的错误。</p>
<p>接着，我们插入NULL值。</p>
<pre class="brush: sql; title: ; notranslate">

kamner@TESTDB&gt; insert into t1 values(null,'SCOTT');
1 row created.

kamner@TESTDB&gt; commit;
Commit complete.

kamner@TESTDB&gt; select * from t1;

ID                   NAME
-------------------- ------------------------------
A0001                TOM
                     SCOTT

kamner@TESTDB&gt; insert into t1 values(null,'SCOTT');
1 row created.

kamner@TESTDB&gt; insert into t1 values(null,'SCOTT');
1 row created.

kamner@TESTDB&gt; commit;
Commit complete.

kamner@TESTDB&gt; select * from t1;

ID                   NAME
-------------------- ------------------------------
A0001                TOM
                     SCOTT
                     SCOTT
                     SCOTT

</pre>
<p>我们看到，在Oracle中，每个NULL值都是不同的，因此他不受唯一索引的约束。这里给我们一点启发，在实际引用中，对于有唯一性要求的列，一般要同时添加非空约束。</p>
<p>综上，可以看出对一个表<strong>建唯一性约束和唯一索引的效果是一样的</strong>，而且可以看到，唯一性约束是通过唯一性索引来实现的。对于NULL值的情况，由于索引不会对NULL建索引页，因此更准确的说是在Oracle中，数据库没办法对NULL值进行比较，因此<strong>每个NULL值都看成是不一样的值</strong>。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="https://www.dbant.com/2017/07/30/unique-constraint-unique-index-in-oracle-11gr2/">Unique constraint &#038; Unique index in Oracle 11gR2</a>最先出现在<a href="https://www.dbant.com">dbAnt</a>。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
