You are on page 1of 9

DB2

1. What RDBMS objects are created with the SQL CREATE statement?

The SQL CREATE statements are used to create the following objects:
STOGROUP A storage group
DATABASE A logical collection of tables
TABLESPACE An area that stores tables
TABLE A data structure organized by a specified columns
INDEX An alternate path to table data
VIEW An alternate representation of one or more tables
SYNONYM An alternate name for local table or view
ALIAS An alternate name for a table definition which may be
local or remote, existent or nonexistent

2. What RDBMS objects are required before you can create a table?

Before you create a table, you need an existing database and tablespace.

3. Can one add columns to a table after it has been defined?


Yes, one can add columns to a table after it has been defined by using the SQL ALTER
TABLE statement.

4. After a table is defined, can columns be removed?


The only way to remove columns from an existing table involves a migration program that
extracts only the desired columns of data, redifining the table without the unwanted
columns, then populating the new table. One would have to handle all of the old table's
dependents programmatically.

5. When is it necessary to create a table index?


It is necessary to create a table index whenever you want to enforce the
uniqueness of the table's primary key.

6. What is synonym? Why is it used?


A synonym is an unqualified alternative name for a table or view.

7. What is a view?
A view is a virtual table that can represent all or part of one or more tables. A view can help
simplify data access as well as provide an additional layer of access control.
8. What is a foriegn key?
A foreign key is the key defined in one table to reference the primary key of a reference
table. This foreign key must have the same structure as the reference table's primary key.

9. What is referential integrity?


Referential integrity is the automatic enforcement of referential constraints that exist
between a reference table and a referencing table. When referential integrity is enforced,
the value of a foreign key exists as a primary key value in the reference table. In other
words, when referential integrity is enforced, all of the foreign key values in, for example,
the "department code" column in an "employee" table exist as primary key values in a
"department" table.

10. What is the syntax of a SELECT statment embedded in a COBOL program?


EXEC SQL
SELECT column_name1,column_name2,column_name3
INTO host_variable1,host_variable2,host_variable3
FROM owner.tablename
WHERE condition
END-EXEC

11. What are column-name qualifiers. Why are they used?


A column-name qualifier could be a table name, a view name, a synonym name, an alias name,
or a correlation name.

12. What is a subquery?


A subquery is a query that is written as part of another query's WHERE clause. For
example: SELECT column_name1, column_name2
FROM table_a
WHERE column_name3 < (select avg(column_name3
from table_a
where column_name4 = 'constant')

13. What is a correlation name?


A correlation name is a special type of column designator that connects specific columns in
the various levels of a multilevel SQL query.

14. What is a correlated subquery?


A correlated subquery is one that has a correlation name as a table or view designator in the
FROM clause of the outer query and the same correlation name as a qualifier of a search in
the WHERE clause of the subquery. For example:
SELECT column_name1, column_name2
FROM table_a x1
WHERE column_name3 < (select avg(column_name3
from table_a
where column_name4 = x1.column_name4)
15. What is a predicate?
A predicate is the SQL language element that specifies a condition about
a value or set of values that may be true, false, or unknown.

16. What is a cursor? Why is it required?


A cursor is a named control structure used to make a set of rows available
to a program.

17. What is a result table?


A result table is the product of a query against one or more tables or
views (i.e., it is the place that hold the results of a query)

18.When is the results table for the query in a DECLARE CURSOR statement created?
The results table for a query specified in a DECLARE CURSOR statement of
a cursor is created during the execution of the OPEN CURSOR statement.

19. How do you bring the data from the result table into your program?
Data is brought into a program's working storage area from a results table
by issuing a FETCH cursor-name statment.

20. What is SQLCA?


The SQLCA is the SQL Communciation made up of a series of variables that
are udpated after each SQL statement is executed.

21. After an SQL statement is executed, what do you infer from the SQLCODE
variable when it is:
(a) Positive : The statement did execute, but constitutes a warning
that some exception condition occurred.
(b) Negative : That an error occurred and the statement did not
complete successfully.
(c) Zero : The statement executed sucessfully.
(ci)

22. What is a join?


A join is a relational operation in which data is retrieved from a
combination of two or more tables or views based on matching values within
the specified column names of each table.

23. How do you code a join of two tables that share the same column name?
The columns names in the select clause need to be qualified by the table
or view name they belong to and could be coded as follows:
EXEC SQL
SELECT table1.column1, table2.column1,
table1.column2, table2.column2
FROM table1,table2
END-EXEC.

24. What are column functions? Give 2 examples.


Column functions are features of SQL that allow you to calculate a single value derived from
one or more values found in the specified column.
AVG Returns the average value of the named column.
COUNT Returns the number of rows in the result of the query.

25. Where and when is the HAVING clause used and what does it do?
The HAVING clause is coded after the GROUP BY clause in a query that is
summarizing results by one or more grouping columns. The HAVING clause
behaves the same as the WHERE clause except that is is used to specify
the conditions each returned group must satisfy. If one row in the group
fails the conditions of the HAVING clause, the entire group is not
returned as part of the result.

26. When would you use the UNION keyword instead of join?
A join depends on the common values of the columns named in a search
condition. If there are no common values in two tables(for the specified
search columns), there would be no way to combine data from two tables
that did not have a common range of values. For example, if employee data
was stored in separate district tables, and the districts did not share
employees(have the same employee in more than one district), it would be
impossible to join the tables by employee id. In this situation, in order
to retrieve all of the employees from three districts, you would have to
code a UNION of the three tables as follows:
EXEC SQL
SELECT empid, empname, salary
FROM district1-tab
UNION
SELECT empid, empname, salary
FROM district2-tab
UNION
SELECT empid, empname, salary
FROM district3-tab
END-EXEC.

27. What is the difference between UNION and UNION ALL?


During the merge of the multiple result(one for each table in the union),
the UNION keyword causes the duplicate rows to be removed from the final
result. The UNION ALL keyword is processed so that all of the duplicate
result rows remain in the merged result table.

28. What is an outer join? How is it implemented in DB2?


29. Can a subquery have a UNION keyword?
No. Only outer-level queries can use the UNION or UNION ALL keyword.

30. If you have two similar tables combined by the UNION keyword, how can you tell which
table a result row came from?
Place a different constant at the end of the column list in the SELECT
clause of each SELECT statement in the union. The merged result will have
this constant present to indicate which SELECT statement and therefore
from which table the row came. Look at the following example:
EXEC SQL
SELECT col1, col2, col3, 'table1'
FROM table1
UNION
SELECT col1, col2, col3, 'table2'
FROM table2
END-EXEC.
The final result rows will have a 'TABLE1' or 'TABLE2' constant to tell
you which table they came from.

31. Where is the WHERE CURRENT OF clause used?


The WHERE CURRENT OF clause is used in cursor processing in order to
execute an UPDATE or DELETE statement. When a result table row is brought
into the program's working storage by a FETCH statement, the cursor
position remains on that row until the next FETCH statement or until the
cursor is closed. During this time, the cursor position could be used to
update or delete the row from the table named in the DECLARE CURSOR
statement.

32. Why is it a bad practice to use SELECT * in embedded SQL?


The reason to avoid the SELECT * in static SQL is because database
requests are validated at precompile time. If the table definitions for
the tables used in a static program change (i.e., columns are added) all
of the "INTO" clauses for those changed tables will become invalid,
causing the programs to end abnormally. If you list all of the table
column names explicitly, column additions will not affect the static
programs.

33. What do you mean by isolation level? Where is it specified?


Isolation level is specified in the BIND or REBIND statement.

34. What is the difference between cursor stability and repeatable read?
With the isolation level set to Cursor stability during read-only
processing, a page lock is held all the while an applications cursor is
positioned on it. As soon as the cursor acquires a lock on the next
required page (i.e.,the cursor is positioned on a different page of data),
the previous page lock is released.
With the isolation level set to Repeatable Read, all of the pages of data
selected by the DECLARE CURSOR statement are locked until the next
explicit or implicit commit.

35. What is a DCLGEN? Is it always required?


DCLGEN is a facility that is used to generate SQL statements that describe
a table or view. These table or view descriptions are then used to check
the validity of other SQL statements at precompile time. The table or view
declares are used by the DB2I utility DCLGEN to build a host language
structure, which is used by the DB2 precompiler to verify that correct
column names and data types have been specified in the SQL statement.

36. What is the difference between a Plan and a Package?


A plan is a DB2 object (produced during the bind process) that associates
one or more database request modules with a plan name.

37. What is a DB2 bind?


A bind is a process that builds "access paths" to DB2 tables. A bind uses
the Database Request Module(s) (DBRM(s)) from the DB2 precompile step as
input and produces an application plan. It also checks the user's
authority and validates the SQL statements in the DBRM(s).

38. What is a DBRM?


A DBRM is a DB2 component created by the DB2 precompiler containing the
SQL source statements extracted from the application program. DBRMs are
input to the bind process.

39. What does the EXPLAIN statement do?


EXPLAIN obtains information (which indexes are used, whether sorting is
necessary, which level of locking is applied) about how SQL statements in
the DBRM will be executed, inserting this informations into the "X".
PLAN_TABLE where "X" is the authorization ID of the owner of the plan.

40. What is meant by a null?


This is a special value that indicates the absence of data in a column.
This value is indicated by a negative value, usually -1.

41. Does COUNT function include a column which has null values?
In COUNT function nulls are handled just like non-null values.

42. Does AVG function take into account columns with NULL?
In AVG function nulls are handled just like non-null values.

43. What is a NULL INDICATOR? Why are they used? How is it defined?
If the data type is nullable, the application must provide a null
indicator. Otherwise an error may occur. If a null indicator is not used,
an SQLCODE = -305 is returned.
EXEC SQL
FETCH c1 INTO :cm
INDICATOR :cmid
END-EXEC.
If cmid < 0
DISPLAY 'commission is null'
If cmid is < 0 - the fetched value is NULL and cm should not be used.

44. What is wrong with this query?


Assuming that all of the columns named exist in the EMPTABLE table,
what is wrong is that you cannot nest a column function within a column
function.

45. How can you sort the output of a query in the order of the results of an expression in
the SELECT clause?
Use the ORDER BY clause and refer to the expression by its position in the
list of columns named in the SELECT clause. For example:
EXEC SQL
SELECT column1, column2, column3, (column1 - column3),
FROM table-a
ORDER BY 4
END-EXEC.

46. What are the different types of locks?


Share, Exclusive and Update

47. How can you work through a results table in reverse order?
You cannot. However, you can specify the sort key as descending in the
ORDER BY clause.

48. What is the use of SQL WHENEVER statement?


The WHENEVER statement is used to specify the HOST LANGUAGE statement
to execute every time the specified exception condition exits.

49. What do you mean by entity integrity?


Entity integrity is when the primary key is in fact unique and not null.

50. What will the EXPLAIN statement do?


EXPLAIN obtains information (which indexes are used, whether sorting is
necessary, which level of locking is applied) about how SQL statements in
the DBRM will be executed, inserting this informations into the "X".
PLAN_TABLE where "X" is the authorization ID of the owner of the plan.

51. How do you invoke DB2 from TSO?


DB2 can be invoked by TSO users by using the DSN RUN command.
52. What is SPUFI?
SPUFI stands for "SQL Processor Using File Input". SPUFI supports the
interactive execution of SQL statements from a TSO terminal.

53. What is the minimum number of tables required to enforce referential integrity?
One.

54. What are the three types of tablespaces?


Simple, Segmented and Partitioned.

55. What is the maximum number of tables allowed in a SQL statement?

56. What is the maximum number of columns in a table or view?


255.

57. What is the maximum characters allowed in a table name?


18 bytes.

58. What is the maximum character allowed in a database name?


8.

59. What is the maximum row length?


250.

60. What is the purpose of the WITHHOLD option in the DECLARE CURSOR?
WITHHOLD option is used to retain cursor position. Whenever a COMMIT is
issued all open cursors are automatically closed unless the WITHHOLD
option is coded for the cursor.
WITHHOLD prevents subsequent COMMITs from destroying the intermediate
results table from the SELECT statements thereby saving positioning with
the cursor.

61. What are stage 1 and stage 2 predicates?

62. What do the following SQLCODE value mean?


(a) -501 : Cursor identified in fetch or close is not open
(b) -811: Result of an embedded select statement is a table of
more than one row, or the result of the subquery of a
basic predicate is more than one value.
(c) -803 : Inserted or updated value invalid because index
index-name constraints columns of a table such that no
2 rows can contain duplicate values in those columns.
RID of existing row is X'rid'.
(d) -913 : Unsuccessful execution caused by deadlock or timeout.
(e) -923 : Connection not established.
(f) -911 : The current unit of work has been rolled back due to
deadlock or timeout.

63. Does the DB2 precompiler need DB2 to be active? Why?


Yes.

64. What is the CICS transaction to connect to DB2?


DSNC

65. In a CICS/DB2 program, would you use a CICS SYNCPOINT or a SQL COMMIT to
commit your updates to the database? Why?

66. Where is the AUTO COMMIT used?


AUTO COMMIT is a SPUFI option that commits the effects of SQL statements
automatically if they are successfully executed.

67. List any two DB2 scalar functions and their use.
DATE : Converts scalar value to Date
TIME : Converts scalar value to Time

68. Consider the following table representing the hierarchy of departments


in an organization.

69. How many rows can be retrieved using a SELECT INTO statement in an
application?
Only one row.

70. What is the difference between UNION and UNION ALL?


During the merge of the multiple result (one for each table in the union),
the UNION keyword causes the duplicate rows to be removed from the final
result. The UNION ALL keyword is processed so that all of the duplicate
result rows remain in the merged result table.

You might also like