10.12.2012 Views

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

|<br />

Chapter 2. Working with tables <strong>and</strong> modifying data<br />

Working with tables<br />

This chapter discusses these topics:<br />

v Creating your own tables: CREATE TABLE<br />

v “Working with temporary tables” on page 21<br />

v “Dropping tables: DROP TABLE” on page 25<br />

v “Defining a view: CREATE VIEW” on page 25<br />

v “Changing data through a view” on page 26<br />

v “Dropping views: DROP VIEW” on page 27<br />

v “Inserting rows: INSERT” on page 27<br />

v “Selecting values as you insert: SELECT from INSERT” on page 31<br />

v “Updating current values: UPDATE” on page 36<br />

v “Deleting rows: DELETE” on page 37<br />

See DB2 <strong>SQL</strong> Reference for more information about working with tables <strong>and</strong> data.<br />

This section discusses how to work with tables. As you work with tables, you might<br />

need to create new tables, copy existing tables, add columns, add or drop<br />

referential <strong>and</strong> check constraints, drop the tables you are working with, or make any<br />

number of changes.<br />

Creating your own tables: CREATE TABLE<br />

Use the CREATE TABLE statement to create a table. The following <strong>SQL</strong> statement<br />

creates a table named PRODUCT:<br />

CREATE TABLE PRODUCT<br />

(SERIAL CHAR(8) NOT NULL,<br />

DESCRIPTION VARCHAR(60) DEFAULT,<br />

MFGCOST DECIMAL(8,2),<br />

MFGDEPT CHAR(3),<br />

MARKUP SMALLINT,<br />

SALESDEPT CHAR(3),<br />

CURDATE DATE DEFAULT);<br />

The preceding CREATE statement has the following elements:<br />

v CREATE TABLE, which names the table PRODUCT.<br />

v A list of the columns that make up the table. For each column, specify the<br />

following information:<br />

– The column’s name (for example, SERIAL).<br />

– The data type <strong>and</strong> length attribute (for example, CHAR(8)). For more<br />

information about data types, see “Data types” on page 4.<br />

– Optionally, a default value. See “Identifying defaults.”<br />

– Optionally, a referential constraint or check constraint. See “Using referential<br />

constraints” on page 253 <strong>and</strong> “Using check constraints” on page 251.<br />

You must separate each column description from the next with a comma, <strong>and</strong><br />

enclose the entire list of column descriptions in parentheses.<br />

Identifying defaults<br />

If you want to constrain the input or identify the default of a column, you can use<br />

the following values:<br />

v NOT NULL, when the column cannot contain null values.<br />

© Copyright IBM Corp. 1983, 2004 19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!