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

|<br />

|<br />

|<br />

|<br />

|<br />

|<br />

Part 1. Using <strong>SQL</strong> queries<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 />

Identifying defaults . . . . . . . . . . . . . . . . . . . . . 19<br />

Creating work tables . . . . . . . . . . . . . . . . . . . . . 20<br />

Creating a new department table . . . . . . . . . . . . . . . . 20<br />

Creating a new employee table . . . . . . . . . . . . . . . . . 21<br />

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

Working with created temporary tables . . . . . . . . . . . . . . 22<br />

Working with declared temporary tables . . . . . . . . . . . . . . 23<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 />

Inserting a single row . . . . . . . . . . . . . . . . . . . . 28<br />

Inserting rows into a table from another table . . . . . . . . . . . . 29<br />

Other ways to insert data . . . . . . . . . . . . . . . . . . . 29<br />

Inserting data into a ROWID column . . . . . . . . . . . . . . . 30<br />

Inserting data into an identity column . . . . . . . . . . . . . . . 30<br />

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

Result table of the INSERT operation . . . . . . . . . . . . . . 32<br />

© Copyright IBM Corp. 1983, 2004 1

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

Saved successfully!

Ooh no, something went wrong!