How would you find out the total number of rows in a DB2 table?
Use SELECT COUNT(*) … in db2 query
How do you eliminate duplicate values in DB2 SELECT ?
Use SELECT DISTINCT … in db2 query
How do you select a row using indexes in DB2?
Specify the indexed columns in the WHERE clause of db2 query.
How do you find the maximum value in a column in db2?
Use SELECT MAX(…) .. in db2 query
How do you retrieve the first 5 characters of FIRSTNAME column of DB2 table EMP?
SQL Query : SELECT SUBSTR(FIRSTNAME,1,5) FROM EMP;
What are aggregate functions?
Built-in mathematical functions for use in SELECT clause.
Can you use MAX on a CHAR column?
YES.
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.
How do you concatenate the FIRSTNAME and LASTNAME from EMP table to give a complete name?
SELECT FIRSTNAME || ‘ ‘ || LASTNAME FROM EMP;
What is the use of VALUE function?
1. Avoid -ve SQLCODEs by handling nulls and zeroes in computations
2. Substitute a numeric value for any nulls used in computation
What is UNION, UNION ALL?
UNION: eliminates duplicates
UNION ALL: retains duplicates
Both these are used to combine the results of different SELECT statements.
Suppose I have five SQL SELECT statements connected by UNION/UNION ALL, how many times should I specify UNION to eliminate the duplicate rows? - Once.
What is the restriction on using UNION in embedded SQL?
It has to be in a CURSOR.
In the WHERE clause what is BETWEEN and IN?
BETWEEN supplies a range of values while IN supplies a list of values.
Is BETWEEN inclusive of the range values specified?
Yes.
What is ‘LIKE’ used for in WHERE clause? What are the wildcard characters?
LIKE is used for partial string matches. ‘%’ (for a string of any character) and ‘_’ (for any single character) are the two wild card characters.
When do you use a LIKE statement?
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.
What is the meaning of underscore ( ‘_’ ) in the LIKE statement?
Match for any single character.
What do you accomplish by GROUP BY … HAVING clause?
GROUP BY partitions the selected rows on the distinct values of the column on which you group by.
HAVING selects GROUPs which match the criteria specified
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;
What is the result of this query if no rows are selected?
SELECT SUM (SALARY) FROM EMP WHERE QUAL=‘MSC’; NULL
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.
What are correlated subqueries?
A subquery in which the inner (nested) query refers back to the table in the outer query. Correlated subqueries must be evaluated for each qualified row of the outer query that is referred to.
What is a cursor? Why should it be used? –
Cursor is a programming device that allows the SELECT to find a set of rows but return them one at a time.
Cursor should be used because the host language can deal with only one row at a time.
How would you retrieve rows from a DB2 table in embedded SQL?
Either by using the single row SELECT statements, or by using the CURSOR.
Apart from cursor, what other ways are available to you to retrieve a row from a table in embedded SQL?
Single row SELECTs.
Where would you specify the DECLARE CURSOR statement?
See answer to next question.
How do you specify and use a cursor in a COBOL program?
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.
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.
Is DECLARE CURSOR executable?
No.
Can you have more than one cursor open at any one time in a program?
Yes.
When you COMMIT, is the cursor closed?
Yes.
What are the advantages of using a PACKAGE?
1. Avoid having to bind a large number of DBRM members into a plan
2. Avoid cost of a large bind
3. Avoid the entire transaction being unavailable during bind and automatic rebind of a plan
4. Minimize fallback complexities if changes result in an error.
What is a collection?
a user defined name that is the anchor for packages. It has not physical existence. Main usage is to group packages.
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?
100 ( for successful completion of the query ), 0 (for successful COMMIT if AUTOCOMMIT is set to Yes).
How would you print the output of an SQL statement from SPUFI?
Print the output dataset.
How do you pull up a query which was previously saved in QMF ?
Looks like index page split has occurred. DO a REORG of the indexes.
What is dynamic SQL?
Dynamic SQL is a SQL statement created at program execution time.
When is the access path determined for dynamic SQL?
At run time, when the PREPARE statement is issued.
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?
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.
How does DB2 store NULL physically?
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.
How do you retrieve the data from a nullable column?
Use null indicators. Syntax … INTO :HOSTVAR:NULLIND
What is the picture clause of the null indicator variable?
S9(4) COMP.
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
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
What is RUNSTATS?
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.
When will you chose to run RUNSTATS?
After a load, or after mass updates, inserts, deletes, or after REORG.
Give some example of statistics collected during RUNSTATS?
# of rows in the table
Percent of rows in clustering sequence
# of distinct values of indexed column
# of rows moved to a nearby/farway page due to row length increase
What is REORG? When is it used?
REORG reorganizes data on physical storage to reclutser rows, positioning overflowed rows in their proper sequence, to reclaim space, to restore free space. It is used after heavy updates, inserts and delete activity and after segments of a segmented tablespace have become fragmented.
What is IMAGECOPY ?
It is full backup of a DB2 table which can be used in recovery.
When do you use the IMAGECOPY?
To take routine backup of tables
After a LOAD with LOG NO
After REORG with LOG NO
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.
What is CHECK PENDING ?
When a table is LOADed with ENFORCE NO option, then the table is left in CHECK PENDING status. It means that the LOAD utility did not perform constraint checking.
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 and after any IMAGECOPY to maintain consistency.
What is a clustering index?
Causes the data rows to be stored in the order specified in the index. A mandatory index defined on a partitioned table space.
How many clustering indexes can be defined for a table?
Only one.
What is the difference between primary key & unique index?
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.
Unique index: a physical object that stores only unique values. There can be one or more unique indexes on a table.
What is sqlcode -922?
Authorization failure
What is sqlcode -811?
SELECT statement has resulted in retrieval of more than one row.
What does the sqlcode of -818 pertain to? –
This is generated when the consistency tokens in the DBRM and the load module are different.
Are views updateable?
Not all of them. Some views are updateable e.g. single table view with all the fields or mandatory fields. Examples of non-updateable views are views which are joins, views that contain aggregate functions (such as MIN), and views that have GROUP BY clause.
If I have a view which is a join of two or more tables, can this view be updateable?
No.
What are the 4 environments which can access DB2?
TSO, CICS, IMS and BATCH
What is an inner join, and an outer join?
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 on e table with the entire clause in corresponding columns of the other table or tables. These kinds of join which involve a match in both columns are called inner joins.
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.
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
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.
What is filter factor?
One divided by the number of distinct values of a column.
What is index cardinality?
The number of distinct values a column or columns contain.
What is a synonym?
Synonym is an alternate name for a table or view used mainly to hide the leading qualifier of a table or view. A synonym is accessible only by the creator.
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.
What do you mean by NOT NULL WITH DEFAULT? When will you use it?
This column cannot have nulls and while insertion, if no value is supplied then it wil have zeroes, spaces or date/time depending on whether it is numeric, character or date/time.
Use it when you do not want to have nulls but at the same time cannot give values all the time you insert this row.
What do you mean by NOT NULL? When will you use it?
The column cannot have nulls. Use it for key fields.
When would you prefer to use VARCHAR?
When a column which contains long text, e.g. remarks, notes, may have in most cases less than 50% of the maximum length.
What are the disadvantages of using VARCHAR?
1. Can lead to high space utilization if most of the values are close to maximum.
2. Positioning of VARCHAR column has to be done carefully as it has performance implications.
3. Relocation of rows to different pages can lead to more I/Os on retrieval.
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.
First CREATE MANAGER table with EMP# as the primary key. Then ALTER it to define the foreign key.
When is the authorization check on DB2 objects done - at BIND time or run time?
At run time.
What is auditing?
Recording SQL statements that access a table. Specified at table creation time or through alter.
0 comments:
Post a Comment