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.

ows to be deleted from the table at a commit point, unless there is a held cursor<br />

open on the table at the commit point. ON COMMIT PRESERVE ROWS causes the<br />

rows to remain past the commit point.<br />

Example: Suppose that you run the following statement in an application program:<br />

EXEC <strong>SQL</strong> DECLARE GLOBAL TEMPORARY TABLE TEMPPROD<br />

AS (SELECT * FROM BASEPROD)<br />

DEFINITION ONLY<br />

INCLUDING IDENTITY COLUMN ATTRIBUTES<br />

INCLUDING COLUMN DEFAULTS<br />

ON COMMIT PRESERVE ROWS;<br />

EXEC<br />

.<br />

<strong>SQL</strong> INSERT INTO SESSION.TEMPPROD SELECT * FROM BASEPROD;<br />

.<br />

EXEC<br />

.<br />

<strong>SQL</strong> COMMIT;<br />

.<br />

When DB2 runs the preceding DECLARE GLOBAL TEMPORARY TABLE<br />

statement, DB2 creates an empty instance of TEMPPROD. The INSERT statement<br />

populates that instance with rows from table BASEPROD. The qualifier, SESSION,<br />

must be specified in any statement that references TEMPPROD. When DB2<br />

executes the COMMIT statement, DB2 keeps all rows in TEMPPROD because<br />

TEMPPROD is defined with ON COMMIT PRESERVE ROWS. When the program<br />

ends, DB2 drops TEMPPROD.<br />

Dropping tables: DROP TABLE<br />

The following <strong>SQL</strong> statement drops the YEMP table:<br />

Working with views<br />

DROP TABLE YEMP;<br />

Use the DROP TABLE statement with care: Dropping a table is NOT equivalent<br />

to deleting all its rows. When you drop a table, you lose more than its data <strong>and</strong> its<br />

definition. You lose all synonyms, views, indexes, <strong>and</strong> referential <strong>and</strong> check<br />

constraints associated with that table. You also lose all authorities granted on the<br />

table.<br />

For more information about the DROP statement, see Chapter 5 of DB2 <strong>SQL</strong><br />

Reference.<br />

This section discusses how to use CREATE VIEW <strong>and</strong> DROP VIEW to control your<br />

views of existing tables. Although you cannot modify an existing view, you can drop<br />

it <strong>and</strong> create a new one if your base tables change in a way that affects the view.<br />

Dropping <strong>and</strong> creating views does not affect the base tables or their data.<br />

Defining a view: CREATE VIEW<br />

A view does not contain data; it is a stored definition of a set of rows <strong>and</strong> columns.<br />

A view can present any or all of the data in one or more tables <strong>and</strong>, in most cases,<br />

is interchangeable with a table. Using views can simplify writing <strong>SQL</strong> statements.<br />

Use the CREATE VIEW statement to define a view <strong>and</strong> give the view a name, just<br />

as you do for a table. The view created with the following statement shows each<br />

department manager’s name with the department data in the DSN8810.DEPT table.<br />

CREATE VIEW VDEPTM AS<br />

SELECT DEPTNO, MGRNO, LASTNAME, ADMRDEPT<br />

FROM DSN8810.DEPT, DSN8810.EMP<br />

WHERE DSN8810.EMP.EMPNO = DSN8810.DEPT.MGRNO;<br />

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

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

Saved successfully!

Ooh no, something went wrong!