RelationalDBDesignRelationalDBDesign





DB2 Questions   «Prev  Next»

DB2 Questions 6

  1. What is cursor stability?
    Answer: Cursor stability means that DB2 takes a lock on the page the cursor is accessing and releases the lock when the cursor moves to another page.
  2. What is the significance of the CURSOR WITH HOLD clause in a cursor declaration?
    Answer: The clause avoids closing the cursor and repositioning it to the last row processed when the cursor is reopened.
  3. What is the SQL Communications Area and what are some of its key fields?
    Answer: It is a data structure that must be included in any host-language program using SQL.
    It is used to pass feedback about the sql operations to the program. Fields are return codes, error messages, handling codes and warnings.
  4. What is the purpose of the WHENEVER statement?
    Answer: The WHENEVER statement is coded once in the host program to control program actions depending on the SQL-CODE returned by each sql statement within the program.
  5. What is DCLGEN
    Answer: DCLGEN stands for declarations generator;  it is a facility to generate DB2 sql data structures in COBOL or PL/I programs.
  6. What is the FREE command?
    Answer: The FREE command can be used to delete plans and/or packages no longer needed.
  7. DB2 can implement a join in three ways using a 1) merge join, 2)nested join or 3) hybrid join.
    Explain the differences.
    Answer:
    merge join: A merge join requires that the tables being joined be in a sequence; the rows are retrieved with a high cluster ratio index or are sorted by DB2.
    nested join: A nested join does not require a sequence and works best on joining a small number of rows. DB2 reads the outer table values and each time scans the inner table for matches.
    hybrid join: The hybrid join is a nested join that requires the outer table be in sequence.
  8. Compare a subselect to a join.
    Answer: Any subselect can be rewritten as a join, but not vice versa.  Joins are usually more efficient as join rows can be returned immediately, subselects require a temporary work area for inner selects results while processing the outer select.
  9. What is the difference between IN subselects and EXISTS subselect?
    Answer: If there is an index on the attributes tested an IN is more efficient since DB2 uses the index for the IN.   (IN for index is the mnemonic).
  10. What is a Cartesian product?
    Answer: A Cartesian product results from a faulty query.
    It is a row in the results for every combination in the join tables.



DB2 joined-table Described

A joined-table specifies an intermediate result table that is the result of either an inner, outer, cross, or exception join.
The table is derived by applying one of the join operators: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER, LEFT EXCEPTION, RIGHT EXCEPTION, or CROSS to its operands.
The table is derived by applying one of the join operators: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER, LEFT EXCEPTION, RIGHT EXCEPTION, or CROSS to its operands.

Diagram for displaying INNER( LEFT, RIGHT, FULL LEFT) OUTER join
Diagram for displaying INNER( LEFT, RIGHT, FULL LEFT) OUTER join

If a join operator is not specified, INNER is implicit. The order in which multiple joins are performed can affect the result. Joins can be nested within other joins. The order of processing for joins is generally from left to right, but based on the position of the required join-condition or USING clause.
Parentheses are recommended to make the order of nested joins more readable. For example:
TB1 LEFT JOIN TB2 ON TB1.C1=TB2.C1
LEFT JOIN TB3 LEFT JOIN TB4 ON TB3.C1=TB4.C1
ON TB1.C1=TB3.C1

is the same as
(TB1 LEFT JOIN TB2 ON TB1.C1=TB2.C1)
LEFT JOIN (TB3 LEFT JOIN TB4 ON TB3.C1=TB4.C1)
ON TB1.C1=TB3.C1

An inner join combines each row of the left table with every row of the right table keeping only the rows where the join-condition (or USING clause) is true. Thus, the result table may be missing rows from either or both of the joined tables. Outer joins include the rows produced by the inner join as well as the missing rows, depending on the type of outer join.
Exception joins include only the missing rows, depending on the type of exception join.
  1. A left outer join includes the rows from the left table that were missing from the inner join.
  2. A right outer join includes the rows from the right table that were missing from the inner join.
  3. A full outer join includes the rows from both tables that were missing from the inner join.
  4. A left exception join includes only the rows from the left table that were missing from the inner join.
  5. A right exception join includes only the rows from the right table that were missing from the inner join.
A joined table can be used in any context in which any form of the SELECT statement is used. A view or a cursor is read-only if its SELECT statement includes a joined table.

Example: left outer join using nested loop

The following figure illustrates a nested loop for a left outer join. The outer join preserves the unmatched row in OUTERT with values A=10 and B=6. The same join method for an inner join differs only in discarding that row. Figure 1.6 illustrates a nested loop join.

Figure 1. Nested loop join for a left outer join
Figure 1.6: Nested loop join for a left outer join