151 lines
9.2 KiB
HTML
151 lines
9.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="copyright" content="(C) Copyright IBM Corporation 2005" />
|
|||
|
<meta name="DC.rights.owner" content="(C) Copyright IBM Corporation 2005" />
|
|||
|
<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="task" />
|
|||
|
<meta name="DC.Title" content="Create and use a table" />
|
|||
|
<meta name="DC.Relation" scheme="URI" content="rzatcgetstartsql.htm" />
|
|||
|
<meta name="DC.Format" content="XHTML" />
|
|||
|
<meta name="DC.Identifier" content="rzatccrttblsql" />
|
|||
|
<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 use a table</title>
|
|||
|
</head>
|
|||
|
<body id="rzatccrttblsql"><a name="rzatccrttblsql"><!-- --></a>
|
|||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|||
|
<h1 class="topictitle1">Create and use a table</h1>
|
|||
|
<div><div class="section">You can create a table by using the SQL CREATE TABLE statement. The
|
|||
|
CREATE TABLE statement allows you to create a table, define the physical attributes
|
|||
|
of the columns in the table, and define constraints to restrict the values
|
|||
|
that are allowed in the table.<p>When creating a table, you need to understand
|
|||
|
the concepts of null value and default value. A null value indicates the absence
|
|||
|
of a column value for a row. It is not the same as a value of zero or all
|
|||
|
blanks. It means "unknown." It is not equal to any value, not even to other
|
|||
|
null values. If a column does not allow the null value, a value must be assigned
|
|||
|
to the column, either a default value or a user supplied value.</p>
|
|||
|
<p>A default
|
|||
|
value is assigned to a column when a row is added to a table and no value
|
|||
|
is specified for that column. If a specific default value was not defined
|
|||
|
for a column, the system default value is used. For more information about
|
|||
|
the default values used by INSERT, see the <a href="../db2/rbafzmstbackup.htm">SQL
|
|||
|
Reference</a>.</p>
|
|||
|
<p>You are going to create a table to maintain information
|
|||
|
about the current inventory of a business. The table have information about
|
|||
|
the items kept in the inventory, their cost, quantity currently on hand, the
|
|||
|
last order date, and the number last ordered. The item number is a required
|
|||
|
value. It cannot be null. The item name, quantity on hand, and order quantity
|
|||
|
have user supplied default values. The last order date and quantity ordered
|
|||
|
allow the null value.</p>
|
|||
|
<p>You also create a second table. This table contain
|
|||
|
information about suppliers of your inventory items, which items they supply,
|
|||
|
and the cost of the item from that supplier.</p>
|
|||
|
</div>
|
|||
|
<ol><li class="stepexpand"><span>Create the first table named INVENTORY_LIST:</span><ol type="a"><li class="substepexpand"><span>On the Enter SQL Statements display, type <tt>CREATE TABLE</tt> and
|
|||
|
press F4 (Prompt). The following display is shown (with the input areas not
|
|||
|
yet filled in):</span> <div class="p"><pre class="screen"> Specify CREATE TABLE Statement
|
|||
|
|
|||
|
Type information, press Enter.
|
|||
|
|
|||
|
Table . . . . . . . . . <strong>INVENTORY_LIST</strong>______ Name
|
|||
|
Collection . . . . . . <strong>SAMPLECOLL</strong>__ Name, F4 for list
|
|||
|
|
|||
|
Nulls: 1=NULL, 2=NOT NULL, 3=NOT NULL WITH DEFAULT
|
|||
|
|
|||
|
Column FOR Column Type Length Scale Nulls
|
|||
|
<strong>ITEM_NUMBER</strong>_______ ____________ <strong>CHAR</strong>___________ <strong>6</strong>____ __ <strong>2</strong>
|
|||
|
<strong>ITEM_NAME</strong>_________ ____________ <strong>VARCHAR</strong>________ <strong>20</strong>___ __ <strong>3</strong>
|
|||
|
<strong>UNIT_COST</strong>_________ ____________ <strong>DECIMAL</strong>________ <strong>8</strong>____ <strong>2</strong>_ <strong>3</strong>
|
|||
|
<strong>QUANTITY_ON_HAND</strong>__ ____________ <strong>SMALLINT</strong>_______ _____ __ <strong>1</strong>
|
|||
|
<strong>LAST_ORDER_DATE</strong>___ ____________ <strong>DATE</strong>___________ _____ __ <strong>1</strong>
|
|||
|
<strong>ORDER_QUANTITY</strong>____ ____________ <strong>SMALLINT</strong>_______ _____ __ <strong>1</strong>
|
|||
|
__________________ ____________ _______________ _____ __ <strong>3</strong>
|
|||
|
Bottom
|
|||
|
Table CONSTRAINT . . . . . . . . . . . . . <strong>N</strong> Y=Yes, N=No
|
|||
|
Distributed Table . . . . . . . . . . . . <strong>N</strong> Y=Yes, N=No
|
|||
|
|
|||
|
F3=Exit F4=Prompt F5=Refresh F6=Insert line F10=Copy line
|
|||
|
F11=Display more attributes F12=Cancel F14=Delete line F24=More keys</pre>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li class="substepexpand"><span>Type the table name INVENTORY_LIST and schema name SAMPLECOLL
|
|||
|
at the <span class="uicontrol">Table</span> and <span class="uicontrol">Collection</span> prompts,
|
|||
|
as shown.</span></li>
|
|||
|
<li class="substepexpand"><span>Each column you want to define for the table is represented
|
|||
|
by an entry in the list on the lower part of the display. For each column,
|
|||
|
type the name of the column, the data type of the column, its length and scale,
|
|||
|
and the null attribute.</span></li>
|
|||
|
<li class="substepexpand"><span>Press F11 to see more attributes that can be specified for the
|
|||
|
columns. This is where a default value can be specified. </span> <div class="p"><pre class="screen"> Specify CREATE TABLE Statement
|
|||
|
|
|||
|
Type information, press Enter.
|
|||
|
|
|||
|
Table . . . . . . . . . INVENTORY_LIST______ Name
|
|||
|
Collection . . . . . . SAMPLECOLL__ Name, F4 for list
|
|||
|
|
|||
|
Data: 1=BIT, 2=SBCS, 3=MIXED, 4=CCSID
|
|||
|
|
|||
|
Column Data Allocate CCSID CONSTRAINT Default
|
|||
|
ITEM NUMBER_______ _ _____ _____ N __________________
|
|||
|
ITEM NAME_________ _ _____ _____ N <span>'***UNKNOWN***'</span>___
|
|||
|
UNIT_COST_________ _ _____ _____ N __________________
|
|||
|
QUANTITY_ON_HAND__ _ _____ _____ N <span>NULL</span>______________
|
|||
|
LAST_ORDER_DATE___ _ _____ _____ N __________________
|
|||
|
ORDER_QUANTITY____ _ _____ _____ N <span>20</span>________________
|
|||
|
__________________ _ _____ _____ _ __________________
|
|||
|
Bottom
|
|||
|
Table CONSTRAINT . . . . . . . . . . . . . N Y=Yes, N=No
|
|||
|
Distributed Table . . . . . . . . . . . . N Y=Yes, N=No
|
|||
|
|
|||
|
F3=Exit F4=Prompt F5=Refresh F6=Insert line F10=Copy line
|
|||
|
F11=Display more attributes F12=Cancel F14=Delete line F24=More keys</pre>
|
|||
|
</div>
|
|||
|
<div class="note"><span class="notetitle">Note:</span> Another way of entering column definitions is to press
|
|||
|
F4 (Prompt) with your cursor on one of the column entries in the list. A display
|
|||
|
that shows all of the attributes for defining a single column appears.</div>
|
|||
|
</li>
|
|||
|
<li class="substepexpand"><span>When all the values have been entered, press Enter to create
|
|||
|
the table. The Enter SQL Statements display is shown again with
|
|||
|
a message indicating that the table has been created.</span></li>
|
|||
|
</ol>
|
|||
|
<div class="note"><span class="notetitle">Note:</span> You can type this CREATE TABLE statement on the Enter SQL Statements
|
|||
|
display as follows: <pre><strong>CREATE TABLE</strong> SAMPLECOLL.INVENTORY_LIST
|
|||
|
(ITEM_NUMBER <strong>CHAR</strong>(6) <strong>NOT NULL,</strong>
|
|||
|
ITEM_NAME <strong>VARCHAR</strong>(20) <strong>NOT NULL WITH DEFAULT</strong> ’***UNKNOWN***’,
|
|||
|
UNIT_COST <strong>DECIMAL</strong>(8,2) <strong>NOT NULL WITH DEFAULT,</strong>
|
|||
|
QUANTITY_ON_HAND <strong>SMALLINT DEFAULT NULL,</strong>
|
|||
|
LAST_ORDER_DATE <strong>DATE,</strong>
|
|||
|
ORDER_QUANTITY <strong>SMALLINT DEFAULT</strong> 20)</pre>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li class="stepexpand"><span>Create a second table named SUPPLIERS. There are two methods you
|
|||
|
can use:</span><ol type="a"><li><span>Type the following command directly on the Enter SQL Statements
|
|||
|
display.</span></li>
|
|||
|
<li><span>Press F4 (Prompt) to use the interactive SQL displays to create
|
|||
|
the definition.</span></li>
|
|||
|
</ol>
|
|||
|
<pre><strong>CREATE TABLE</strong> SAMPLECOLL.SUPPLIERS
|
|||
|
(SUPPLIER_NUMBER <strong>CHAR</strong>(4)<strong>NOT NULL,</strong>
|
|||
|
ITEM_NUMBER <strong>CHAR</strong>(6) <strong>NOT NULL,</strong>
|
|||
|
SUPPLIER_COST <strong>DECIMAL</strong>(8,2))</pre>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
</div>
|
|||
|
<div>
|
|||
|
<div class="familylinks">
|
|||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzatcgetstartsql.htm" title="This topic describes how to create and work with schemas, tables, and views using SQL statements in interactive SQL.">Get started with SQL</a></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</body>
|
|||
|
</html>
|