Friday, March 30, 2012

Tips on DB2

Tips on DB2
1. In the WHERE clause what is BETWEEN and IN? - GS
BETWEEN supplies a range of values while IN supplies a list of values.
2. Is BETWEEN inclusive of the range values specified? - GS
Yes.
3. What is 'LIKE' used for in WHERE clause? What are the wildcard characters? - GS
LIKE is used for partial string matches. ‘%’ ( for a string of any character ) and ‘_’ (for any single character ) are the two wild card characters.
4. When do you use a LIKE statement?
To provide partial search facility e.g. to search employee by name, you need not specify the complete name, using LIKE, you can search for partial string matches.
5. What is the meaning of underscore ( ‘_’ ) in the LIKE statement? - GS
Match for any single character.
6. What do you accomplish by GROUP BY .... HAVING clause? - GS
Can think of the HAVING as a ‘WHERE’ clause on the GROUP
7. What is a cursor? why should it be used? - GS
When multiple of rows are to be retrieved with embedded SQL, a cursor should be used.
8. Where would you specify the DECLARE CURSOR statement? - GS
See answer to next question.
9. How do you specify and use a cursor in a COBOL program? - GS
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.
10. How would you retrieve rows from a DB2 table in embedded SQL? - GS
Either by using the single row SELECT statements,or by using the CURSOR.
11. What is the COBOL picture clause for a DB2 column defined as DECIMAL(11,2)? - GS
PIC S9(9)V99 COMP-3.
Note: In DECIMAL(11,2), 11 indicates the size of the data type and 2 indicates the precision.
12. What is DCLGEN ? - GS
DeCLarations GENerator: used to create the host language copy books for the table definitions. Also creates the DECLARE table.
13. What are the contents of a DCLGEN? - GS
1. EXEC SQL DECLARE TABLE statement which gives the layout of the table/view in terms of DB2 datatypes.
2. A host language copy book that give the host variable definitions for the column names.
14. Is it mandatory to use DCLGEN? If not, why would you use it at all? - GS
It is not mandatory to use DCLGEN.
Using DCLGEN, helps detect wrongly spelt column names etc. during the precompile stage itself ( because of the DECLARE TABLE ). DCLGEN being a toll, would generate accurate host variable definitions for the table reducing chances of error.
15. Is DECLARE TABLE in DCLGEN necessary? Why it used?
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.
16. What is EXPLAIN? - GS
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 ).
17. What do you need to do before you do EXPLAIN?
Make sure that the PLAN_TABLE is created. Do RUNSTATS. ( Any thing else? )
18. Where is the output of EXPLAIN stored? - GS
In userid.PLAN_TABLE
19. EXPLAIN has output with MATCHCOLS = 0. What does it mean? – GS
While doing the accesspath, objects accessed have matching cols as 0, in other words index scan or TS scan is used depending on access type R or I
20. How do you do the EXPLAIN of a dynamic SQL statement?
Using Spufi. At bind time, accesspath are not defined. So all permutation and combinations of SQLs need to be examined separately in spufi.
21. How do you simulate the EXPLAIN of an embedded SQL statement in SPUFI/QMF? Give an example with a host variable in WHERE clause.)
22. What are the isolation levels possible ? - GS
CS: Cursor Stability – A read lock is released as soon as access is moved to next page
RR: Repeatable Read – Lock is released only after commit or end of execution
23. Where do you specify them ?
ISOLATION LEVEL is a parameter for the bind process.
24. I use CS and update a page. Will the lock be released after I am done with that page?
No
25. What is ALTER ? - GS
SQL command used to change the definition of DB2 object.
26. What are PACKAGES ? - GS
They contain executable code for SQL statements. Can contain SQL statements for only one DBRM.
Non Executable accesspath instruction for a DBRM.
27. What are the advantages of using a PACKAGE?
In Online Systems – Where there is normally one plan used, You do not have to bind the plan again. So downtime is reduced.
If packages are used than binding changed program is required, however in other case you will have to bind whole plan.
28. What is RUNSTATS? - GS
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. These statistics are stored in DB2 catalog tables and is critical in determining accesspaths for a SQL
29. When will you chose to run RUNSTATS?
After a load, or after mass updates, inserts, deletes, or after REORG. And Periodically
30. Give some example of statistics collected during RUNSTATS?
Col Count – Number of unique values in a column (if high index access is used), Number of columns populated.
31. 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? - GS
100 ( for successful completion of the query ), 0 (for successful COMMIT if AUTOCOMMIT is set to Yes).
32. How would you print the output of an SQL statement from SPUFI? - GS
Print the output dataset.
33. How do you pull up a query which was previously saved in QMF ? - GS
34. How do you select a row using indexes? - GS
Specify the indexed columns in the WHERE clause.
35. Lot of updates have been done ona table due to which indexes have gone haywire. What do you do? – GS
Reorg
36. What are split indexes? ( exact question is not known ) GS
Ramesh, I think they were talking about what do you do if index splits occur due to mass inserts/updates. I think the answer is a REORG is required.
37. What happens when you say OPEN CURSOR?
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.
In case of Open Cursor
If Cursor statement is complex (i.e. multiple tables accessed, access path use multiple indexes etc.) than materialization happenes and a temporary table is created.
In other case cursor is simply placed on first row depending on Order By clause
38. Is DECLARE CURSOR executable?
No.
39. Can you have more than one cursor open at any one time in a program ? - GS
Yes.
40. How would you find out the total number of rows in a table? - GS
Use SELECT COUNT(*) ...
41. How do you eliminate duplicate values in SELECT? - GS
Use SELECT DISTINCT ...
42. How do you find the maximum value in a column? - GS
Use SELECT MAX(...
43. What does the sqlcode of -818 pertain to? - GS
This is generated when the consistency tokens in the DBRM and the load module are different.
44. What is UNION,UNION ALL? - GS
UNION : eliminates duplicates
UNION ALL: retains duplicates
Both these are used to combine the results of different SELECT statements.
45. Suppose I have five SQL SELECT statements connected by UNION/UNION ALL, how many times should I specify UNION to eliminate the duplicate rows? - GS
Once.
46. What is dynamic SQL? - GS Dynamic SQL is a SQL statement created at program execution time.
47. What else is there in the PLAN apart from the access path? - GS
PLAN has the executable code for the SQL statements in the host program
48. When is the access path determined for dynamic SQL? - GS
At run time, when the PREPARE statement is issued.
49. 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? - GS
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.
50. Apart from cursor, what other ways are available to you to retrieve a row from a table in embedded SQL? - GS
Single row SELECTs.
51. How do you retrieve the data from a nullable column? - GS
Use null indicators. Syntax INTO :HOSTVAR:NULLIND
52. What is the picture clause of the null indicator variable? - GS
S9(4) COMP.
53. What does it mean if the null indicator has -1, 0, -2
-1 : the field is null
0 : the field is not null
-2 : the field value is truncated
54. How do you insert a record with a nullable column?
To insert a NULL, move -1 to the null indicator
To insert a valid value, move 0 to the null indicator
55. Some questions on concatenate, substring features of DB2. - GS
56. What is IMAGECOPY ? - GS
It is full backup of a DB2 table which can be used in recovery.
57. When do you use the IMAGECOPY? - GS
To take routine backup of tables
After a LOAD with LOG NO
58. What is a clustering index ? - GS
A mandatory index defined on a partitioned table space. Causes the data rows to be stored in the order specified in the index. Obviously a table can have only one clustering index.
59. What are correlated subqueries? - GS
.
One in which the lower level nested select refers back to the table in the higher level.
60. What are the issues related with correlated subqueries? - GS
61. What is sqlcode -922 ?
62. What is sqlcode -811?
63. What is a DBRM, PLAN ?
DBRM: DataBase Request Module, has the SQL statements extracted from the host language program by the SQL precompile.
PLAN: A result of the BIND process. It has the executable code for the SQL statements in the DBRM.
64. What happens to the PLAN if index used by it is dropped? Plan is marked as invalid. The next time the plan is invoked, it is recreated.
65. What is the difference between primary key & unique index ?
66. What is QUIESCE?
A QUIESCE flushes all DB2 buffers on to the disk. This gives a correct snapshot of the database and should be used before any IMAGECOPY to maintain consistency.
67. Are views updatable ?
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).
68. If I have a view which is a join of two or more tables, can this view be updatable? - GS
No.
69. What is a synonym ?
Synonym is an alternate name for a table or view. A synonym is accessible only by the creator.
70. What is FREEPAGE and PCTFREE in TABLESPACE creation?
PCTFREE: percentage of each page to be left free
FREEPAGE: Number of pages to be loaded with data between each free page
71. What is CHECK PENDING ? Tips on Mainframe Pend status of table space to check for different values. Usuall before load TS is pended in four different pend status. After load a image copy job is run which will eliminate these pend statuses
72. What are the 4 environments which can access DB2 ?
TSO, CICS, IMS and BATCH
73. What is outer join ?
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.
74. What are simple, segmented and partitioned table spaces ?
Simple Tablespace:
Can contain one or more tables
Rows from multiple tables can be interleaved on a page under the DBAs control and maintenance
Segmented Tablespace:
Can contain one or more tables
Tablespace is divided into segments of 4 to 64 pages in increments of 4 pages. Each segment is dedicated to single table. A table can occupy multiple segments
Partitioned Tablespace:
Can contain one table
Tablespace is divided into parts and each part is put in a separate VSAM dataset.
75. How is a typical DB2 batch pgm executed ? Tips on Mainframe Use DSN utility to run a DB2 batch program. An example is shown:
DSN SYSTEM(DSP3)
RUN PROGRAM(EDD470BD) PLAN(EDD470BD) LIB('EDGS01T.OBJ.LOADLIB')
END
Use IKJEFT01 utility program to run this command in a JCL.
76. Assuming that a site’s standard is that pgm name = plan name, what is the easiest way to find out which pgms are affected by change in a table’s structure ?
Query the catalogue table SYSPLANDEP.
77. Name some fields from SQLCA.
SQLCODE, SQLERRM, SQLERRD ...
78. When do you specify the isolation level? How?
During the BIND process. ISOLATION ( CS/RR )...
79. How can you quickly find out the # of rows updated after a mass update statement?
Check the value stored in SQLERRD(3).
80. Consider the employee table with column PROJECT nullable. How can you get a list of employees who are not assigned to any project?
SELECT EMPNO
FROM EMP
WHERE PROJECT IS NULL;
81. Why SELECT * is not preferred in embedded SQL programs?
For three reasons:
• If the table structure is changed ( a field is added ), the program will have to be modified
• Program might retrieve the columns which it might not use, leading on I/O over head.
• The chance of an index only scan is lost.
82. What is filter factor?
Number of distinct col values/Number of rows in the table
83. What is index cardinality? - GS
84. What are the various locking levels available?
PAGE, TABLE, TABLESPACE
Subpage, row level with type2 indexes
85. How does DB2 determine what locking level to use?
Type of indexes, Bind
86. What are the disadvantages of PAGE level lock?
Concurrency is limited
87. What is lock escalation?
Lock moved from a subpage to page or page to tablespace
88. What are the various locks available?
SHARE, EXCLUSIVE, UPDATE
89. What is the difference between CS and RR isolation levels?
CS: Releases the lock on a page after use
RR: Retains all locks acquired till end of transaction
90. Can I use LOCK TABLE on a view?
No. To lock a view, take lock on the underlying tables.
91. What are COLLECTIONS?
Prefixes for a package name. Makes it convenient to use in PKLIST for plans.
92. When you COMMIT, is the cursor closed?
Yes.
93. How do you leave the cursor open after issuing a COMMIT? ( for DB2 2.3 or above only )
Use WITH HOLD option in DECLARE CURSOR statement. Has no effect in CICS. I believe CICS will retain posn after a SYNCPOINT???.
In pseudoconversation CURSOR with HOLD also gets closed
94. Give the COBOL definition of a VARCHAR field.
95. What is the physical storage length of each of the following DB2 data types:
DATE, TIME, TIMESTAMP?
DATE: 4bytes
TIME: 3bytes
TIMESTAMP: 10bytes
96. 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)
97. What is the result of this query if no rows are selected:
SELECT SUM(SALARY)
FROM EMP
WHERE QUAL=‘MSC’;
NULL
98. What is the difference between SYNONYM and ALIAS?
SYNONYM: is dropped when the table or tablespace is dropped. Synonym is available only to the creator.
ALIAS: is retained even if table or tablespace is dropped. ALIAS can be created even if the table does not exist. It is used mainly in distributed environment to hide the location info from programs. Alias is a global object & is available to all.
99. My SQL statement SELECT AVG(SALARY) FROM EMP yields inaccurate results. Why?
Because SALARY is not declared to have NULLs and the employees for whom the salary is not known are also counted.
100.How do you retrieve the first 5 characters of FIRSTNAME column of EMP table?
SELECT SUBSTR(FIRSTNAME,1,5) FROM EMP;
101.How do you concatenate the FIRSTNAME and LASTNAME from EMP table to give a complete name?
SELECT FIRSTNAME || ‘ ‘ || LASTNAME FROM EMP;
102.What is the use of VALUE function?
Avoid handling NULLable fields. It assigns a default of 0 to numeric fields which are NULL.
103.What do you mean by NOT NULL WITH DEFAULT? When will you use it?
In insert if column is not specified than default value is taken
104.What do you mean by NOT NULL? When will you use it?
Column needs to have some value specified.
105.What are aggregate functions?
Max, Sum, Avg
106.What is REORG? When is it used?
107.How do I create a table MANAGER ( EMP#, MANAGER) where MANAGER is a foreign key which references to EMP# in the same table? Give the exact DDL.
108.Will precompile of an DB2-COBOL program bomb, if DB2 is down?
No. Because the precompiler does not refer to the DB2 catalogue tables.
109.Can you use MAX on a CHAR column?
YES.
110.What is the restriction on using UNION in embedded SQL?
It has to be in a CURSOR.
111.When does the authorization check on DB2 objects is done - at BIND time or run time?
Both, there is bind authority and execution authority
112.What is COPY PENDING status?
A state in which, an image copy on a table needs to be taken, In this status, 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.
113.How many clustering indexes can be defined for a table?
Only one.
114.How does DB2 store NULL physically?
High Values
115.When would you prefer to use VARCHAR?
116.What are the disadvantages of using VARCHAR?
117.What happens to the plans when an index used by them is dropped?
Marked as invalid
118.What is auditing?
Logging updates
119.What is ACQUIRE/RELEASE in BIND?
When to release the lock at commit or task termination point.

No comments:

Post a Comment