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 />

Contents<br />

About this book . . . . . . . . . . . . . . . . . . . . . . . . xix<br />

Who should read this book . . . . . . . . . . . . . . . . . . . . xix<br />

Terminology <strong>and</strong> citations . . . . . . . . . . . . . . . . . . . . . xix<br />

How to read the syntax diagrams . . . . . . . . . . . . . . . . . . xx<br />

Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . xxi<br />

How to send your comments . . . . . . . . . . . . . . . . . . . . xxi<br />

Summary of changes to this book . . . . . . . . . . . . . . . . xxiii<br />

Part 1. Using <strong>SQL</strong> queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 1<br />

Chapter 1. Retrieving data . . . . . . . . . . . . . . . . . . . . 3<br />

Result tables . . . . . . . . . . . . . . . . . . . . . . . . . . 3<br />

Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . 4<br />

Selecting columns: SELECT . . . . . . . . . . . . . . . . . . . . 5<br />

Selecting all columns: SELECT * . . . . . . . . . . . . . . . . . . 5<br />

Selecting some columns: SELECT column-name . . . . . . . . . . . . 6<br />

Selecting derived columns: SELECT expression . . . . . . . . . . . . 7<br />

Eliminating duplicate rows: DISTINCT . . . . . . . . . . . . . . . . 7<br />

Naming result columns: AS . . . . . . . . . . . . . . . . . . . . 7<br />

Selecting rows using search conditions: WHERE . . . . . . . . . . . . . 8<br />

Putting the rows in order: ORDER BY . . . . . . . . . . . . . . . . . 9<br />

Specifying the sort key . . . . . . . . . . . . . . . . . . . . . 10<br />

Referencing derived columns . . . . . . . . . . . . . . . . . . . 10<br />

Summarizing group values: GROUP BY . . . . . . . . . . . . . . . . 11<br />

Subjecting groups to conditions: HAVING . . . . . . . . . . . . . . . 12<br />

Merging lists of values: UNION . . . . . . . . . . . . . . . . . . . 13<br />

Using UNION to eliminate duplicates . . . . . . . . . . . . . . . . 13<br />

Using UNION ALL to keep duplicates . . . . . . . . . . . . . . . . 13<br />

Creating common table expressions: WITH . . . . . . . . . . . . . . 14<br />

Using WITH instead of CREATE VIEW . . . . . . . . . . . . . . . 14<br />

Using common table expressions with CREATE VIEW . . . . . . . . . 15<br />

Using common table expressions when you use INSERT . . . . . . . . 15<br />

Using recursive <strong>SQL</strong> . . . . . . . . . . . . . . . . . . . . . . 15<br />

Accessing DB2 data that is not in a table . . . . . . . . . . . . . . . 16<br />

Using 15-digit <strong>and</strong> 31-digit precision for decimal numbers . . . . . . . . . 16<br />

Finding information in the DB2 catalog . . . . . . . . . . . . . . . . 18<br />

Displaying a list of tables you can use . . . . . . . . . . . . . . . 18<br />

Displaying a list of columns in a table . . . . . . . . . . . . . . . 18<br />

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

Working with tables . . . . . . . . . . . . . . . . . . . . . . . 19<br />

Creating your own tables: CREATE TABLE . . . . . . . . . . . . . 19<br />

Working with temporary tables . . . . . . . . . . . . . . . . . . 21<br />

Dropping tables: DROP TABLE . . . . . . . . . . . . . . . . . . 25<br />

Working with views . . . . . . . . . . . . . . . . . . . . . . . 25<br />

Defining a view: CREATE VIEW . . . . . . . . . . . . . . . . . 25<br />

Changing data through a view . . . . . . . . . . . . . . . . . . 26<br />

Dropping views: DROP VIEW . . . . . . . . . . . . . . . . . . 27<br />

Modifying DB2 data . . . . . . . . . . . . . . . . . . . . . . . 27<br />

Inserting rows: INSERT . . . . . . . . . . . . . . . . . . . . . 27<br />

Selecting values as you insert: SELECT from INSERT . . . . . . . . . 31<br />

Updating current values: UPDATE . . . . . . . . . . . . . . . . . 36<br />

© Copyright IBM Corp. 1983, 2004 iii

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

Saved successfully!

Ooh no, something went wrong!