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

159 lines
11 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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="Insert rows using the INSERT statement" />
<meta name="abstract" content="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." />
<meta name="description" content="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." />
<meta name="DC.subject" content="statements, INSERT, data manipulation statement (DML), INSERT statement, description, VALUES clause, blocked, using, examples, inserting, row to table, INTO clause, clause, INTO, inserting constant, NULL value, INSERT INTO clause, value, inserting NULL, inserting host variable, inserting special register, inserting expression, inserting subquery, inserting DEFAULT, default value, commitment control, reusing deleted rows, row, reusing deleted with INSERT" />
<meta name="keywords" content="statements, INSERT, data manipulation statement (DML), INSERT statement, description, VALUES clause, blocked, using, examples, inserting, row to table, INTO clause, clause, INTO, inserting constant, NULL value, INSERT INTO clause, value, inserting NULL, inserting host variable, inserting special register, inserting expression, inserting subquery, inserting DEFAULT, default value, commitment control, reusing deleted rows, row, reusing deleted with INSERT" />
<meta name="DC.Relation" scheme="URI" content="rbafydml.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyvalues.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafymultrow.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyblkins.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyrfinserting.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyinsertidentity.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstbackup.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="rbafyinsert" />
<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 rows using the INSERT statement</title>
</head>
<body id="rbafyinsert"><a name="rbafyinsert"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Insert rows using the INSERT statement</h1>
<div><p>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.</p>
<p>You can use the INSERT statement to add new rows to a table or view in
one of the following ways: </p>
<ul><li>Specifying values in the INSERT statement for columns to be added.</li>
<li>Including a select-statement in the INSERT statement to tell SQL what
data for the new row is contained in another table or view. </li>
<li>Specifying the blocked form of the INSERT statement to add multiple rows.</li>
</ul>
<p>For every row you insert, you must supply a value for each column defined
with the NOT NULL attribute if that column does not have a default value.
The INSERT statement for adding a row to a table or view may look like this:</p>
<pre> <strong>INSERT INTO</strong> table-name
(column1, column2, ... )
<strong>VALUES</strong> (value-for-column1, value-for-column2, ... )</pre>
<p>The INTO clause names the columns for which you specify values. The VALUES
clause specifies a value for each column named in the INTO clause. The value
you specify can be:</p>
<ul><li>A <strong>constant.</strong> Inserts the value provided in the VALUES clause.</li>
<li>A <strong>null value.</strong> Inserts the null value, using the keyword NULL. The
column must be defined as capable of containing a null value or an error occurs.</li>
<li>A <strong>host variable.</strong> Inserts the contents of a host variable.</li>
<li>A <strong>special register.</strong> Inserts a special register value; for example,
USER.</li>
<li>An <strong>expression.</strong> Inserts the value that results from an expression.</li>
<li>A <strong>scalar fullselect</strong> inserts the value that is the
result of running the select statement.</li>
<li>The <strong>DEFAULT</strong> keyword. Inserts the default value of the column. The
column must have a default value defined for it or allow the NULL value, or
an error occurs.</li>
</ul>
<p>You must provide a value in the VALUES clause for each column named in
an INSERT statement's column list. The column name list can be omitted if
all columns in the table have a value provided in the VALUES clause. If a
column has a default value, the keyword DEFAULT may be used as a value in
the VALUES clause. This causes the default value for the column to be placed
in the column.</p>
<p>It is a good idea to name all columns into which you are inserting values
because:</p>
<ul><li>Your INSERT statement is more descriptive.</li>
<li>You can verify that you are providing the values in the proper order based
on the column names.</li>
<li>You have better data independence. The order in which the columns are
defined in the table does not affect your INSERT statement.</li>
</ul>
<p>If the column is defined to allow null values or to have a default, you
do not need to name it in the column name list or specify a value for it.
The default value is used. If the column is defined to have a default value,
the default value is placed in the column. If DEFAULT was specified for the
column definition without an explicit default value, SQL places the default
value for that data type in the column. If the column does not have a default
value defined for it, but is defined to allow the null value (NOT NULL was
not specified in the column definition), SQL places the null value in the
column.</p>
<ul><li>For numeric columns, the default value is 0.</li>
<li>For fixed length character or graphic columns, the default is blanks.</li>
<li><img src="./delta.gif" alt="Start of change" />For fixed length binary columns, the default is hexadecimal
zeros.<img src="./deltaend.gif" alt="End of change" /></li>
<li><img src="./delta.gif" alt="Start of change" />For varying length character, graphic, or binary columns
and for LOB columns, the default is a zero length string.<img src="./deltaend.gif" alt="End of change" /></li>
<li>For date, time, and timestamp columns, the default value is the current
date, time, or timestamp. When inserting a block of records, the default date/time
value is extracted from the system when the block is written. This means that
the column will be assigned the same default value for each row in the block.</li>
<li>For DataLink columns, the default value corresponds to DLVALUE('','URL','').</li>
<li>For distinct-type columns, the default value is the default value of the
corresponding source type. </li>
<li>For ROWID columns or columns that are defined AS IDENTITY, the database
manager generates a default value.</li>
</ul>
<p>When your program attempts to insert a row that duplicates another row
already in the table, an error might occur. Multiple null values may or may
not be considered duplicate values, depending on the option used when the
index was created.</p>
<ul><li>If the table has a primary key, unique key, or unique index, the row is
not inserted. Instead, SQL returns an SQLCODE of -803.</li>
<li>If the table does not have a primary key, unique key, or unique index,
the row can be inserted without error.</li>
</ul>
<p>If SQL finds an error while running the INSERT statement, it stops inserting
data. If you specify COMMIT(*ALL), COMMIT(*CS), COMMIT(*CHG), or COMMIT(*RR),
no rows are inserted. Rows already inserted by this statement, in the case
of INSERT with a select-statement or blocked insert, are deleted. If you specify
COMMIT(*NONE), any rows already inserted are <em>not</em> deleted.</p>
<p>A table created by SQL is created with the Reuse Deleted Records parameter
of *YES. This allows the database manager to reuse any rows in the table that
were marked as deleted. The CHGPF command can be used to change the attribute
to *NO. This causes INSERT to always add rows to the end of the table.</p>
<p>The order in which rows are inserted does not guarantee the order in which
they will be retrieved.</p>
<p>If the row is inserted without error, the SQLERRD(3) field of the SQLCA
has a value of 1.</p>
<div class="note"><span class="notetitle">Note:</span> For blocked INSERT or for INSERT with select-statement, more than one
row can be inserted. The number of rows inserted is reflected in SQLERRD(3)
in the SQLCA. It is also available from the ROW_COUNT diagnostics item in
the GET DIAGNOSTICS statement.</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rbafyvalues.htm">Insert rows using the VALUES keyword</a></strong><br />
You can use the VALUES keyword to insert a single row or multiple rows into a table.</li>
<li class="ulchildlink"><strong><a href="rbafymultrow.htm">Insert rows into a table using a select-statement</a></strong><br />
You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement.</li>
<li class="ulchildlink"><strong><a href="rbafyblkins.htm">Insert multiple rows in a table with the blocked INSERT statement</a></strong><br />
A blocked INSERT statement can be used to insert multiple rows into a table with a single statement.</li>
<li class="ulchildlink"><strong><a href="rbafyrfinserting.htm">Insert data into tables with referential constraints</a></strong><br />
There are some important things to remember when inserting data into tables with referential constraints.</li>
<li class="ulchildlink"><strong><a href="rbafyinsertidentity.htm">Insert values into an identity column</a></strong><br />
You can insert a value into an identity column or allow the system to insert a value for you.</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafydml.htm" title="Data manipulation language (DML) describes the portion of SQL that allows you to manipulate or control your data.">Data manipulation language</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmstbackup.htm">INSERT statement</a></div>
</div>
</div>
</body>
</html>