DB2 9 Fundamentals Exam 730 Prep, Part 4:: Working With DB2 Data
DB2 9 Fundamentals Exam 730 Prep, Part 4:: Working With DB2 Data
24 Jul 2006
This tutorial introduces you to Structured Query Language (SQL) and helps to give
you a good understanding of how DB2® 9 uses SQL to manipulate data in a
relational database. This tutorial is the fourth in a series of seven tutorials that you
can use to help prepare for DB2 9 Fundamentals Certification (Exam 730).
• An introduction to SQL
Objectives
After completing this tutorial, you should be able to:
System Requirements
If you haven't already done so, you can download the free trial version of IBM DB2 9
to work along with this tutorial. Installing DB2 helps you understand many of the
concepts that are tested on the DB2 9 Fundamentals Certification exam. DB2
installation is not covered in this tutorial, but the process is documented in the DB2
Information Center.
Most SQL statements contain one or more of the following language elements:
individual scalar values that can be of different types and that can be
used to pass values into the procedure, receive return values from the
procedure, or both. User-defined procedures are registered to a
database in the system catalog (accessible through the
SYSCAT.ROUTINES catalog view) using the CREATE PROCEDURE
statement.
• An expression specifies a value. There are string expressions, arithmetic
expressions, and case expressions, which can be used to specify a
particular result based on the evaluation of one or more conditions.
• A predicate specifies a condition that is true, false, or unknown about a
given row or group. There are subtypes:
• A basic predicate compares two values (for example, x > y).
• The BETWEEN predicate compares a value with a range of values.
• The EXISTS predicate tests for the existence of certain rows.
• The IN predicate compares one or more values with a collection of
values.
• The LIKE predicate searches for strings that have a certain pattern.
• The NULL predicate tests for null values.
To restrict the number of rows in a result set, use the FETCH FIRST clause. For
example:
Retrieve specific columns from a table by specifying a select list of column names
separated by commas. For example:
Use the DISTINCT clause to eliminate duplicate rows in a result set. For example:
Without the AS clause, the derived column would have been named 2, indicating that
it is the second column in the result set.
• Find the names of staff members whose salaries are greater than
$20,000:
In this example, the percent sign (%) is a wild card character that
represents a string of zero or more characters.
A subquery is a SELECT statement that appears within the WHERE clause of a main
query and feeds its result set to that WHERE clause. For example:
A correlation name is defined in the FROM clause of a query and can serve as a
convenient short name for a table. Correlation names also eliminate ambiguous
references to identical column names from different tables. For example:
Sort the result set in descending order by specifying DESC in the ORDER BY clause:
The simplest join is one in which there are no specified conditions. For example:
This statement returns all combinations of rows from the ORG table and the STAFF
table. The first three columns come from the ORG tables, and the last four columns
come from the STAFF table. Such a result set (the cross product of the two tables) is
not very useful. What is needed is a join condition to refine the result set. For
example, here is a query that is designed to identify staff members who are
managers:
The statement you looked at in the last section is an example of an inner join. Inner
joins return only rows from the cross product that meet the join condition. If a row
exists in one table but not the other, it is not included in the result set. To explicitly
specify an inner join, rewrite the previous query with an INNER JOIN operator in the
FROM clause:
...
FROM org INNER JOIN staff
ON manager = id
...
The keyword ON specifies the join conditions for the tables being joined. DeptNumb
and DeptName are columns in the ORG table, and Manager_ID and Manager are
based on columns (ID and Name) in the STAFF table. The result set for the inner
join consists of rows that have matching values for the Manager and ID columns in
the left table (ORG) and the right table (STAFF), respectively. (When you perform a
join on two tables, you arbitrarily designate one table to be the left table and the
other to be the right.)
Outer joins return rows that are generated by an inner join operation, plus rows that
would not be returned by the inner join operation. There are three types of outer
joins:
• A left outer join includes the inner join plus the rows from the left table that
are not returned by the inner join. This type of join uses the LEFT OUTER
JOIN (or LEFT JOIN) operator in the FROM clause.
• A right outer join includes the inner join plus the rows from the right table
that are not returned by the inner join. This type of join uses the RIGHT
OUTER JOIN (or RIGHT JOIN) operator in the FROM clause.
• A full outer join includes the inner join plus the rows from both the left
table and the right table that are not returned by the inner join. This type
of join uses the FULL OUTER JOIN (or FULL JOIN) operator in the
FROM clause.
Construct more complex queries to answer more difficult questions. The following
query is designed to generate a list of employees who are responsible for projects,
identifying those employees who are also managers by listing the departments that
they manage:
The first outer join gets the name of any project for which the employee is
responsible; this outer join is enclosed by parentheses and is resolved first. The
second outer join gets the name of the employee's department if that employee is a
manager.
that are returned by the first query, but not by the second or any
subsequent queries.
• The INTERSECT set operator generates a result table by including only
rows that are returned by all the queries.
Following is an example of a query that makes use of the UNION set operator. The
same query could use the EXCEPT or the INTERSECT set operator by substituting
the appropriate keyword for UNION.
This statement returns a list of sales dates from the SALES table. The SALES table
in the SAMPLE database contains sales data, including the number of successful
transactions by a particular sales person on a particular date. There is typically more
than one record per date. The GROUP BY clause groups the data by sales date, and
the MAX function in this example returns the maximum number of sales recorded for
each sales date.
Here, the YEAR function is used to return the year portion of date values, and the
SUM function is used to return the total in each set of grouped sales figures. The
grouping sets list specifies how the data is to be grouped, or aggregated. A pair of
empty parentheses is added to the grouping sets list to get a grand total in the result
set. The statement returns the following:
- Ontario-North 9
- Ontario-South 52
- Quebec 53
1995 - 8
1996 - 147
A statement that is almost identical to the previous one, but that specifies the
ROLLUP clause, or the CUBE clause instead of the GROUPING SETS clause, returns
a result set that provides a more detailed perspective on the data. It might provide
summaries by location or time.
The HAVING clause is often used with a GROUP BY clause to retrieve results for
groups that satisfy only a specific condition. A HAVING clause can contain one or
more predicates that compare some property of the group with another property of
the group or a constant. For example:
This statement returns a list of salespeople whose sales totals exceed 25.
• Use a VALUES clause to specify column data for one or more rows. For
example:
Or the equivalent:
INSERT INTO staff (id, name, dept, job, years, salary, comm)
VALUES
(1212,'Cerny',20,'Sales',3,90000.00,30000.00),
(1213,'Wolfrum',20,'Sales',2,90000.00,10000.00)
UPDATE staff
SET dept = 51, salary = 70000
WHERE id = 750
Or the equivalent:
UPDATE staff
SET (dept, salary) = (51, 70000)
WHERE id = 750
If you don't specify a WHERE clause, DB2 updates each row in the table or view!
If you don't specify a WHERE clause, DB2 deletes all the rows in the table or view!
For example, consider the EMPLOYEE table to be the target table that contains
up-to-date information about the employees of a large company. Branch offices
handle updates to local employee records by maintaining their own version of the
EMPLOYEE table called MY_EMP. The MERGE statement can be used to update the
EMPLOYEE table with information that is contained in a MY_EMP table, which is the
The following statement inserts a row for new employee number 000015 into the
MY_EMP table.
And the following statement inserts updated salary data for existing employee
number 000010 into the MY_EMP table.
At this point, the inserted data exists only in the MY_EMP table because it has not
yet been merged with the EMPLOYEE table. Following is the MERGE statement that
takes the contents of the MY_EMP table and integrates them with the EMPLOYEE
table.
Correlation names have been assigned to both the source and the target table to
avoid ambiguous table references in the search condition. The statement identifies
the columns in the MY_EMP table that are to be considered. The statement also
specifies the actions that are to be taken when a row in the MY_EMP table is found
to have a match in the EMPLOYEE table, or when a row does not have a match.
The following query executed against the EMPLOYEE table now returns a record for
employee 000015:
And the following query returns the record for employee 000010 with an updated
value for the SALARY column.
The primary key for this table, Cust_ID, is an automatically generated identity
column. You can use a data-change-table-reference clause to retrieve the
generated identity column value that is being used as a customer number.
A UOW starts implicitly when the first SQL statement within an application process is
issued against the database. All subsequent reads and writes by the same
application process are considered part of the same UOW. The application ends the
UOW by issuing either a COMMIT or a ROLLBACK statement, whichever is
appropriate. The COMMIT statement makes all changes made within the UOW
permanent, whereas the ROLLBACK statement reverses those changes. If the
application ends normally without an explicit COMMIT or ROLLBACK statement, the
UOW is automatically committed. If the application ends abnormally before the end
of a UOW, that unit of work is automatically rolled back.
A savepoint lets you selectively roll back a subset of actions that make up a UOW
without losing the entire transaction. You can nest savepoints and have several
savepoint levels active at the same time; this allows your application to roll back to a
specific savepoint, as necessary. Suppose you have three savepoints (A, B, and C)
defined within a particular UOW:
do some work;
savepoint A;
do some more work;
savepoint B;
do even more work;
savepoint C;
wrap it up;
roll back to savepoint B;
For more information about savepoint levels and a detailed example illustrating
DB2's savepoint support, see Resources.
This db2 command specifies the -td option flag, which tells the command line
processor to define and to use @ as the statement termination character (because
the semicolon is already being used as a statement termination character inside the
procedure body); the -v option flag, which tells the command line processor to echo
command text to standard output; and the -f option flag, which tells the command
line processor to read command input from the specified file instead of from
standard input.
Most SQL procedures accept at least one input parameter. In our example, the input
parameter contains a value (quota) that is used in the SELECT statement contained
in the procedure body.
Many SQL procedures return at least one output parameter. Our example includes
an output parameter (sql_state) that is used to report the success or failure of the
SQL procedure. DB2 returns an SQLSTATE value in response to conditions that
could be the result of an SQL statement. Because the returned SQLCODE or
SQLSTATE value pertains to the last SQL statement issued in the procedure body,
and accessing the values alters the subsequent values of these variables (because
an SQL statement is used to access them), the SQLCODE or SQLSTATE value should
be assigned to and returned through a locally defined variable (such as the sql_state
variable in our example).
The parameter list for an SQL procedure can specify zero or more parameters, each
of which can be one of three possible types:
• IN parameters pass an input value to an SQL procedure; this value
cannot be modified within the body of the procedure.
• OUT parameters return an output value from an SQL procedure.
• INOUT parameters pass an input value to an SQL procedure and return
an output value from the procedure.
SQL procedures can return zero or more result sets. In our example, the
SALES_STATUS procedure returns one result set. This has been done by:
1. Declaring the number of result sets that the SQL procedure returns in the
DYNAMIC RESULT SETS clause.
2. Declaring a cursor in the procedure body (using the WITH RETURN FOR
clause) for each result set that is returned. A cursor is a named control
structure that is used by an application program to point to a specific row
within an ordered set of rows. A cursor is used to retrieve rows from a set.
Variables must be declared at the beginning of the SQL procedure body. To declare
a variable, assign a unique identifier to and specify an SQL data type for the variable
and, optionally, assign an initial value to the variable.
To successfully create SQL procedures, you must have installed the DB2
Application Development Client on the database server. (See the first tutorial in this
series for more on the Application Development Client.) The dependency on a C
compiler to create SQL procedures was eliminated in DB2 Universal Database
Version 8. All of the operations that were dependent on a C compiler are now
performed by DB2-generated byte code that is hosted in a virtual machine. For more
information about this enhancement, see Resources.
Use the SQL CALL statement to call SQL procedures from the DB2 command line.
The procedure being called must be defined in the system catalog. Client
applications written in any supported language can call SQL procedures. To call the
SQL procedure SALES_STATUS, perform the following steps:
Do not include double quotation marks if you are using the command line
processor (CLP) in interactive input mode, characterized by the db2 =>
input prompt.
In this example, a value of 25 for the input parameter quota is passed to the SQL
procedure, as well as a question mark (?) place-holder for the output parameter
sql_state. The procedure returns the name and the total sales figures for each
salesperson whose total sales exceed the specified quota (25). The following is
sample output returned by this statement:
SQL_STATE: 00000
SALES_PERSON TOTAL_SALES
GOUNOT 50
LEE 91
"SALES_STATUS" RETURN_STATUS: "0"
Suppose that you needed a function that returns the area of a circle when the radius
of that circle is specified as an argument to the function. Such a function is not
available as a built-in DB2 function, but you could create a user-defined SQL scalar
function to perform this task and reference the function wherever scalar functions
are supported within an SQL statement.
The NO EXTERNAL ACTION clause specifies that the function does not take any
action that changes the state of an object that the database manager does not
manage. The DETERMINISTIC keyword specifies that the function always returns
the same result for a given argument value. This information is used during query
optimization. A convenient way to execute the function is to reference it in a query.
In the following example, the query is executed (arbitrarily) against the
SYSIBM.SYSDUMMY1 catalog view, which only has one row:
You can also create a user-defined table function, which takes zero or more input
arguments and returns data as a table. A table function can only be used in the
FROM clause of an SQL statement.
Suppose that you needed a function that returns the names and employee numbers
of all employees that hold a specific job, with the title of that job specified as an
argument to the function. The following is an example of a table function that
performs this task:
The following query references the new table function in the FROM clause and
passes the job title 'CLERK' as the argument to the function. A correlation name,
introduced by the keyword AS, is required by the syntax:
Section 6. Summary
This tutorial was designed to introduce you to Structured Query Language (SQL)
and to some of the ways that DB2 9 uses SQL to manipulate data in a relational
database. It also covered the fundamentals of SQL, including SQL language
elements, Data Manipulation Language (DML), SQL procedures, and user-defined
functions. Part 5: Working with DB2 objects, which discusses data types, tables,
views, and indexes as defined by DB2, will help you understand how to create and
use them.
To keep an eye on this series, bookmark the series page, DB2 9 DBA exam 730
prep tutorials.
Resources
Learn
• Check out the other parts of the DB2 9 Fundamentals exam 730 prep tutorial
series.
• Certification exam site. Click the exam number to see more information about
Exams 730 and 731.
• DB2 9 overview. Find information about the new data server that includes
patented pureXML technology.
• DB2 XML evaluation guide: A step-by-step introduction to the XML storage and
query capabilities of DB2 9
• Find additional information about DB2 9 and SQL from the DB2 Information
Center.
• An IBM DB2 Universal Database "Stinger" Feature Preview: Enhanced
Savepoints offers more information on savepoints.
• What's New for SQL PL in the IBM DB2 Universal Database "Stinger" Release
explains the DB2 features that reduce the dependency on a C compiler to
create SQL procedures.
• Visit the developerWorks DBA Central zone to read articles and tutorials and
connect to other resources to expand your database administration skills.
• Check out the developerWorks DB2 basics series, a group of articles geared
toward beginning users.
Get products and technologies
• A trial version of DB2 9 is available for free download.
• Download DB2 Express-C, a no-charge version of DB2 Express Edition for the
community that offers the same core data features as DB2 Express Edition and
provides a solid base to build and deploy applications.