ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafyrefinteg.htm

103 lines
6.4 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Referential integrity and tables" />
<meta name="abstract" content="Referential integrity is the condition of a set of tables in a database in which all references from one table to another are valid." />
<meta name="description" content="Referential integrity is the condition of a set of tables in a database in which all references from one table to another are valid." />
<meta name="DC.subject" content="referential integrity" />
<meta name="keywords" content="referential integrity" />
<meta name="DC.Relation" scheme="URI" content="rbafysqlpctablecreate.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyaddor.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyaddrcon.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafysamptblx.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmsthctabl.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstatabl.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbafyrefinteg" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Referential integrity and tables</title>
</head>
<body id="rbafyrefinteg"><a name="rbafyrefinteg"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Referential integrity and tables</h1>
<div><p>Referential integrity is the condition of a set of tables in a
database in which all references from one table to another are valid. </p>
<div class="section"><p>Consider
the following example: </p>
<ul><li>CORPDATA.EMPLOYEE serves as a master list of employees.</li>
<li>CORPDATA.DEPARTMENT acts as a master list of all valid department numbers.</li>
<li>CORPDATA.EMP_ACT provides a master list of activities performed for projects.</li>
</ul>
<p>Other tables refer to the same entities described in these tables.
When a table contains data for which there is a master list, that data should
actually appear in the master list, or the reference is not valid. The table
that contains the master list is the <dfn class="term">parent table</dfn>, and the table
that refers to it is a <dfn class="term">dependent table</dfn>. When the references from
the dependent table to the parent table are valid, the condition of the set
of tables is called <dfn class="term">referential integrity</dfn>.</p>
</div>
<div class="section"><p>Stated another way, referential integrity is the state of a database
in which all values of all foreign keys are valid. Each value of the foreign
key must also exist in the parent key or be null. This definition of referential
integrity requires an understanding of the following terms: </p>
<ul><li>A <dfn class="term">unique key</dfn> is a column or set of columns in a table which
uniquely identify a row. Although a table can have several unique keys, no
two rows in a table can have the same unique key value.</li>
<li>A <dfn class="term">primary key</dfn> is a unique key that does not allow nulls.
A table cannot have more than one primary key.</li>
<li>A <dfn class="term">parent key</dfn> is either a unique key or a primary key which
is referenced in a referential constraint.</li>
<li>A <dfn class="term">foreign key</dfn> is a column or set of columns whose values
must match those of a parent key. If any column value used to build the foreign
key is null, then the rule does not apply.</li>
<li>A <dfn class="term">parent table</dfn> is a table that contains the parent key.</li>
<li>A <dfn class="term">dependent table</dfn> is the table that contains the foreign
key.</li>
<li>A <dfn class="term">descendent table</dfn> is a table that is a dependent table or
a descendent of a dependent table.</li>
</ul>
</div>
<div class="section"><p>Enforcement of referential integrity prevents the violation of
the rule that states that every non-null foreign key must have
a matching parent key.</p>
</div>
<div class="section"><p>SQL supports the referential integrity concept with the CREATE
TABLE and ALTER TABLE statements. </p>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rbafyaddor.htm">Add or drop referential constraints</a></strong><br />
Constraints are rules that ensure that references from one table, a dependent table, to data in another table, the parent table, are valid. You use referential constraints to ensure referential integrity.</li>
<li class="ulchildlink"><strong><a href="rbafyaddrcon.htm">Add referential constraints</a></strong><br />
The rule that every department number shown in the sample employee table must appear in the department table is a referential constraint.</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafysqlpctablecreate.htm" title="A table can be visualized as a two-dimensional arrangement of data consisting of rows and columns.">Create a table</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="rbafysamptblx.htm" title="This topic contains the sample tables referred to and used in this topic and the SQL Reference topic collection.">DB2 UDB for iSeries sample tables</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmsthctabl.htm">CREATE TABLE</a></div>
<div><a href="../db2/rbafzmstatabl.htm">ALTER TABLE</a></div>
</div>
</div>
</body>
</html>