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

76 lines
4.6 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="Add and use check constraints" />
<meta name="abstract" content="A check constraint assures the validity of data during insert and update operations by limiting the allowable values in a column or group of columns." />
<meta name="description" content="A check constraint assures the validity of data during insert and update operations by limiting the allowable values in a column or group of columns." />
<meta name="DC.subject" content="examples, check constraints, CREATE TABLE statement, ALTER TABLE statement, constraint, check, adding, using" />
<meta name="keywords" content="examples, check constraints, CREATE TABLE statement, ALTER TABLE statement, constraint, check, adding, using" />
<meta name="DC.Relation" scheme="URI" content="rbafymovnow.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstatabl.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmsthctabl.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="rbafycheckcon" />
<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>Add and use check constraints</title>
</head>
<body id="rbafycheckcon"><a name="rbafycheckcon"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Add and use check constraints</h1>
<div><p>A <em>check constraint</em> assures the validity of data during insert
and update operations by limiting the allowable values in a column
or group of columns.</p>
<div class="section"><p>Use the SQL CREATE TABLE and ALTER TABLE statements to add or
drop check constraints.</p>
</div>
<div class="section"><p>In this example, the following statement creates a table with
three columns and a check constraint over COL2 that limits the values allowed
in that column to positive integers: </p>
<pre><strong>CREATE TABLE</strong> T1 (COL1 <strong>INT</strong>, COL2 <strong>INT CHECK</strong> (COL2&gt;0), COL3 <strong>INT</strong>)</pre>
</div>
<div class="section"><p>Given this table, the following statement:</p>
<pre><strong>INSERT INTO</strong> T1 <strong>VALUES</strong> (-1, -1, -1)</pre>
<p>fails
because the value to be inserted into COL2 does not meet the check constraint;
that is, -1 is not greater than 0.</p>
</div>
<div class="section"><p>The following statement is successful:</p>
</div>
<div class="example"> <pre><strong>INSERT INTO</strong> T1 <strong>VALUES</strong> (1, 1, 1)</pre>
</div>
<div class="section"><p>Once that row is inserted, the following statement fails: </p>
<pre><strong>ALTER TABLE</strong> T1 <strong>ADD CONSTRAINT</strong> C1 <strong>CHECK</strong> (COL1=1 <strong>AND</strong> COL1&lt;COL2)</pre>
</div>
<div class="section"><p>This ALTER TABLE statement attempts to add a second check constraint
that limits the value allowed in COL1 to 1 and also effectively rules that
values in COL2 be greater than 1. This constraint is not allowed because the
second part of the constraint is not met by the existing data (the value of
'1' in COL2 is not less than the value of '1' in COL1).</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafymovnow.htm" title="DB2 UDB for iSeries supports unique, referential, and check constraints.">Constraints</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmstatabl.htm">ALTER TABLE statement</a></div>
<div><a href="../db2/rbafzmsthctabl.htm">CREATE TABLE statement</a></div>
</div>
</div>
</body>
</html>