<?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>Oracle Database Tips blog &#187; oracle forein key constraint</title>
	<atom:link href="http://oracle-database-tips.com/wp/tag/oracle-forein-key-constraint/feed" rel="self" type="application/rss+xml" />
	<link>http://oracle-database-tips.com/wp</link>
	<description>News for Oracle professionals</description>
	<lastBuildDate>Fri, 11 May 2012 12:12:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Foreign key constraints and other type of constraints in an Oracle database</title>
		<link>http://oracle-database-tips.com/wp/sql/foreign-key-constraints-and-other-type-of-constraints-in-an-oracle-database</link>
		<comments>http://oracle-database-tips.com/wp/sql/foreign-key-constraints-and-other-type-of-constraints-in-an-oracle-database#comments</comments>
		<pubDate>Fri, 23 Oct 2009 11:16:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[amp]]></category>
		<category><![CDATA[check constraint]]></category>
		<category><![CDATA[check constraints]]></category>
		<category><![CDATA[empl]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[jones 1]]></category>
		<category><![CDATA[lower case]]></category>
		<category><![CDATA[null value]]></category>
		<category><![CDATA[oracle constraint]]></category>
		<category><![CDATA[oracle constraints]]></category>
		<category><![CDATA[oracle forein key constraint]]></category>
		<category><![CDATA[surname table]]></category>
		<category><![CDATA[uniqueness]]></category>
		<category><![CDATA[upper case]]></category>

		<guid isPermaLink="false">http://oracle-database-tips.com/wp/?p=79</guid>
		<description><![CDATA[A foreign key is one of the different types of Oracle constraints.]]></description>
			<content:encoded><![CDATA[<p>A foreign key is one of the different types of Oracle constraints.<br />
The purpose of a constraint is literally to constrain or restrict the quality of          data which you are allowed to insert into a table.</p>
<p>There are 5 kinds of constraints :</p>
<ul class="medium">
<li><a href="#CK">Check</a></li>
<li><a href=#NK">Not-Null</a></li>
<li><a href="#UK">Unique</a></li>
<li><a href="#PK">Primary Key</a></li>
<li><a href="l#FK">Foreign Key</a></li>
</ul>
<h3><a id="CK" name="CK"></a><br />
Check Constraints</h3>
<p>A check constraint allows you to define a function which determines whether          the data is allowable or not.</p>
<p>Example: Let&#8217;s add a check constraint to employees.surname  (table Employees, column Surname)<br />
which will only allow characters to be entered in upper case:</p>
<div id="textbox">
<div>SQL&gt; alter table employees add constraint ch_employees check (surname = upper(surname));</div>
</div>
<p>If you try to add a surname in lower case , you will receive the following        error:</p>
<div id="textbox">
<div>SQL&amp;gtinsert into employees (surname) values (&#8216;jones&#8217;)</p>
<p>ORA-02290: check constraint (CH_EMPLOYEES) violated</p></div>
</div>
<p>Now let&#8217;s add it the proper way:</p>
<div id="textbox">
<div>SQL&gt; insert into employees (surname) values (&#8216;JONES&#8217;);</p>
<p>1 row created.</p></div>
</div>
<h3><a id="NK" name="NK"></a><br />
Not-Null Constraints</h3>
<p>This simple constraint determines whether your column is allowed to contain    null data.<br />
By default, a column can contain null data, unless you add a not-null constraint.</p>
<p>Example :  Let&#8217;s add a not-null constraint to Employees.Surname</p>
<div id="textbox">
<div>SQL&gt;alter table employees modify (surname not null);</div>
</div>
<p>Now try to insert a null value.</p>
<div id="textbox">
<div>SQL&gt; insert into employees (surname) values (null);</p>
<p>ORA-01400: cannot insert NULL into (&#8220;EMPLOYEES&#8221;.&#8221;SURNAME&#8221;)</p></div>
</div>
<h3><a id="UK" name="UK"></a><br />
Unique Constraints</h3>
<p>These constraints enforce uniqueness in your column&#8217;s data : it will stop duplicated    data from being inserted.</p>
<p>Example:</p>
<div id="textbox">
<div>SQL&gt; alter table employees add constraint u_employees unique enable validate;</div>
</div>
<p>We know there is already an entry named &#8216;JONES&#8217; in the table, so let&#8217;s see    what happens when we try to insert it again:</p>
<div id="textbox">
<div>SQL&gt; insert into employees (surname) values (&#8216;JONES&#8217;);</p>
<p>ORA-00001: unique constraint (U_EMPLOYEES) violated</p></div>
</div>
<h3><a id="PK" name="PK"></a><br />
Primary Key Constraints</h3>
<p>A primary key constraint is a combination of a unique <em>and</em> a not-null constraint.<br />
It allows you to use a column in your table to uniquely identify each row by your predetermined key.<br />
Creating a primary key automatically creates a unique index of the same name on the table.<br />
This is how Oracle enforces the uniqueness.</p>
<p>Example: Before we can create a primary key, let&#8217;s drop the previous not-null and unique key constraints.</p>
<div id="textbox">
<div>SQL&gt; alter table employees modify(surname  null);</p>
<p>SQL&gt; alter table employees  drop constraint u_employees;</p>
<p>SQL&gt; alter table employees add constraint pk_employee primary key (surname);</p></div>
</div>
<p>Either inserting a pre-existing surname or attempting to insert a null value into the surname column<br />
will now result in an error.</p>
<div id="textbox">
<div>SQL&gt; insert into employees (surname) values (&#8216;JONES&#8217;);</p>
<p>ORA-00001: unique constraint (PK_EMPLOYEES) violated</p>
<p>SQL&gt; insert into employees (surname) values (null);</p>
<p>ORA-01400: cannot insert NULL into (&#8220;EMPLOYEES&#8221;.&#8221;SURNAME&#8221;)</p></div>
</div>
<h3><a id="FK" name="FK"></a><br />
Foreign Key Constraints</h3>
<p>Another name for these is relational integrity constraints.<br />
They enforce logical relationships between parent and child tables.</p>
<p>Let&#8217;s use a geographical example.</p>
<p>We have a parent table called Countries and a child table called Cities.<br />
A row in the Cities table must be able to find its parent Country in the Countries table<br />
before you are allowed to  insert the City.</p>
<p>Parent table <em>Countries</em> :</p>
<table border="1" cellspacing="0" cellpadding="0" width="203">
<tbody>
<tr>
<td width="64">CountryID</td>
<td width="133">CountryName</td>
</tr>
<tr>
<td>1</td>
<td>UK</td>
</tr>
<tr>
<td>2</td>
<td>USA</td>
</tr>
<tr>
<td>3</td>
<td>INDIA</td>
</tr>
</tbody>
</table>
<p>Child table <em>Cities</em>:</p>
<table border="1" cellspacing="0" cellpadding="0" width="294">
<tbody>
<tr>
<td width="80">CityID</td>
<td width="108">CityName</td>
<td width="98">CountryID</td>
</tr>
<tr>
<td>M00001</td>
<td>MADRAS</td>
<td>3</td>
</tr>
<tr>
<td>F00001</td>
<td>FARMINGTON</td>
<td>2</td>
</tr>
<tr>
<td>B00001</td>
<td>BOISE</td>
<td>2</td>
</tr>
<tr>
<td>L00001</td>
<td>LONDON</td>
<td>1</td>
</tr>
<tr>
<td>B00002</td>
<td>BANGALORE</td>
<td>3</td>
</tr>
</tbody>
</table>
<p>To implement this is slightly more complicated than the other constraints:<br />
First you create the primary key on the parent table:</p>
<div id="textbox">
<div>SQL&gt; alter table countries add constraint pk_countries  primary key (countryid);</div>
</div>
<p>Now we create the foreign key constraint on the child table which references the primary key on the parent table:</p>
<div id="textbox">
<div>SQL&gt; alter table cities add constraint fk_cities foreign key (countryid) references countries (countryid);</div>
</div>
<p>Time to test:  let&#8217;s try to add a row in Cities with a non-existent Countryid.</p>
<div id="textbox">
<div>SQL&gt; insert into cities (cityid, cityname, countryid) values (&#8216;M00002&#8242;,&#8217;MELBOURNE&#8217;,5);</p>
<p>ORA-02291: integrity constraint (FK_CITIES) violated &#8211; parent key not found</p></div>
</div>
<h3>Recommended reading:</h3>
<div id="content">
<li><a title="opens new window" onclick="window.open('http://www.oracle-database-tips.com/cgi-bin/counter.pl?url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FB19306_01%2Fserver.102%2Fb14220%2Fintro.htm%23sthref227&amp;referrer=http%3A%2F%2Fwww.oracle-database-tips.com%2Fforeign_key.html'); return false;" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/intro.htm#sthref227" target="_blank">Oracle online documentation: Database Concepts</a></li>
</div>
]]></content:encoded>
			<wfw:commentRss>http://oracle-database-tips.com/wp/sql/foreign-key-constraints-and-other-type-of-constraints-in-an-oracle-database/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

