100 lines
6.5 KiB
HTML
100 lines
6.5 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="Create and alter an identity column" />
|
||
|
<meta name="abstract" content="Every time that a new row is added to a table with an identity column, the identity column value in the new row is incremented (or decremented) by the system." />
|
||
|
<meta name="description" content="Every time that a new row is added to a table with an identity column, the identity column value in the new row is incremented (or decremented) by the system." />
|
||
|
<meta name="DC.subject" content="CREATE TABLE statement, identity columns, creating, examples, creating identity columns, identity column, identity column, removing, removing identity columns, identity columns" />
|
||
|
<meta name="keywords" content="CREATE TABLE statement, identity columns, creating, examples, creating identity columns, identity column, identity column, removing, removing identity columns, identity columns" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafysqltech.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafyidentsequence.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafyinsertidentity.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafyupdateidentity.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="rbafysqlpidentity" />
|
||
|
<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>Create and alter an identity column</title>
|
||
|
</head>
|
||
|
<body id="rbafysqlpidentity"><a name="rbafysqlpidentity"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Create and alter an identity column</h1>
|
||
|
<div><p>Every time that a new row is added to a table with an identity
|
||
|
column, the identity column value in the new row is incremented (or decremented)
|
||
|
by the system. </p>
|
||
|
<div class="section"><p>Only columns of type SMALLINT, INTEGER, BIGINT, DECIMAL, or NUMERIC
|
||
|
can be created as identity columns. You are allowed only one identity column
|
||
|
per table. When you are changing a table definition, only a column that you
|
||
|
are adding can be specified as an identity column; existing columns cannot.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>When you create a table, you can define a column in the table
|
||
|
to be an identity column. For example, create a table ORDERS with 3 columns
|
||
|
called ORDERNO, SHIPPED_TO, and ORDER_DATE. Define ORDERNO as an identity
|
||
|
column. </p>
|
||
|
<pre><strong>CREATE</strong> TABLE ORDERS
|
||
|
(ORDERNO <strong>SMALLINT NOT NULL
|
||
|
GENERATED ALWAYS AS IDENTITY</strong>
|
||
|
(<strong>START WITH 500
|
||
|
INCREMENT BY 1
|
||
|
CYCLE</strong>),
|
||
|
SHIPPED_TO <strong>VARCHAR</strong> (36) ,
|
||
|
ORDER_DATE <strong>DATE</strong>)</pre>
|
||
|
<p>This column is defined with starting
|
||
|
value of 500, incremented by 1 for every new row inserted, and will recycle
|
||
|
when the maximum value is reached. In this example, the maximum value for
|
||
|
the identity column is the maximum value for the data type. Because the data
|
||
|
type is defined as SMALLINT, the range of values that can be assigned to ORDERNO
|
||
|
is from 500 to 32767. When this column value reaches 32767, it will restart
|
||
|
at 500 again. If 500 is still assigned to a column, and a unique key is specified
|
||
|
on the identity column, then a duplicate key error is returned. The next insert
|
||
|
will attempt to use 501. If you do not have a unique key specified for the
|
||
|
identity column, 500 is used again, regardless of how many times it appears
|
||
|
in the table.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>For a larger range of values, specify the column to be an INTEGER
|
||
|
or even a BIGINT. If you wanted the value of the identity column to decrease,
|
||
|
specify a negative value for the INCREMENT option. It is also possible to
|
||
|
specify the exact range of numbers by using MINVALUE and MAXVALUE.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>You can modify the attributes of an existing identity column using
|
||
|
the ALTER TABLE statement. For example, if you wanted to restart the identity
|
||
|
column with a new value: </p>
|
||
|
<pre><strong>ALTER TABLE</strong> ORDER
|
||
|
<strong>ALTER COLUMN</strong> ORDERNO
|
||
|
<strong>RESTART WITH</strong> 1</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>You can also drop the identity attribute from a column: </p>
|
||
|
<pre><strong>ALTER TABLE</strong> ORDER
|
||
|
<strong>ALTER COLUMN</strong> ORDERNO
|
||
|
<strong>DROP IDENTITY</strong></pre>
|
||
|
<p>The column ORDERNO remains as a SMALLINT
|
||
|
column, but the identity attribute is dropped. The system will no longer generate
|
||
|
values for this column.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafysqltech.htm" title="Data definition language (DDL) describes the portion of SQL that allows you to create, alter, and destroy database objects. These database objects include schemas, tables, views, sequences, catalogs, indexes, and aliases.">Data definition language (DDL)</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="rbafyidentsequence.htm" title="While IDENTITY columns and sequences are similar in many ways, there are also differences.">Comparison of identity columns and sequences</a></div>
|
||
|
<div><a href="rbafyinsertidentity.htm" title="You can insert a value into an identity column or allow the system to insert a value for you.">Insert values into an identity column</a></div>
|
||
|
<div><a href="rbafyupdateidentity.htm" title="You can update the value in an identity column to a specified value or have the system generate a new value.">Update an identity column</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|