DB2 FAQS 1. How would you find out the total number of rows in a table? o Use SELECT COUNT(*) ... 2. How do you elimina...
DB2 FAQS
1. How would you find out the total number of rows in a table?
o Use SELECT COUNT(*) ...
2. How do you eliminate duplicate values in SELECT?
o Use SELECT DISTINCT ...
3. How do you select a row using indexes?
o Specify the indexed columns in the WHERE clause.
4. What are aggregate functions?
o Built-in mathematical functions for use in SELECT clause.
5. How do you find the maximum value in a column?
o Use SELECT MAX(...)
6. Can you use MAX on a CHAR column?
o Yes.
7. My SQL statement SELECT AVG(SALARY) FROM EMP yields inaccurate results. Why?
o Because SALARY is not declared to have NULLs and the employees for whom the salary is not known are also counted.
8. How do you retrieve the first 5 characters of FIRSTNAME column of EMP table?
o SELECT SUBSTR(FIRSTNAME,1,5) FROM EMP;
9. How do you concatenate the FIRSTNAME and LASTNAME from EMP table to give a complete name?
o SELECT FIRSTNAME ¦¦ ' ' ¦¦ LASTNAME FROM EMP;
10. What is the use of VALUE function?
o Avoid -ve SQLCODEs by handling nulls and zeroes in computations
o Substitute a numeric value for any nulls used in computation
11. What is UNION ,UNION ALL?
o UNION : eliminates duplicates
UNION ALL: retains duplicates
Both these are used to combine the results of different SELECT statements.
Both these are used to combine the results of different SELECT statements.
12. Suppose I have five SQL SELECT statements connected by UNION/UNION ALL, how many times should I specify UNION to eliminate the duplicate rows?
o Once.
13. What is the restriction on using UNION in embedded SQL?
o It has to be in a CURSOR.
14. In the WHERE clause what is BETWEEN and IN?
o BETWEEN supplies a range of values while IN supplies a list of values.
15. Is BETWEEN inclusive of the range values specified?
o Yes.
16. What is 'LIKE' used for in WHERE clause? What are the wildcard characters?
o LIKE is used for partial string matches. '%' ( for a string of any character ) and '_' (for any single character ) are the two wild card characters.
17. When do you use a LIKE statement?
o To do partial search e.g. to search employee by name, you need not specify the complete name; using LIKE, you can search for partial string matches.
18. What is the meaning of underscore ( '_' ) in the LIKE statement?
o Match for any single character.
19. What do you accomplish by GROUP BY ... HAVING clause?
o GROUP BY partitions the selected rows on the distinct values of the column on which you group by.
o HAVING selects GROUPs which match the criteria specified
20. Consider the employee table with column PROJECT nullable. How can you get a list of employees who are not assigned to any project?
o SELECT EMPNO
FROM EMP
WHERE PROJECT IS NULL;
FROM EMP
WHERE PROJECT IS NULL;
21. What is the result of this query if no rows are selected:
o SELECT SUM(SALARY)
FROM EMP
WHERE QUAL='MSC';
NULL
FROM EMP
WHERE QUAL='MSC';
NULL
22. Why SELECT * is not preferred in embedded SQL programs?
o For three reasons:
1. If the table structure is changed ( a field is added ), the program will have to be modified.
2. Program might retrieve the columns which it might not use, leading on I/O over head.
3. The chance of an index only scan is lost.
23. What are correlated sub queries?
o A sub query in which the inner ( nested ) query refers back to the table in the outer query. Correlated sub queries must be evaluated for each qualified row of the outer query that is referred to.
24. What is a cursor? why should it be used?
o Cursor is a programming device that allows the SELECT to find a set of rows but return them one at a time.
o Cursor should be used because the host language can deal with only one row at a time.
25. How would you retrieve rows from a DB2 table in embedded SQL?
o Either by using the single row SELECT statements, or by using the CURSOR.
26. Apart from cursor, what other ways are available to you to retrieve a row from a table in embedded SQL?
o Single row SELECTs.
27. How do you specify and use a cursor in a COBOL program?
o Use DECLARE CURSOR statement either in working storage or in procedure division (before open cursor), to specify the SELECT statement. Then use OPEN, FETCH rows in a loop and finally CLOSE.
28. What happens when you say OPEN CURSOR?
o If there is an ORDER BY clause, rows are fetched, sorted and made available for the FETCH statement. Other wise simply the cursor is placed on the first row.
29. Is DECLARE CURSOR executable?
o No.
30. Can you have more than one cursor open at any one time in a program?
o Yes.
31. When you COMMIT, is the cursor closed?
o Yes.
32. How do you leave the cursor open after issuing a COMMIT? (for DB2 2.3 or above only )
o Use WITH HOLD option in DECLARE CURSOR statement. But, it has not effect in psuedo-conversational CICS programs.
33. Give the COBOL definition of a VARCHAR field.
o A VARCHAR column REMARKS would be defined as follows:
10 REMARKS.
49 REMARKS-LEN PIC S9(4) USAGE COMP.
49 REMARKS-TEXT PIC X(1920).
34. What is the physical storage length of each of the following DB2 data types: DATE, TIME, TIMESTAMP?
DATE: | 4bytes |
TIME: | 3bytes |
TIMESTAMP: | 10bytes |
35. What is the COBOL picture clause of the following DB2 data types: DATE, TIME, TIMESTAMP?
DATE: | PIC X(10) |
TIME : | PIC X(08) |
TIMESTAMP: | PIC X(26) |
36. What is the COBOL picture clause for a DB2 column defined as DECIMAL(11,2)?
o PIC S9(9)V99 COMP-3.
Note: In DECIMAL(11,2), 11 indicates the size of the data type and 2 indicates the precision.
Note: In DECIMAL(11,2), 11 indicates the size of the data type and 2 indicates the precision.
37. What is DCLGEN?
o DeCLarations GENerator: used to create the host language copybooks for the table definitions. Also creates the DECLARE table.
38. What is JOIN and different types of JOIN.
o The ability to join rows and combine data from two or more tables is one of the most powerful features of relational system. Three types of joins:
1. Equi-join
2. Non-equijoin
3. self-join
39. Can I alter a table (e.g. adding a column) when other user is selecting some Columns or updating some columns from the same table?
o yes possible. until the updation or selection is committed db2 table will not be restructured. new column definition will be there but it will not be included until all the tasks on the table are committed.
40. What are the different methods of accessing db2 from tso?
o There are three ways in establishing tso/db2 connection
1. SPUFI
2. QMF
3. CATALOG VISIBILITY
41. How is the connection established between TSO & DB2?
o A thread between TSO & DB2 is established while attempting to make Connection between tso & db2.
42. What is sqlcode -922?
o Authorization failure
43. How do you do the EXPLAIN of a dynamic SQL statement?
o Use SPUFI or QMF to EXPLAIN the dynamic SQL statement
o Include EXPLAIN command in the embedded dynamic SQL statements
44. How is a typical DB2 batch pgm executed?
o Use DSN utility to run a DB2 batch program from native TSO. An example is shown:
DSN | SYSTEM(DSP3) |
RUN | PROGRAM(EDD470BD) PLAN(EDD470BD) |
LIB | ('EDGS01T.OBJ.LOADLIB') |
END | |
o Use IKJEFT01 utility program to run the above DSN command in a JCL.
45. Is it mandatory to use DCLGEN? If not, why would you use it at all?
o It is not mandatory to use DCLGEN.
Using DCLGEN, helps detect wrongly spelt column names etc. during the
pre-compile stage itself (because of the DECLARE TABLE ).
DCLGEN being a tool, would generate accurate host variable definitions for the table reducing chances of error.
Using DCLGEN, helps detect wrongly spelt column names etc. during the
pre-compile stage itself (because of the DECLARE TABLE ).
DCLGEN being a tool, would generate accurate host variable definitions for the table reducing chances of error.
46. Name some fields from SQLCA.
o SQLCODE, SQLERRM, SQLERRD
47. How does DB2 determine what lock-size to use?
o Based on the lock-size given while creating the table space
o Programmer can direct the DB2 what lock-size to use
o If lock-size ANY is specified, DB2 usually choses a lock-size of PAGE
48. What is the difference between CS and RR isolation levels?
o CS: Releases the lock on a page after use
o RR: Retains all locks acquired till end of transaction
49. Where do you specify them?
o ISOLATION LEVEL is a parameter for the bind process.
50. How do you simulate the EXPLAIN of an embedded SQL statement in SPUFI/QMF? Give an example with a host variable in WHERE clause.
o Use question mark in place of a host variable (or an unknown value). e.g.
SELECT EMP_NAME FROM EMP WHERE EMP_SALARY > ? |
51. What is ACQUIRE/RELEASE in BIND?
o Determine the point at which DB2 acquires or releases locks against table and Table spaces, including intent locks.
52. In SPUFI suppose you want to select max. of 1000 rows, but the select returns only 200 rows. What are the 2 sqlcodes that are returned?
o 100 (for successful completion of the query), 0 (for successful COMMIT if AUTOCOMMIT is set to Yes).
53. How would you print the output of an SQL statement from SPUFI?
o Print the output data set.
54. What does it mean if the null indicator has -1, 0, -2?
o -1 : the field is null
o 0 : the field is not null
o -2 : the field value is truncated
55. How do you retrieve the data from a nullable column?
o Use null indicators. Syntax ... INTO :HOSTVAR:NULLIND
56. What else is there in the PLAN apart from the access path?
o PLAN has the executable code for the SQL statements in the host program
57. What is lock escalation?
o Promoting a PAGE lock-size to table or table space lock-size when a transaction has aquired more locks than specified in NUMLKTS. Locks should be taken on objects in single table space for escalation to occur.
58. When is the access path determined for dynamic SQL?
o At run time, when the PREPARE statement is issued.
59. What are the various locks available?
o SHARE, EXCLUSIVE, UPDATE
60. What is sqlcode -811?
o SELECT statement has resulted in retrieval of more than one row.
61. What are the advantages of using a PACKAGE?
o Avoid having to bind a large number of DBRM members into a plan
o Avoid cost of a large bind
o Avoid the entire transaction being unavailable during bind and automatic rebind of a plan
o Minmize fallback complexities if changes result in an error.
62. What is REORG? When is it used?
o REORG reorganizes data on physical storage to re-cluster rows, positioning oveflowed rows in their proper sequence, to reclaim space, to restore free space. It is used after huge updates, inserts and delete activity and after segments of a segmented table space have become fragmented.
63. How does DB2 store NULL physically?
o as an extra-byte prefix to the column value. physically, the nul prefix is Hex '00' if the value is present and Hex 'FF' if it is not
64. What is CHECK PENDING?
o When a table is LOADed with ENFORCE NO option, then the table is left in CHECKPENDING status. It means that the LOAD utility did not perform constraint checking.
65. When do you specify the isolation level? How?
o During the BIND process. ISOLATION (CS/RR)...
66. What is a DBRM, PLAN?
o DBRM: DataBase Request Module, has the SQL statements extracted from the host language program by the pre-compiler.
o PLAN: A result of the BIND process. It has the executable code for the SQL statements in the DBRM.
67. Is DECLARE TABLE in DCLGEN necessary? Why it used?
o It not necessary to have DECLARE TABLE statement in DCLGEN. This is used by the pre-compiler to validate the table-name, view-name, column name etc., during pre-compile.
68. What do you need to do before you do EXPLAIN?
o Make sure that the PLAN_TABLE is created under the AUTHID.
69. What is a collection?
o a user defined name that is the anchor for packages. It has not physical existence. Main usage is to group packages.
70. How can you quickly find out the # of rows updated after an update statement?
o Check the value stored in SQLERRD(3).
71. What is EXPLAIN?
o EXPLAIN is used to display the access path as determined by the optimizer for a SQL statement. It can be used in SPUFI (for single SQL statement ) or in BIND step (for embedded SQL ).
72. Where is the output of EXPLAIN stored?
o In userid.PLAN_TABLE
73. Suppose I have a program which uses a dynamic SQL and it has been performing well till now. Off late, I find that the performance has deteriorated. What happened?
o Probably RUN STATS is not done and the program is using a wrong index due to incorrect stats. Probably RUNSTATS is done and optimizer has chosen a wrong access path based on the latest statistics.
74. What are the contents of a DCLGEN? - GS
o EXEC SQL DECLARE TABLE statement which gives the layout of the table/view in terms of DB2 data types.
o A host language copy book that gives the host variable definitions for the column names.
75. Will pre-compile of an DB2-COBOL program bomb, if DB2 is down?
o No. Because the pre-compiler does not refer to the DB2 catalogue tables.
76. What are the isolation (also called isolation parameters) levels possible?
o CS: Cursor Stability
o RR: Repeatable Read
77. What is the picture clause of the null indicator variable?
o S9(4) COMP.
78. EXPLAIN has output with MATCHCOLS = 0. What does it mean?
o A non-matching index scan if ACCESSTYPE = I.
79. I use CS and update a page. Will the lock be released after I've done with that page?
o No.
80. What are the various locking levels available?
o PAGE, TABLE, TABLESPACE
81. What is ALTER?
o SQL command used to change the definition of DB2 objects.
82. Can I use LOCK TABLE on a view?
o No. To lock a view, take lock on the underlying tables.
83. What are the disadvantages of PAGE level lock?
o High resource utilization if large updates are to be done
84. What are PACKAGES ?
o They contain executable code for SQL statements for one DBRM.
85. Lot of updates have been done on a table due to which indexes have gone haywire. What do you do?
o Looks like index page split has occured. DO a REORG of the indexes.
86. What is dynamic SQL?
o Dynamic SQL is a SQL statement created at program execution time.
87. What is IMAGECOPY ?
o It is full backup of a DB2 table which can be used in recovery.
88. What is QUIESCE?
o A QUIESCE flushes all DB2 buffers on to the disk. This gives a correct snapshot of the database and should be used before and after any IMAGECOPY to maintain consistency.
89. What does the sqlcode of -818 pertain to? - GS
o This is generated when the consistency tokens in the DBRM and the load module are different.
90. What happens to the PLAN if index used by it is dropped?
o Plan is marked as invalid. The next time the plan is accessed, it is rebound.
91.
o
92. What is FREEPAGE and PCTFREE in TABLESPACE creation?
o PCTFREE: percentage of each page to be left free
o FREEPAGE: Number of pages to be loaded with data between each free page
93. Are views updatable?
o Not all of them. Some views are updatable e.g. single table view with all the fields or mandatory fields. Examples of non-updatable views are views which are joins, views that contain aggregate functions (such as MIN), and views that have GROUP BY clause.
94. What is COPY PENDING status?
o A state in which, an image copy on a table needs to be taken, in this state, the table is available only for queries. You cannot update this table. To remove the COPY PENDING status, you take an image copy or use REPAIR utility.
95. What is an inner join, and an outer join?
o Inner Join: combine information from two or more tables by comparing all values that meet the search criteria in the designated column or columns of one table with all the values in corresponding columns of the other table or tables. This kind of join which involve a match in both columns are called inner joins.
o Outer join is one in which you want both matching and non matching rows to be returned. DB2 has no specific operator for outer joins, it can be simulated by combining a join and a correlated sub query with a UNION .
96.
o
97. What is the difference between primary key & unique index?
o Primary: a relational database constraint. Primary key consists of one or more columns that uniquely identify a row in the table. For a normalized relation, there is one designated primary key.
o Unique index: a physical object that stores only unique values. There can be one or more unique indexes on a table.
98. When do you use the IMAGECOPY?
o To take routine backup of tables.
o After a LOAD with LOG NO. After REORG with LOG ON.
99. What is RUNSTATS?
o A DB2 utility used to collect statistics about the data values in tables which can be used by the optimizer to decide the access path. It also collects statistics used for space management. These statistics are stored in DB2 catalog tables.
100. How do you insert a record with a nullable column?
o To insert a NULL, move -1 to the null indicator
o To insert a valid value, move 0 to the null indicator
No comments
Post a Comment