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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

For information about using the INSERT statement, see “Inserting rows: INSERT”<br />

on page 27.<br />

Creating a new employee table<br />

You can use the following statements to create a new employee table called YEMP.<br />

CREATE TABLE YEMP<br />

(EMPNO CHAR(6) PRIMARY KEY NOT NULL,<br />

FIRSTNME VARCHAR(12) NOT NULL,<br />

MIDINIT CHAR(1) NOT NULL,<br />

LASTNAME VARCHAR(15) NOT NULL,<br />

WORKDEPT CHAR(3) REFERENCES YDEPT<br />

ON DELETE SET NULL,<br />

PHONENO CHAR(4) UNIQUE NOT NULL,<br />

HIREDATE DATE ,<br />

JOB CHAR(8) ,<br />

EDLEVEL SMALLINT ,<br />

SEX CHAR(1) ,<br />

BIRTHDATE DATE ,<br />

SALARY DECIMAL(9, 2) ,<br />

BONUS DECIMAL(9, 2) ,<br />

COMM DECIMAL(9, 2) );<br />

This statement also creates a referential constraint between the foreign key in<br />

YEMP (WORKDEPT) <strong>and</strong> the primary key in YDEPT (DEPTNO). It also restricts all<br />

phone numbers to unique numbers.<br />

If you want to change a table definition after you create it, use the statement ALTER<br />

TABLE. If you want to change a table name after you create it, use the statement<br />

RENAME TABLE.<br />

You can change a table definition by using the ALTER TABLE statement only in<br />

certain ways. For example, you can add <strong>and</strong> drop constraints on columns in a table.<br />

You can also change the data type of a column within character data types, within<br />

numeric data types, <strong>and</strong> within graphic data types. You can add a column to a<br />

table. However, you cannot drop a column from a table.<br />

For more information about changing a table definition by using ALTER TABLE, see<br />

Part 2 (Volume 1) of DB2 Administration <strong>Guide</strong>. For other details about the ALTER<br />

TABLE <strong>and</strong> RENAME TABLE statements, see Chapter 5 of DB2 <strong>SQL</strong> Reference.<br />

Working with temporary tables<br />

When you need a table only for the duration of an application process, you can<br />

create a temporary table. There are two kinds of temporary tables:<br />

v Created temporary tables, which you define using a CREATE GLOBAL<br />

TEMPORARY TABLE statement<br />

v Declared temporary tables, which you define using a DECLARE GLOBAL<br />

TEMPORARY TABLE statement<br />

<strong>SQL</strong> statements that use temporary tables can run faster because of the following<br />

reasons:<br />

v DB2 does no logging (for created temporary tables) or limited logging (for<br />

declared temporary tables).<br />

v DB2 does no locking (for created temporary tables) or limited locking (for<br />

declared temporary tables).<br />

Temporary tables are especially useful when you need to sort or query intermediate<br />

result tables that contain a large number of rows, but you want to store only a small<br />

subset of those rows permanently.<br />

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

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

Saved successfully!

Ooh no, something went wrong!