103 lines
6.2 KiB
HTML
103 lines
6.2 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="Insert data into tables with referential constraints" />
|
|
<meta name="abstract" content="There are some important things to remember when inserting data into tables with referential constraints." />
|
|
<meta name="description" content="There are some important things to remember when inserting data into tables with referential constraints." />
|
|
<meta name="DC.subject" content="INSERT statement, and referential constraints, referential constraints, inserting into tables, constraint, referential, examples, inserting data with constraints, inserting data with constraints example" />
|
|
<meta name="keywords" content="INSERT statement, and referential constraints, referential constraints, inserting into tables, constraint, referential, examples, inserting data with constraints, inserting data with constraints example" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyinsert.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="rbafyrfinserting" />
|
|
<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>Insert data into tables with referential constraints</title>
|
|
</head>
|
|
<body id="rbafyrfinserting"><a name="rbafyrfinserting"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Insert data into tables with referential constraints</h1>
|
|
<div><p>There are some important things to remember when inserting data
|
|
into tables with referential constraints.</p>
|
|
<div class="section"><p> If you are inserting data into a parent table with a parent key,
|
|
SQL does not allow:</p>
|
|
<ul><li>Duplicate values for the parent key</li>
|
|
<li>If the parent key is a primary key, a null value for any column of the
|
|
primary key</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section"><p>If you are inserting data into a dependent table with foreign
|
|
keys:</p>
|
|
<ul><li>Each non-null value you insert into a foreign key column must be equal
|
|
to some value in the corresponding parent key of the parent table.</li>
|
|
<li>If any column in the foreign key is null, the entire foreign key is considered
|
|
null. If all foreign keys that contain the column are null, the INSERT succeeds
|
|
(as long as there are no unique index violations).</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section"><p>Alter the sample application project table (PROJECT) to define
|
|
two foreign keys:</p>
|
|
<ul><li>A foreign key on the department number (DEPTNO) which references the department
|
|
table</li>
|
|
<li>A foreign key on the employee number (RESPEMP) which references the employee
|
|
table. <pre><strong>ALTER TABLE</strong> CORPDATA.PROJECT <strong>ADD CONSTRAINT</strong> RESP_DEPT_EXISTS
|
|
<strong>FOREIGN KEY</strong> (DEPTNO)
|
|
<strong>REFERENCES</strong> CORPDATA.DEPARTMENT
|
|
<strong>ON DELETE RESTRICT</strong>
|
|
|
|
<strong>ALTER TABLE</strong> CORPDATA.PROJECT <strong>ADD CONSTRAINT</strong> RESP_EMP_EXISTS
|
|
<strong>FOREIGN KEY</strong> (RESPEMP)
|
|
<strong>REFERENCES</strong> CORPDATA.EMPLOYEE
|
|
<strong>ON DELETE RESTRICT</strong>
|
|
</pre>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div class="section"><p>Notice that the parent table columns are not specified in the
|
|
REFERENCES clause. The columns are not required to be specified as long as
|
|
the referenced table has a primary key or eligible unique key which can be
|
|
used as the parent key.</p>
|
|
</div>
|
|
<div class="section"><p>Every row inserted into the PROJECT table must have a value of
|
|
DEPTNO that is equal to some value of DEPTNO in the department table. (The
|
|
null value is not allowed because DEPTNO in the project table is defined as
|
|
NOT NULL.) The row must also have a value of RESPEMP that is either equal
|
|
to some value of EMPNO in the employee table or is null.</p>
|
|
</div>
|
|
<div class="section"><p>The following INSERT statement fails because there is no matching
|
|
DEPTNO value ('A01') in the DEPARTMENT table. </p>
|
|
<pre> <strong>INSERT INTO</strong> CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP)
|
|
<strong>VALUES</strong> ('AD3120', 'BENEFITS ADMIN', 'A01', '000010')</pre>
|
|
</div>
|
|
<div class="section"><p>Likewise, the following INSERT statement is unsuccessful since
|
|
there is no EMPNO value of '000011' in the EMPLOYEE table. </p>
|
|
<pre> <strong>INSERT INTO</strong> CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP)
|
|
<strong>VALUES</strong> ('AD3130', 'BILLING', 'D21', '000011')</pre>
|
|
</div>
|
|
<div class="section"><p>The following INSERT statement completes successfully because
|
|
there is a matching DEPTNO value of 'E01' in the DEPARTMENT table and a matching
|
|
EMPNO value of '000010' in the EMPLOYEE table. </p>
|
|
<pre> <strong>INSERT INTO</strong> CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP)
|
|
<strong>VALUES</strong> ('AD3120', 'BENEFITS ADMIN', 'E01', '000010')</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyinsert.htm" title="This topic shows the basic SQL statements and clauses that insert data into tables and views. Examples using these SQL statements are supplied to help you develop SQL applications.">Insert rows using the INSERT statement</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |