96 lines
6.2 KiB
HTML
96 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="concept" />
|
||
|
<meta name="DC.Title" content="Triggers and their relationship to referential integrity" />
|
||
|
<meta name="abstract" content="A physical file can have both triggers and referential constraints associated with it. The running order among trigger actions and referential constraints depends on the constraints and triggers that are associated with the file." />
|
||
|
<meta name="description" content="A physical file can have both triggers and referential constraints associated with it. The running order among trigger actions and referential constraints depends on the constraints and triggers that are associated with the file." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbaforzahftra.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="rbaforzahftrr" />
|
||
|
<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>Triggers and their relationship to referential integrity</title>
|
||
|
</head>
|
||
|
<body id="rbaforzahftrr"><a name="rbaforzahftrr"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Triggers and their relationship to referential integrity</h1>
|
||
|
<div><p>A physical file can have both triggers and referential constraints
|
||
|
associated with it. The running order among trigger actions and referential
|
||
|
constraints depends on the constraints and triggers that are associated with
|
||
|
the file.</p>
|
||
|
<p>In some cases, the system evaluates referential constraints before the
|
||
|
system calls an after trigger program. This is the case with constraints that
|
||
|
specify the RESTRICT rule.</p>
|
||
|
<p>In some cases, all statements in the trigger program, including nested
|
||
|
trigger programs, run before the constraint is applied. This is true for NO
|
||
|
ACTION, CASCADE, SET NULL, and SET DEFAULT referential constraint rules. When
|
||
|
you specify these rules, the system evaluates the file's constraints based
|
||
|
on the nested results of trigger programs. For example, an application inserts
|
||
|
employee records into an EMP file that has a constraint and trigger: </p>
|
||
|
<div class="p"> <ul><li>The referential constraint specifies that the department number for an
|
||
|
inserted employee record to the EMP file must exist in the DEPT file.</li>
|
||
|
<li>Whenever an insert to the EMP file occurs, the trigger program checks
|
||
|
if the department number exists in the DEPT file. The trigger program then
|
||
|
adds the number if it does not exist.</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<p>When the insertion to the EMP file occurs, the system calls the trigger
|
||
|
program first. If the department number does not exist in the DEPT file, the
|
||
|
trigger program inserts the new department number into the DEPT file. Then
|
||
|
the system evaluates the referential constraint. In this case, the insertion
|
||
|
is successful because the department number exists in the DEPT file.</p>
|
||
|
<p>There are some restrictions when both a trigger and referential constraint
|
||
|
are defined for the same physical file:</p>
|
||
|
<div class="p"> <ul><li>If a delete trigger associates with a physical file, that file must not
|
||
|
be a dependent file in a referential constraint with a delete rule of CASCADE.</li>
|
||
|
<li>If an update trigger associates with a physical file, no field in this
|
||
|
physical file can be a foreign key in a referential constraint with a delete
|
||
|
rule of SET NULL or SET DEFAULT.</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<p>If failure occurs during either a trigger program or referential constraint
|
||
|
validation, all trigger programs associated with the change operation roll
|
||
|
back if all the files run under the same commitment definition. The referential
|
||
|
constraints are guaranteed when all files in the trigger program and the referential
|
||
|
integrity network run under the same commitment definition. If you open the
|
||
|
files without commitment control or in a mixed scenario, undesired results
|
||
|
might occur.</p>
|
||
|
<p>You can use triggers to enforce referential constraints and business rules.
|
||
|
For example, you can use triggers to simulate the update cascade constraints
|
||
|
on a physical file. However, you would not have the same functional capabilities
|
||
|
as provided by the constraints that the system referential integrity functions
|
||
|
define. You might lose the following referential integrity advantages if you
|
||
|
define them with triggers:</p>
|
||
|
<div class="p"> <ul><li>Dependent files might contain rows that violate one or more referential
|
||
|
constraints that put the constraint into check pending but still allow file
|
||
|
operations.</li>
|
||
|
<li>The ability to inform users when the system places a constraint in check
|
||
|
pending.</li>
|
||
|
<li>When an application runs under COMMIT(*NONE) and an error occurs during
|
||
|
a cascaded delete, the database rolls back all changes.</li>
|
||
|
<li>While saving a file that is associated with a constraint, the database
|
||
|
network saves all dependent files in the same library.</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbaforzahftra.htm" title="A trigger is a set of actions that run automatically when a specified change or read operation is performed on a specified database file. On iSeries, you define a set of trigger actions in any supported high-level language.">Trigger automatic events in your database</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|