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

129 lines
7.9 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="Delete from tables with referential constraints" />
<meta name="abstract" content="If a table has a primary key but no dependents, the DELETE statement operates as it does without referential constraints. The same is true if a table has only foreign keys, but no primary key. If a table has a primary key and dependent tables, DELETE deletes or updates rows according to the delete rules specified." />
<meta name="description" content="If a table has a primary key but no dependents, the DELETE statement operates as it does without referential constraints. The same is true if a table has only foreign keys, but no primary key. If a table has a primary key and dependent tables, DELETE deletes or updates rows according to the delete rules specified." />
<meta name="DC.subject" content="DELETE statement, deleting from tables, constraint, referential, delete rules" />
<meta name="keywords" content="DELETE statement, deleting from tables, constraint, referential, delete rules" />
<meta name="DC.Relation" scheme="URI" content="rbafydelete.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafydeleterules.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="rbafyrfdeleting" />
<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>Delete from tables with referential constraints</title>
</head>
<body id="rbafyrfdeleting"><a name="rbafyrfdeleting"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Delete from tables with referential constraints</h1>
<div><p>If a table has a primary key but no dependents, the DELETE statement
operates as it does without referential constraints. The same is true if a
table has only foreign keys, but no primary key. If a table has a primary
key and dependent tables, DELETE deletes or updates rows according
to the delete rules specified.</p>
<div class="section"><p>All delete rules of all affected relationships must be satisfied
in order for the delete operation to succeed. If a referential constraint
is violated, the DELETE fails. </p>
</div>
<div class="section"><p>The action to be taken on dependent tables when a DELETE is performed
on a parent table depends on the delete rule specified for the referential
constraint. If no delete rule was defined, the DELETE NO ACTION rule is used.
</p>
<dl><dt class="dlterm">DELETE NO ACTION </dt>
<dd>Specifies that the row in the parent table can be deleted if no other
row depends on it. If a dependent row exists in the relationship, the DELETE
fails. The check for dependent rows is performed at the end of the statement.</dd>
<dt class="dlterm">DELETE RESTRICT </dt>
<dd>Specifies that the row in the parent table can be deleted if no other
row depends on it. If a dependent row exists in the relationship, the DELETE
fails. The check for dependent rows is performed immediately. <p>For example,
you cannot delete a department from the department table if it is still responsible
for some project that is described by a dependent row in the project table.</p>
</dd>
<dt class="dlterm">DELETE CASCADE </dt>
<dd>Specifies that first the designated rows in the parent table are deleted.
Then, the dependent rows are deleted. <p>For example, you can delete a department
by deleting its row in the department table. Deleting the row from the department
table also deletes: </p>
<ul><li>The rows for all departments that report to it</li>
<li>All departments that report to those departments and so forth.</li>
</ul>
</dd>
<dt class="dlterm">DELETE SET NULL</dt>
<dd>Specifies that each nullable column of the foreign key in each dependent
row is set to its default value. This means that the column is only set to
its default value if it is a member of a foreign key that references the row
being deleted. Only the dependent rows that are immediate descendents are
affected.</dd>
<dt class="dlterm">DELETE SET DEFAULT </dt>
<dd>Specifies that each column of the foreign key in each dependent row is
set to its default value. This means that the column is only set to its default
value if it is a member of a foreign key that references the row being deleted.
Only the dependent rows that are immediate descendants are affected. <p>For
example, you can delete an employee from the employee table (EMPLOYEE) even
if the employee manages some department. In that case, the value of MGRNO
for each employee who reported to the manager is set to blanks in the department
table (DEPARTMENT). If some other default value was specified on the create
of the table, that value is used.</p>
<p>This is due to the REPORTS_TO_EXISTS
constraint defined for the department table.</p>
</dd>
</dl>
</div>
<div class="section"><p>If a descendent table has a delete rule of RESTRICT or NO ACTION
and a row is found such that a descendant row cannot be deleted, the entire
DELETE fails.</p>
</div>
<div class="section"><p>When running this statement with a program, the number of rows
deleted is returned in SQLERRD(3) in the SQLCA. This number includes only
the number of rows deleted in the table specified in the DELETE statement.
It does not include those rows deleted according to the CASCADE rule. SQLERRD(5)
in the SQLCA contains the number of rows that were affected by referential
constraints in all tables. The SQLERRD(3) value is also available from the
ROW_COUNT item in the GET DIAGNOSTICS statement. The SQLERRD(5) value is
available from the DB2_ROW_COUNT_SECONDARY item.</p>
</div>
<div class="section"><p>The subtle difference between RESTRICT and NO ACTION rules is
easiest seen when looking at the interaction of triggers and referential constraints.
Triggers can be defined to fire either before or after an operation (a DELETE
statement, in this case). A <em>before trigger</em> fires before the DELETE
is performed and therefore before any checking of constraints. An <em>after
trigger</em> is fired after the DELETE is performed, and after a constraint
rule of RESTRICT (where checking is performed immediately), but before a constraint
rule of NO ACTION (where checking is performed at the end of the statement).
The triggers and rules occur in the following order: </p>
</div>
<div class="section"> <ol><li>A <em>before trigger</em> is fired before the DELETE and before a constraint
rule of RESTRICT or NO ACTION.</li>
<li>An <em>after trigger</em> is fired after a constraint rule of RESTRICT,
but before a NO ACTION rule.</li>
</ol>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rbafydeleterules.htm">Example: DELETE cascade rule</a></strong><br />
</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafydelete.htm" title="To remove rows from a table, use the DELETE statement.">Remove rows from a table using the DELETE statement</a></div>
</div>
</div>
</body>
</html>