Date post: | 10-Apr-2018 |
Category: |
Documents |
Upload: | anuthangavelnpt |
View: | 238 times |
Download: | 0 times |
of 87
8/8/2019 DB2 Basic Manual
1/87
DB2 Basic
Material
- 1 -
8/8/2019 DB2 Basic Manual
2/87
INTRODUCTION TO A RELATIONAL
DATABASE
- 2 -
8/8/2019 DB2 Basic Manual
3/87
What is a Data base Management System.
A Database management system (DBMS) is a software package that manages data storedin databases. You use a DBMS to control and use the information stored on your computersystem more effectively and efficiently than you can with standard file processing. TheDBMS is an additional layer of software between the program and the stored data .Its theDBMS rather than the application program that has a complete picture of the physical andthe logical structure of the data stored.
In a relational system Data is perceived by users as a collection of tables called relations
Operators generate new tables from old ones The rows are referred to as tuples
Columns are referred to as attributes
Each row in a relation is distinct i.e duplicate rows are not allowed
Each relation must have a primary key, an attribute or a combination of attributeswhose value uniquely identifies the rows.
A relation may also contain foreign keys which reference other relations in a database.
Entire information content of the database is represented as explicit data values (asexplicit values in column positions within rows of a table)
DB2
IBMS Database 2 , commonly referred to as DB2, became generally available in the fallof 1983. DB2 was marketed as a relational database management system for the MVSoperating system. It uses tables to represent the data that it manages .
HOW DOES DB2 ORGANIZE AND ACCESS INFORMATION.
Db2 is a database management system. A more complete description is that db2 is arelational database management system that uses the industry standard Structured Query
Language (SQL) as a central component . A relational database system presents allinformation in the form of tables. A table is just a two dimensional array with horizontalrows and vertical columns. The intersection of a row and a column is a value .A valuemay be a text string , a number or nothing (null value).The name relational database comes from the technical term for a table , a relation . Inaddition , the formal names for the elements of a relation are tuple (row) andattribute(column). But these arent the only terms. A row is called a record and a columnis called a field.Although files and tables have similar structures ,they are processed differently . instandard file processing , the unit of processing is a an individual record while in db2 it isan entire table ,which may have one ,many or no rows.
- 3 -
D4
8/8/2019 DB2 Basic Manual
4/87
- 4 -
DB2 ARCHITECTURE
DB2 as perceived by an individual User
SQL
user
VIEW1 VIEW2
Base
Table B1B2 B3 B4
VSAM
Dataset
D1
D2D3 D4
Virtual
Real
Stored
8/8/2019 DB2 Basic Manual
5/87
DB2 OBJECTS
- 5 -
8/8/2019 DB2 Basic Manual
6/87
STORAGE STRUCTURE
The total collection of stored data is divided up into a number of disjoint databases . Each
database is divided into a number of disjoint spaces several table spaces and indexspaces. A space is a dynamically extendable collection of pages ,where a page is a block of
physical storage (it is a unit of i/o i.e the unit transferred between primary and secondarystorage in a physical i/o operation. The pages are given size of either 4k or 32 k. Eachtable space contains one or more stored tables . a stored table is is a physical representationof a base table .
DATABASE:
A Database in DB2 is a collection of logically related objects that is a logical collection
of stored tables that belong together in some way ,together with their associated indexesand the various spaces that contain those tables and indexes . it thus contains a set of tablespaces (each containing one or more stored tables ) together with a set of index spaces(each containing exactly one index). A given stored table and all its associated indexesmust be wholly contained within a single database. Objects are grouped together into thesame database primarily for operational reasons .Tables can be moved from one databaseto another without having any logical impact on user or user programs.
TABLE SPACES:
A table space can be thought of as a logical address space on secondary storage that is use
to hold one or more stored tables (logical because it is typically not just a set of physicallyadjacent areas) . As the amount of data in those table grows (or as the number increases),so storage will be acquired from the appropriate storage group and added to the table spaceaccordingly. One table space can be approx. 64 billion bytes in size and there is effectivelyno limit to the number of table spaces in a database ,nor to the no. of databases .Fundamentally the table space is the storage unit for recovery and re-organization
purposes; i.e. it is the unit that can be recovered via the recover utility . If the table spaceis very large , however , recovery and re-organization could take a very long time.DB2therefore provides the option to partition a large table space into simple , partitioned andsegmented forms.A simple table space can contain more than one stored table , though one is usually the
best option. One reason for having more than one is that stored records for different storedtables can be clustered together in such a way as to improve access times to logicallyrelated data. In particular , certain join queries would then be handled more efficientlysince the I/O operations would be reduced.Partitioned table spaces are intended for stored tables that are sufficiently large. A
partitioned table space thus contains exactly one stored table , partitioned in accordancewith value ranges of a partitioning columns or column combination. Individual partitionsof a partioned table space are independent of each other in the sense that they can beindependently recovered or reorganized.They can also be associated with different storage groups i.e. it is possible to store
different partitions on different devices and thereby spread the table space I/O load.
- 6 -
8/8/2019 DB2 Basic Manual
7/87
SEGMENTED TABLE SPACE:
Like simple table spaces, they can contain any number of stored tables ;unlike simple tablespaces , however, segmented table spaces do not support any kind of cross tableclustering- that is they do not allow records for different stored tables to be interleaved on
a single page. Instead , they keep the tables physically separated.
INDEX SPACE
An index space is to an index what a table space is to a stored table. However, since thecorrespondence between indexes between indexes and index spaces is always one to one,there are no data definition statements for index spaces; instead, the necessary index space
parameters are specified on the corresponding index definition statements. Thus for e.g.there is no create index space; instead the index space is created automatically when thecorresponding index is created. Like Table spaces ,index spaces can be reorganized and
recovered independently.
INDEX
Index in db2 are based on a structure known as B-tree. A b-tree is a multilevel, treestructured index with the property that the tree is always balanced; i.e. all its leaf entries inthe structure are equidistant from the root of the tree, and this property is maintained asnew entries are inserted into the tree and existing entries are deleted. As a result, the index
provides uniform and predictable performance for retrieval operations. A given table canhave any number of associated indexes. Also if the installation follows ourrecommendation that every base table have a primary key, then every stored table will
have at least one index, namely the primary index.To perform an exhaustive search on a given stored table according to a given index, thedata manager will access all records in that stored table in the sequence defined by thatindex and since that sequence may be quite different from the stored tables physicalsequence, a given data page might be accessed many times. It follows that an exhaustivesearch via a physical sequence unless the index concerned is a clustering index for whichthe sequence defined by the index is the same as, or close to, the physical sequence. Theindex is used to control the physical placement of the indexed records. Clustering indexesare extremely important for optimization purpose. The optimizer will try to choose anaccess path that is based on a clustering index, if one is available, and if clusteringsequence is appropriate for the sql under consideration.
- 7 -
8/8/2019 DB2 Basic Manual
8/87
STORAGE GROUPS:
A storage group is a named collection of direct access volumes, all of which are the samedevice type. Each table space and each index space normally has an associated storage
group. When storage is needed for the space or partition , it is taken from the specifiedstorage group. Within each Storage group, spaces and partitions are stored using VSAMlinear data sets .
SYNONYM:Is an alternate name for a base table or view. Each user can assign his or her ownsynonyms for any table or view that was created by some other fully qualified name.
VIEW:
It is a predefined selection of data in base tables. It is a virtual table that does notphysically exist but is processed as a table. It is derived from one or more base tables, orviews or combinations of views and tables. The view definition is stored in the DB2catalog. Changes to the data in the view can change the data in the base table.
CATALOG :
Contains information about every DB2 object that is maintained by it. Whenever a DB2object is created, dropped or altered by any ddl statement or whenever control over the
object is changed by any dcl statement of sql, an entry is made in the appropriate catalogtable. Catalog table can be accessed using sql, they however cannot be updated by auser(but done so by the system).To retrieve information from the DB2 catalog, the select privilege on the catalog isneeded. Some of the catalog tables are :
SYSTABLES
SYSSTOGROUP
SYSTABLESPACE
- 8 -
8/8/2019 DB2 Basic Manual
9/87
COMPONENTS OF DB2
- 9 -
8/8/2019 DB2 Basic Manual
10/87
SYSTEM STRUCTURE:
The major components of DB2 are SYSTEM SERVICES
LOCKING SERVICES
DATABASE SERVICES
- 10 -
RELATIONAL DATABASE SYSTEM
MVS OPERATING SYSTEM
TSO
Subsystem
IMS
Subsystem
CICS
Subsystem
DB2 Subsystem
RESOURCE
LOCK
MANAGER
Isolate Users
Locking Services
DATABASE
SERVICES
Access Data
Create Objects
DB2 Catalog
SYSTEM
SERVICES
Perform logging
Manage Log
Inventory Manage
Threads
8/8/2019 DB2 Basic Manual
11/87
SYSTEM SERVICES:
This component handles all system wide tasks including connections to other MVSsubsystems.
It also handles system startup and shut down operator communication.
Managing the system log : The system log is a set of predefined disk data sets that areused to record information for recovering user and system data in the event of a failure.When an active log data set becomes full (or on operator command ), DB2 switches toa new data set and copies the old one to an archive log data set on disk or tape. Whenthe active data sets are full, they are recycled. Information regarding the data setsthemselves is recorded in the BOOT STRAP DATA SET (BSDS).
Gathering system wide statistics, performance and accounting information.
LOCKING SERVICES:
Provided by an MVS subsystem called the IMS Resource Lock Manager (IRLM). Despitethe IMS in its name , the IRLM does not really have anything to do with it. It is ageneral purpose lock manager which controls the concurrent access to db2 data.
DATABASE SERVICES:
The primary purpose of the database component is to support the definition , retrieval andupdate of database data- to implement the functions of the sql. The necessary support is
provided by a series of five sub components :
Precompile
Bind
Runtime Supervisor
Data Manager
Buffer Manager
Together these components support the preparation of the application programs forexecution and the subsequent execution of those programs. The functions of the individual
components are as follows:
PRECOMPILER
This component is a preprocessor for the host language. Its function is to analyse the hostlanguage source module stripping out all the sql statements it finds and replaces them bythe host language CALL statements. From the sql it encounters, the precompiler constructsa database request module which becomes the input to the bind component.
- 11 -
8/8/2019 DB2 Basic Manual
12/87
THE PROCESS:
Searches for and expands DB2 related include members .
Searches for sql statement in the body of the programs source code .
Creates a modified version of the source program in which every sql statement in the
program is commented out and a call to the db2 runtime interface module along withapplicable parameters , replaces each original sql statement.
Extracts all sql statements from the program and places them in a dbrm to ensure thatthese two items are inextricably tied.
Reports on the success or failure of the precompile process.
The precompiler searches for sql statements embedded in EXEC SQL and END-EXEC.
- 12 -
PRECOMPILATION
It uses the SQL statements to build a DBRM for P, which it
stores away as a member of an MVS partitioned data set
DBRM contains a copy of the original SQL statements,
together with additional information
COBOL SOURCE
MODULE P
PRECOMPILER- Source listing
- Diagnostics
- Cross-References
etc.
-
-
-
CALL Long-Interface
--
-
-Modified COBOL
Source Module P.
-
-
-
- DBRM for P
(SQL statements, etc.)
8/8/2019 DB2 Basic Manual
13/87
BIND:
This component is the compiler for the sql statements. It reads the sql statements from theDBRMS and produces a mechanism to access data as directed by the sql statements being
bound. The bind plan accepts as input one or more DBRMs produces from the previousDB2 program precompilation. The output of the bind plan is an application plancontaining executable logic representing optimized access paths to DB2 data. Anapplication plan is executable only with a corresponding module. Before u can run a DB2
program, regardless of the environment, an application plan name must be specified.The major functions of bind are:
Parsing and syntax checking
Optimization
Authorization
Check that DB2 tables and columns being accessed conforms to the correspondingDB2 catalog information.
BIND PLAN vs. BIND PACKAGE:
In some cases however, a DBRM may not be bound directly into a plan. Instead it mayfirst be bound into a package and then finally, the packages may be bound into a plan.There are certain disadvantages of directly binding the DBRMs into an application plan :
If an individual DBRM needed to be recompiled for any reason(for eg some index wasdropped),the entire plan had to be recompiled and rebound.
If multiple plans involved the same DBRM , that same DBRM had to be compiledmultiple times and , if that DBRM ever needed to be recompiled , then all relevant
plans had to be recompiled and rebound in their entirety.
Adding a new DBRM to an existing plan required (again) a recompilation and rebindof the entire plan.
Partly as a consequence of the foregoing points, bind and rebind times are becomingunacceptably high in some DB2 installations and availability was suffering as a result.
The package concept was introduced to remedy these deficiencies. If a given DBRMneeds to be recompiled, all that has to be done is an appropriate package bind it is notnecessary to recompile the entire plan. Indeed, it may not be necessary to do a new plan
bind to incorporate the new package either.
- 13 -
8/8/2019 DB2 Basic Manual
14/87
RUNTIME SUPERVISOR:
The runtime supervisor is resident in main memory when the application program is
executing. Its job is to oversee that execution. When the application program requestssome database operation to be performed (wishes to execute some sql), control first goesto the runtime supervisor, which uses control information in the application plan to requestthe appropriate operations on part of the data manager.
DATA MANAGER:
The data manager can be thought of as a very sophisticated access method. It performs allof the normal access method functioning such as search, retrieval, update , index
maintenance etc. Broadly speaking, the data manager is the component that manages thephysical database(s). It invokes other system components as necessary in order to performdetailed functions such as locking, logging, I/O operations etc during the performance ofits basic task.
BUFFER MANAGER:
The buffer manager is the component responsible for physically transferring data betweenexternal storage and virtual memory. It employs sophisticated techniques to get the best
performance out of the buffer pools under its care and to minimize the amount of physicalI/O actually performed.
- 14 -
DB2 Application Programs Preparation & Execution :-
(Control Flow Diagram Overview)
Linkage
Editor
Modified
Source
Module
Object
Module
Compiler
DBRM
Appln.
Plan
BIND
(Load Module)
application plan
Runtime
Supervisor
Data
Manager
Buffer
Manager
Load
Module
DB
Source
Module
Precompiler
8/8/2019 DB2 Basic Manual
15/87
DATATYPE FUNCTIONS
AND
OPERATORS
- 15 -
8/8/2019 DB2 Basic Manual
16/87
INTRODUCTION:
The basic data object is the scalar value ;the object appearing at the intersection of a givenrow and a given column of a given table. Each scalar value is of some scalar data type. Foreach such data type, there is also an associated format for writing literals of that type.Scalar objects can be operated upon by means of certain scalar operators. DB2 also
provides certain scalar functions which can be regarded as scalar operators. Scalar objectsand operators can be combined to form scalar expressions.
- 16 -
DB2 DATATYPES
DATA TYPE DESCRIPTION VALURANGE STORAGE
REQUIRED
INTEGER Full word
binary integer 1 - 2 billion 4 bytes
SMALL INT Half word
binary integer 1-32767 2 bytes
DECIMAL(p,q) p-total no. of 0
8/8/2019 DB2 Basic Manual
17/87
- 17 -
DB2 DATATYPES
DATA TYPE DESCRIPTION STORAGE RANGE
REQUIRED VALUE
VARCHAR(n) Variable length 0
8/8/2019 DB2 Basic Manual
18/87
- 18 -
CONSTANTS & LITERALS
Integer : Signed or unsigned decimal
integer with no decimal point
Eg : 4 -15 +364 0
Decimal : Signed or unsigned decimal
number with decimal point
Eg : 40 -95.7 +364.05 0.0007
Float : Written as a decimal constant,followed by a character E,
followed by an integer constant
Eg : 4E3 -95.7E46 +364E.5 07E1
Character : Written either as a string of characters
string enclosed in a single quotes or a string of pairs
hexadecimal digits enclosed in single quotespreceded by letter X.
Eg : 123MAIN St.
PIGON
X FIF2F3F40D156247
Graphic: : Written as a string of double byte characters
string preceded by the character the whole enclosed in single quotes and
preceded by letter G.
Eg : G
8/8/2019 DB2 Basic Manual
19/87
NUMERIC OPERATORS:
ARE : +,-,*,/
FUNCTIONS:
CONCATENATION ( ): can be used to concatenate two character strings or twographic strings. E.g (INITIALS || LASTNAME)
CHAR: Converts a date, time ,timestamp to its character string representation.
DATE: Converts a scalar value to a date.
DAY: Extracts the day portion of a date or a timestamp.
DAYS: Converts a date or timestamp to a number of days.
DECIMAL: Converts a number to decimal representation.
DIGITS: Converts a number(decimal or integer) to a character string representation.
FLOAT: Converts a number to a floating point representation.
HEX: Converts a scalar value to a character string representing a internal HEX code
HOUR: Extracts the hours portion of a time or timestamp.
INTEGER: Converts a number to integer representation.
LENGTH: Computes the length of the scalar value in bytes .
MICROSECOND: Extracts the microsecond portion of a timestamp.
MINUTE: Extracts the minute portion of a date or timestamp.
SECOND: Extracts the second portion of a date or timestamp.
SUBSTR: Extracts a part of the string from a string.Assume = AARTIe.g. SUBSTR(SNAME,1,3) would extracts AAR
TIME: Converts a scalar value to time.
TIMESTAMP: Converts either (a) Single scalar value or(b) a pair of scalar values (representing
a date and time resp.) to a timestamp
VALUE: Converts a null value into a non null value.e.g. Select custno,fname,lname,value((H):||homeph,(w):|| workph, no phone)
- 19 -
8/8/2019 DB2 Basic Manual
20/87
VARGRAPHIC: Converts a character sting to a graphic string.
YEAR: Extracts the year portion of a date or timestamp.
SPECIAL REGISTERS
Each individual user is assigned an authorization ID. That id is used to sign on to thesystem, and serves as the primary id for the user in question. Tables and other objects thatare purely private to that user will typically be created under the control of, and hence beowned by the primary id.
Each functional area(eg each department) in the organization is also assigned anauthorization id. However, that id is typically not given the sign-on authority; users signon to the system under their primary id. Once signed on, users can operate under their
primary id or using the sql statement (SET CURRENT SQLID) they can switch to asecondary id. An external subsystem such as IBMs RACF keeps track of the secondaryid(s) that can legitimately be used by a given primary id. A given primary id can have anynumber of secondary ids also that the same secondary id can be used by any number of
primary ids.
SCALAR EXPRESSIONS
Six types of scalar expression are
Numeric
Character String
Graphic String
Date
Time
Timestamp expressions
Assignments :Assignment operations are performed when values are retrieved from the DataBase orstored into the DataBase.
Comparisons :The general form of a comparison is
Comparand operator comparand where
Two comparands must be compatible
The operator is any of the following> , = ,
8/8/2019 DB2 Basic Manual
21/87
Examples
WEIGHT * 454=1000SUBSTR (PNAME,1,1) = CFINAL_DATE = REVIEW DATE - CURRENT DATESUM (QTY) =500
NULLS :
Means missing information. The problem with missing information is that is frequentlyencountered in the real world. For example , historical records sometimes include suchentries as date of birth not known:; meeting agendas often show a speaker as to beannounced and police records may have entries as present whereabouts not known.Hence it is desirable to have some way of dealing with such situations in our formaldatabase systems. SQL systems like DB2 represent missing information by means of
special markers called NULLS. For e.g. we might say loosely that the weight of some partis null. What we mean by such a statement is that (a) the part exists (b) of course , it has aweight but (c) we do not know what that weight is. Instead we mark that slot as null.In general any column can contain nulls unless the definition of that column explicitlyspecifies NOT NULL. If a given column is allowed to contain nulls, and a row is insertedinto the table and no value is provided for that column, DB2 will automatically place a nullin that position.In DB2, a column that can accept nulls is physically represented in the stored database by
two columns, the data column itself and a hidden indicator column ,one byte wide ,that isstored as a prefix to the actual data column.
NOT NULL WITH DEFAULT means that the column in question cannot contain NULLS,
but that it is nevertheless still legal to omit a value for the column on insert. If a row isinserted and no value is provided for some column on which not null with defaultapplies,DB2 automatically places one of the following default values in that position:
Zero for numeric columns
Blanks for fixed length columns
Empty (zero-length string) for varying length string columns.
- 21 -
8/8/2019 DB2 Basic Manual
22/87
EMBEDDED SQL
- 22 -
8/8/2019 DB2 Basic Manual
23/87
EMBEDDED SQL:
Any SQL statement that can be used at the terminal can also be used in an applicationprogram. There are various differences of detail between a given interactive SQLstatement and its corresponding embedded form.
Embedded SQL statements are prefixed by EXEC SQL and are terminated by END-EXEC
SQL statements can include references to host variables
Any tables (base tables or views) used in the program must be declared
After any SQL statement has been executed, feedback information is returned to theprogram in an area called the SQL Communication Area (SQLCA)
The embedded SQL SELECT statement requires an INTO clause specifying the hostvariables.
- 23 -
8/8/2019 DB2 Basic Manual
24/87
HOST VARIABLES
- 24 -
8/8/2019 DB2 Basic Manual
25/87
HOST VARIABLES:Host variables are variables declared in the working storage section and are used by DB2when it moves data between your program and a table. They are defined according to the
rules of programming language.Host variables can appear in SQL data manipulation statements. They are generally usedfor designating a target for retrieval. Such variables can appear in the following positions :
Into clause in select or fetch (target for retrieval)
Where or having clause (value to be compared)
Set clause in update (value to be assigned)
Values clause in insert (value tobe inserted)
Example
EXEC SQL
Select status , city into :status , :cityfrom employeewhere sno = :given-sno;
END-EXEC
SQL statements can include references to host variables, such references are prefixed witha colon to distinguish them from SQL column names
- 25 -
8/8/2019 DB2 Basic Manual
26/87
INDICATOR VARIABLES
- 26 -
8/8/2019 DB2 Basic Manual
27/87
INDICATOR VARIABLE
In general if there is a chance that the source or a retrieval operation might be null, the usershould include an INDICATOR VARIABLE in the into clause in addition to the targetvariable as illustrated in the following example:
EXAMPLE :
EXEC SQL
Select status , cityInto :status:status-ind, :city:cityindFrom sWhere sno = :Given-Sno
END-EXEC.
For processing and finding out whether the fields were NULL or not, a check can beperformed as :
IF STATUS-IND < 0 THEN /*STATUS WAS NULL */
Indicator variables can be used in the VALUES clause to insert NULL VALUES.
EXAMPLE: -
IF COLORIND < 0 OR CITYIND < 0
EXEC SQL
INSERT INTO P (PNO,COLOR, CITY)VALUES(:PNO,:PCOLOR:COLOR-IND, :PCITY:CITYIND)
END-EXEC.
Each indicator variable is a half-word integer (pic S9(4) comp).
If defined as an array (occurs clause) ,then it may be used for a list of columns with thefirst occurrence of the indicator variable corresponding to the first column in that listusing a single indicator variable, Nulls can be handled for all the fields of the table.
The indicator variable will have a negative value if the select returns a null value.
If the indicator variable returns a value > 0, it indicates a truncated variable is thelength of the character string before truncation
If the Indicator variables contain :
A negative No. indicates that the column has been set to Null
The value 2 indicates that the column has been set to Null as a result ofData Conversion Error
A positive or a zero value indicates that the column is not Null
- 27 -
8/8/2019 DB2 Basic Manual
28/87
If a column, defined as a char data type,is truncated on retrieval because the hostvariable is not large enough, the indicator Variable, contains the original length of thetruncated column
EXAMPLE: -
01 DEPT-INDICATORS.10 DEPT-IND OCCURS 5 TIMES PIC S9(4) COMP.
EXEC SQLSELECT DEPTNO, DNAME, MGR, HO, LOC
INTO :DCLDEPT: DEPT-INDFROM DEPTWHERE DEPTNO = A00
END-EXEC
Indicator variables can appear on the right hand side of an assignment in the SETclause to set a value to NULL.
EXAMPLE: -
IF RANKIND = < 0
EXEC SQLUPDATE SSET STATUS = :STATUS:STATUS-INDWHERE CITY =LONDON
END-EXEC.
- 28 -
8/8/2019 DB2 Basic Manual
29/87
CURSORS
- 29 -
8/8/2019 DB2 Basic Manual
30/87
CURSORS:
A mechanism allowing an application program to retrieve a set of rows. The cursor facilityallows a COBOL program to gain addressability to individual row occurrences of a many-rows result table
Following steps to be performed to use cursors
Declare cursor : to define cursor
Open cursor : to create result table
Fetch cursor : to retrieve rows from cursor, one at a time to beexecuted in a loop
Close cursor : to close the cursor
Declaring (defining) a cursor is done in the data division of your program.This is purely declarative in nature. Therefore no information is being retrieved from thedatabase yet. The Cursor name should begin with a letter and must not exceed 18characters.When the open statement is encountered the select in the cursor declaration is executed.The open cursor statement not only executes the selection of data from the DB2 database
but also establishes the initial position of the cursor in the results table. One program canhave multiple cursors.
Declaring a CursorEXEC SQL
DECLARE cursor CURSOR FOR SELECT col1, col2.... FROM tableWHERE condition [FOR UPDATE OF col1, col2 ..]
END-EXEC
Opening a CursorEXEC SQL
OPEN cursorEND-EXEC
Fetching a row from cursor :EXEC SQL
FETCH cursor INTO : host var1, :host var2,...END-EXEC.
Closing a cursor :EXEC SQL
CLOSE cursorEND-EXEC.
- 30 -
8/8/2019 DB2 Basic Manual
31/87
Example-Based on a Single Table
Operations Involving Cursors :-
EXEC SQLDECLARE X CURSOR FORSELECT S#, SNAME, STATUS
FROM S WHERE CITY = :YEND-EXEC.
EXEC SQLOPEN X
END-EXEC.
LOOP UNTIL NO MORE ROWS OR ERROR
EXEC SQLFETCH X INTO :S#,:SNAME.:SNAME-IND
END-EXEC.
PROCESSING STATEMENTS .
EXEC-SQLCLOSE X
END-EXEC.
- 31 -
8/8/2019 DB2 Basic Manual
32/87
DATA MODIFICATION:
Often an application program must read data and then based on its value, either update ordelete data. One can use the UPDATE or DELETE SQL statements to modify and delete
rows from DB2 tables. These statements are similar to select statements which operate ona set of data at any given point of time.
This is accomplished with a cursor and a special clause of the update and delet statementsusable only by embedded SQL namely :WHERE CURRENT OF. The cursor is declaredwith a special FOR UPDATE OF CLAUSE.
A for update ofclause appears with:
Select statement to indicate what columns can be updated when retrieved
The columns to be updated must be listed in the for update of clause or the declare You do not have to select a column to update it
Rules for Update
The select statement must be on a single table and not on a join
If the declare cursor statement contains a subquery it must not be on the same table asthe main query
You cannot use distinct, group by, order by or built-in functions
Only those columns are eligible for updation which are selected in the for.. updateclause
The current forms of UPDATE & DELETE
EXEC SQLUPDATE tableSET field = : exp [, field = : exp]WHERE CURRENT OF cursor
END-EXEC.
EXEC SQLDELETE FROM tableWHERE CURRENT OF cursor
END-EXEC.
- 32 -
8/8/2019 DB2 Basic Manual
33/87
Example: (Use of Cursor)
EXEC SQL
Declare C1 cursor forSelect Deptno, Deptname, Mgrno From DeptWhere ADMRDEPT = :ADMRDEPTfor update of MGRNO
END-EXEC.
PROCEDURE DIVISION.
MOVE A00 TO ADMRDEPT.
EXEC SQL
OPEN C1END-EXEC.
PERFORM 200-MODIFY-DEPT-INFO UNTIL NO-MORE-ROWS.
EXEC SQL
CLOSE C1END-EXEC.
200-Modify-Dept-Info.
EXEC SQL
Fetch C1 into :deptno, : deptname, : mgrnoEND-EXEC.
If sqlcode < 0GO TO 9999-error-paragraph.
If sqlcode = +100Move NO to more-rows
Else
EXEC SQLUpdate Dept Set MGRNO = 00000Where current of C1
END-EXEC.
- 33 -
8/8/2019 DB2 Basic Manual
34/87
DATA RETRIEVAL
Pseudocode for retrieving data from an application join ,
using cursors:EXEC SQL
Declare Deptcur cursor forSelect Deptno, DeptnameFrom Dept
END-EXEC.
EXEC SQL
Declare Empcur cursor forSelect empno, salary from emp
where workdept = :hv-workdeptEND-EXEC.
EXEC SQL
Open DeptcurEND-EXEC.
Loop until no more dept rows or error
EXEC SQL
Fetch deptcur into :Deptno , :Deptname
END-EXEC.
Move deptno to HV-WORKDEPT
EXEC SQL
Open EmpcurEND-EXEC.
Loop until no more employee rows or error
EXEC SQL
Fetch empcur into:empno, :salary
END-EXEC.
Process retrieved dataEnd Loop (2)End of Loop (1)
- 34 -
8/8/2019 DB2 Basic Manual
35/87
WITH HOLD OPTION:
This is an optional specification on a cursor declaration. The significance can beunderstood by considering what happens in its absence.
Suppose we need to process some large table, one row at a time by means of a curso andupdate a few of the as we go. It is often desirable to divide the work up into batches and tomake the processing of each batch into a separate transaction (by issuing a separatecommit at the end of each one); thus, e.g. a table of one million rows might be processed
by a sequence of 10,000 transactions, each one dealing with just 100 rows. This way , fore.g. if it becomes necessary to roll a given transaction back, then at most 100 updates willhave to be undone, instead of potentially as many as a million.
The problem with this approach ,however is that every time we issue a commit, weimplicitly close the cursor, thereby losing our position within the table. The first thing eachtransaction has to do, therefore is to execute some re-positioning code in order to get backto the row that is due to be processed next. And that re-positioning code can often be quitecomplex, especially if the processing sequence is determined by a combination of severalcolumns.
If the cursor declaration specifies with hold, however, commit does not close the cursor,instead, leaves it open, positioned such that the next FETCH will move it to the next rowin sequence. The possibly complex code for repositioning is thus no longer required.
However it is important to note that the first operation on the cursor following the commitmust be fetch. Update and delete current are illegal.
- 35 -
8/8/2019 DB2 Basic Manual
36/87
ERROR / EXCEPTION
HANDLING
- 36 -
8/8/2019 DB2 Basic Manual
37/87
WHENEVER:
The whenever statement has the syntax :
EXEC SQL
WHENEVER condition actionEND-EXEC.
Where condition is one of the following :
NOT FOUND means SQLCODE = 100
SQLWARNING means SQLCODE >0 & NOT = 100
SQLERROR means SQLCODE < 0
ACTION specifies a CONTINUE or a GO TO statement.
The whenever statement causes the program to automatically check the sqlcode. Basedon the value it finds, it takes the action you specify. Some application programmers prefer(for structured reasons) to avoid the whenever statement, and use all specific errorchecking after each SQL statement is issued.If you omit the action for a whenever statement, the default of continue will apply for thatcondition.There is no limit to the no. of whenever statements you can use.
Example using WHENEVER
1000-INQUIRY.
EXEC SQLWHENEVER SQLERRORGOTO 1000-UNDO
END-EXEC.
EXEC SQL
SELECT FLD1, FLD2 INTO : FLD1, :FLD2FROM EMP-TABLE WHERE CODE = 113
END-EXEC.
1000-UNDO
DISPLAY ERROR! CANT PROCEEDEXEC SQL
ROLLBACKEND-EXEC.
1000-EXIT.
EXIT.
- 37 -
8/8/2019 DB2 Basic Manual
38/87
TRANSACTION PROCESSING
- 38 -
8/8/2019 DB2 Basic Manual
39/87
WHAT IS A TRANSACTION ?
A transaction is a logical unit of work. Consider the following example. Suppose for thesake of example the parts table contains an additional column totqty representing the totalquantity for the part in question. The value of the totqty for any part is supposed to beequal to the sum of all sp.qty values taken all over sp rows for that part. Now consider thefollowing sequence of operations, the intent of which is to add a new shipment(S5,P1,1000) to the database:
EXEC SQL
WHENEVER SQLERROR GOTO UNDOEND-EXEC.
EXEC SQL
INSERT INTO SP VALUES (S5,P1,1000)END-EXEC.
EXEC SQL
UPDATE PSET TOTQTY = TOTQTY + 1000WHERE P# = P1
END-EXEC
EXEC SQL
COMMIT WORKEND-EXEC.
UNDO:EXEC SQL
ROLLBACKEND-EXEC
FINISH:
RETURN
The insert adds the new shipment to the sp table, the update updates the totqty column forthe part p1 appropriately.
The point of example is that what is presumably intended to be a single atomic transactioncreate a new shipment infact involves 2 updates to the database. What is more is thatthe database is not consistent even between thise two updates. Thus a logical unit of workis not necessarily just one SQL operation ; rather it is a sequence of several suchoperations, in general, that transforms a consistent state of the database into another
consistent state, without necessarily preserving consistency at all intermediate points.Now it is clear that what must not be allowed to happen in the example is for one of the
- 39 -
8/8/2019 DB2 Basic Manual
40/87
two updates to executed and the other not (because then the database would be in aninconsistent state). Ideally we would want that both the updates are done, but we cannothave such a guarantee: there is always a chance that things will go wrong. For example asystem crash may occur between the two updates.
But a system that supports transaction processing does provide the next best thing to sucha guarantee. Specifically, it guarantees that if the transaction executes some updates andthen a failure occurs, for whatever reason before the transaction reaches its normaltermination, the those updates will be undone. Thus the transaction either executes in itsentirety or is totally cancelled.
The system component that provides this atomicity is know as the transaction manager andthe COMMIT WORK and ROLLBACK WORK are the keys to the way it works.
The COMMIT WORK operation signals successful end-of-transaction :it tells thattransaction manager that a logical unit of work has been successfully completed.
The ROLLBACK operation by constrast, signals unsuccessful end-of-transactionindicating an inconsistent state and all the updates made by the logical unit of work must
be rolled back or undone.
SYNTAX:
COMMIT
The SQL commit statement takes the formEXEC SQL
COMMIT [WORK]END-EXEC.
ROLLBACK
The SQL ROLLBACK statement takes the form
EXEC SQL
ROLLBACK [WORK]END-EXEC
- 40 -
8/8/2019 DB2 Basic Manual
41/87
CONCURRENCY AND LOCKING
- 41 -
8/8/2019 DB2 Basic Manual
42/87
THREE CONCURRENCY PROBLEMS:
DB2 is a shared system; i.e. it is a system that allows any number of transactions to access
the same database at the same time. Any such system requires some kind of concurrencycontrol mechanism to ensure that current transactions do not interfere with each othersoperation and DB2 includes such a mechanism namely locking.There are essentially three ways in which things can go wrong- three ways. They are
The lost update problem
The uncommited dependency problem
The incorrect analysis problemThe lost update problem:
Consider the situation as shown in the figure:
Transaction A retrieves some row R at time t1 ; transaction B retrieves the same row attime t2; transaction A updates the row(on the basis of the values seen at time t1) at time t3;and transaction B updates the same row (on the basis of the value seen at time t2, whichare the same as seen at time t1) at time t4. Transaction As update is lost at time t4,
becauses transaction B overwrites it without even looking at it.
- 42 -
The Lost Update Problem
TRANSACTION A TIME TRANSACTION B
- -
- -
FETCH R T1 -
- -
- T2 -
- FETCH R
UPDATE R T3 -
- -
- T4 UPDATE R
-
8/8/2019 DB2 Basic Manual
43/87
The uncommitted dependency problem:
This problem arises if one transaction is allowed to retrieve (or update) a row that has beenupdated by another transaction and has not yet been committed by that other transaction.For if it has not been committed, there is always a possibility that it never will becommitted but will be rolled back instead- in which case the first transaction will have datathat now no longer exists. Consider the figure given below:
In this example , transaction A sees an uncommitted update at time t2. That update is thenundone at time t3. Transaction A is therefore operating on false assumption that row R hasthe value as seen at time t2, where as in fact , it has whatever value it had prior to time t1.As a result, transaction A may well produce incorrect output.
Inconsistent analysis problem:
This problem could be a simple summation problem.
- 43 -
The Uncommitted Dependency Problem
TRANSACTION A TIME TRANSACTION B
- -
T1 UPDATE R
-
-
- -
FETCH R T2 -
- -
- -
- T3 ROLLBACK
- -TRAN A BECOMES DEPENDENT ON AN UNCOMMITTED CHANGE
AT TIME T2
8/8/2019 DB2 Basic Manual
44/87
How DB2 solves these problems:
DB2s concurrency control mechanism is based on a technique called locking. The basicidea of locking is simple: when a transaction needs and assurance that some of the object itis interested in typically a database row - will not change in some unpredictable mannerwhile its back is turned , it acquires a lock on that object and there by to prevent them fromchanging it. The first transaction is thus able to carry out its processing in the certainknowledge that the object in question will remain in stable state for as long as thattransaction wishes it to
The two types of locks that can be placed are shared lock(S) and exclusive lock (X). We
assume that if a transaction requests a lock that is currently not available , the transactionsimply waits until it is. In practice, the installation can specify a maximum wait time ; thenif any transaction ever reaches this threshold in waiting for a lock, it times out and thelock request fails ( a negative SQLCODE is returned)
From the compatibility matrix , two inferences can be drawn:
If transaction A holds an X lock on row R , then a request from transaction B for a lock ofeither type on R will cause B to go into a wait state. B will wait until As lock is released.
If transaction A hold a shared lock (S) lock on row R , thenA request from transaction B for an X lock on R will cause B to go into a waitstate (and B will wait until As lock is released);
A request from transaction B for an S lock on R will be granted (that is , B willnow also hold an S lock on R)
- 44 -
A
B X S
X N N
S N Y
COMPATIBILITY MATRIX
8/8/2019 DB2 Basic Manual
45/87
Transaction requests for row locks are always implicit. When a transaction successfullyfetches a row , it automatically acquires an S lock on that row. When a transactionsuccessfully updates a row, it automatically acquires and X lock on that row. If it alreadyholds an S lock on that row,as it will in FETCH/UPDATE or FETCH/DELETESEQUENCE, then the update or delete promotes the S lock to X level.
The lost update problem revisited
The above figure shows what would happen to the interleaved execution under the lockingmechanism of DB2. As one can see, transaction As update at time t3 is not accepted
because it is an implicit request for an X lock on R, and such a request conflicts with the Slock already held by transaction B; so A goes into a wait state. For analogous reasons , Bgoes into a wait state at time t4. Now both transaction are unable to proceed, so there is noquestion of any update being lost.DB2 thus solves the lost update problem by reducing itto another problem but at least it does solve the original problem. This problem is calledthe deadlock problem, discussed later.
- 45 -
Handling the Lost Update Problem
TRAN A TIME TRAN B
- -
FETCH R T1 -
(acquires S lock on R) - -
- -
- T2 FETCH R- (acquires S lock On R)
-
UPDATE R T3 -
(request X lock on R) --
Wait
Wait UPDATE RT4 (request for X lock on R)
WaitWait
8/8/2019 DB2 Basic Manual
46/87
The uncommitted dependency problem revisited
Transaction As operation at time t2 is not accepted , because it is an implicit request for alock on R, and such a request conflicts with the X lock already held by B; so A goes into await state and remains so until B reaches a synchpoint(either commit or rollback), whenBs lock is released and A is able to proceed; and at that point A sees a committed value(either the pre B value, if B terminates and with a ROLLBACK , or the post b valueotherwise. Either way, A is no longer dependent on an uncommitted update.
- 46 -
Handling the Uncommitted Dependency Problem
TRAN A TIME TRAN B
- -
- T1 UPDATE R
- (X lock on R)
- -
FETCH R -(request for S lock on R) T2 -
-
Wait
`
Wait SYNCPOINT
T3 (Release X lock
Wait on R)
resume : FETCH R T4
(X lock on R)`
8/8/2019 DB2 Basic Manual
47/87
Deadlocks .A 'deadlock' occurs when program A locks page X exclusively and attempts to
lock page Y, while program B has already locked page Y exclusively and attempts to lockpage X. Neither program can continue without the required lock. DB2 cancels one of theprocesses (the one with the fewest log records) with a time out code. To avoid thissituation, you can:
Use the same sequence of update. Both programs should advance along thetables in ascending key order; this makes it less likely that they will cross eachother's paths and attempt to lock the same page. Both programs should accessvarious tables in the same sequence, for the same reason.
Avoid clashing updates through different paths (if possible). The problemwith such updates can be exemplified as follows:User A is updating page 1 using index X for access to the data;User B is updating page 2 using index Y for access to the data.
However, each index also references the data on the other page (i.e., Index Xreferences data on page 2 and index Y references data on page 1).
Therefore, after the data is reached by means of one index, and after that data isupdated, each query must modify the other index so it would reflect the new,updated data. However, that other index is still locked by the other query.
Such deadlocks are hard to avoid. You may watch out for some conditionswhich make this situation more likely:
Large number of indexes on a table, with frequent deletes orupdates.
Multi-row updates and deletes.
Large value used for SUBPAGES for the index.
Use frequent COMMITs. The chance that a page needed byprogram A will already be held by program B is thereby reduced.
Use cursor with FOR UPDATE OF ... This technique locks with INTENTUPDATE, ahead of time, all the pages which you will need. This is a morerelaxed lock than an exclusive lock, but it still prevents other programs fromlocking one of your pages exclusively. Thus, less contention, fewer deadlocksand fewer time outs will occur.
- 47 -
8/8/2019 DB2 Basic Manual
48/87
Explicit locking facilities
In addition to the implicit locking mechanism described earlier, DB2 provides certainexplicit facilities which the programmer should be aware of. The explicit facilitiesconsists of : the SQL statement LOCK TABLE , the ISOLATION PARAMETER on the
bind plan command, the table space LOCKSIZE parameter and the ACQUIRE/RELEASEPARAMETER on the bind plan.
LOCK TABLE :
Depending on the versions, this command either locks up the table or an entire table space
SYNTAX
LOCK TABLE table IN mode MODE
Where mode is SHARE or EXCLUSIVE & table must designate a base table not aview.Once a lock is acquired no other transaction will be able to access any part of the table inany way - until the original lock is released. When that original lock is released depends on
the RELEASE parameter on BIND.The Lock table statement can be used to control locks from within a DB2 application
program. Every individual page lock uses system resources in storage and processing timeA single table lock reduces the storage and processing time required by the many smalllocks. This results in saving of system resources.
- 48 -
8/8/2019 DB2 Basic Manual
49/87
ISOLATION PARAMETER:
Specifies the isolation level for the application plan being bound.DB2 supports twoisolation levels for every transaction
Cursor Stability (CS)
Repeatable Read (RR)
These are used while cursor manipulation
Cursor Stability
DB2 takes a lock on the page the cursor is accessing and releases the lock on that page,
when the cursor moves onto a different page(This is not done when we use FOR UPDATE of statement in cursor)The lock on the last page is released at commit time (or at Thread de-allocation time)
Repeatable Read
In cursor stability, while your transaction reads data, other transaction could change thedata you have already read. In repeatable read, DB2 holds all page locks while the cursoris moving all till the transaction commits (or till thread deallocation).Cursor Stability provides more /higher concurrency while repeatable read provide moreconsistency.
The problem with CS is that a transaction operating at that level may have a recordchanged behind its back as in inconsistent analysis problem and so may produce awrong answer.
By contrast, a transaction that operates under isolation level RR, can behave completely as
if it were executing in a single user system.
- 49 -
8/8/2019 DB2 Basic Manual
50/87
LOCKSIZE PARAMETER:
Physically, DB2 locks data in terms of pages or tables or table spaces, depending on whatwas specified as the LOCKSIZE for the relevant table space in the CREATE or ALTERTABLESPACE operation.
For a given table space, the LOCKSIZE can be specified as PAGE, TABLE, TABLESPACE or ANY.
TABLESPACE : means all locks acquired on data in the tablespace will be at thetablespace level
TABLE : means locks acquired on data in the tablespace will be at the table level
PAGE : means locks acquired on data in the tablespace will be at the page levelwhenever possible.
ANY : (Which is the default) means that DB2 itself will decide the appropriatephysical unit of locking for the tablespace for each plan .Defaults to a page lock, but ifthe no. of pages that are locked exceeds an installation default, DB2 does a lockescalation and automatically locks a larger unit.
- 50 -
8/8/2019 DB2 Basic Manual
51/87
ACQUIRE/RELEASE PARAMETERS
DB2 always implicitly acquires locks of some kind
The acquire and release parameters on the BIND command specify when such tablespacelevel locks are to be acquired and released .For ACQUIRE, the possible specifications are
USE & ALLOCATE;For RELEASE, they are
COMMIT & DEALLOCATE
While Binding, DB2 allows us to specify transaction level lock acquiring and releasingparameters.
ACQUIRE:
ACQUIRE (use) - This option tells DB2 to take locks at the time of SQL statementsexecution. This is DB2 default.
ACQUIRE (allocate) - This option tells DB2 to take all necessary locks at the start of thetransaction.
RELEASE:
Similar to the DB2 acquire parameters, there are 2 release parameters.
RELEASE (commit) - This option tells DB2 to release all locks of transaction committime. This is the DB2 default.Release (de-allocate) - This option tells DB2 to release all locks only when the programends (i.e. the thread is de-allocated).
All combinations except ACQUIRE (Allocate) and RELEASE (commit) are allowed.
For more concurrency, use ACQUIRE (use) and RELEASE (commit).This is the DB2 default.
For better performance, use ACQUIRE (ALLOCATE) and RELEASE(DEALLOCATE).
- 51 -
8/8/2019 DB2 Basic Manual
52/87
- 52 -
Fields of Sql Communication Area
01SQLCA.05 SQLCAID PIC X(8).
05 SQLCABC PIC S9(9) COMP-4.
05 SQLCODE PIC S9(9) COMP-4
05 SQLERRM.
49 SQLERRML PIC S9(4) COMP-4.
49 SQLERRMCPIC X(70).
05 SQLERRP PIC X(8).
05 SQLERRD OCCURS 6 TIMES PIC S9(9) COMP-4.
05 SQLWARN.
10 SQLWARN0 PIC X.
10 SQLWARN1 PIC X.
10 SQLWARN2 PIC X.
10 SQLWARN3 PIC X.10 SQLWARN4 PIC X.
10 SQLWARN5 PIC X.
10 SQLWARN6 PIC X.
10 SQLWARN7 PIC X.
05 SQLEXT PIC X(8).
8/8/2019 DB2 Basic Manual
53/87
- 53 -
SQL WARNINGS
SQLWARN0 - This indicates that any of the following warnings
are flagged.
SQLWARN1 - This is flagged if a value is truncated when
assigned to a host variable
SQLWARN2 - This is set to W if some of the rows werent
considered by a column function because the
column being processed contained null values
SQLWARN3 - Here a W means you did not supply enough host
variables in an into clause to match the columns in
a select clause
SQLWARN4 - This flag is set to W if a dynamic SQL update or
delete statement does not have a Where clause
SQLWARN5 - This indicates that the SQL statement is valid only
in SQL/DS and not in DB2
SQLWARN6 - This indicates a W when an arithmetic operation
produces an unusual date or timestamp
8/8/2019 DB2 Basic Manual
54/87
- 54 -
SQLERRD is an array of six full word items.The third of the six, contains
useful information.
After an insert,delete or update statement, sqlerrd(3) contains the no.
rows the statement affected.
Eg.
EXEC SQL
DELETE FROM EMP
WHERE DNO IN
(SELECT DNO FROM MASTER_DEPT)
END-EXEC.
Display SQLERRD(3) ROWS WERE DELETED.
SQL WARNINGS
8/8/2019 DB2 Basic Manual
55/87
- 55 -
DB2 ERROR CODES
SQLCODE Keyword Meaning+100 SELECT Row not found during FETCH,
SELECT, UPDATE or DELETE
+304 Program Value and host variable are
incompatible
-305 Variables Null value occurred, and no indicator
variable was defined
-501 Cursor Cursor named in FETCH or CLOSE is
not open-551 Authority You lack the authority to access the
named object, possibly because its
name is not spelled correctly
-803 Updating Duplicate keys not allowed
-805 Plan DBRM not bound into this plan
-811 SELECT Embedded SELECT or sub select
returned more than one row
8/8/2019 DB2 Basic Manual
56/87
DATA DEFINITION
In this topic we will understand the two database objects and the SQL commands used in
creating, modifying or removing their definitions from the database:
TABLE
CREATE TABLE
ALTER TABLE
DROP TABLE
INDEX
CREATE INDEX
ALTER INDEX
DROP INDEX
TABLES
A table is a collection of rows having the same attribute or columns. In other words it is amatrix where the intersection of every row and a column is a value , which is the smallestunit of data that can be retrieved or changed. A row is a smallest unit of data that can beinserted or deleted.
The structure of a table in a tablespace can be created with the following command:
CREATE TABLE TABLE_NAME
(COLUMN1 DATATYPE {NOT NULL|NOT NULL WITH DEFAULT} ,
COLUMN2 DATATYPE {NOT NULL|NOT NULL WITH DEFAULT } ,
COLUMN3 DATATYPE {NOT NULL|NOT NULL WITH DEFAULT } ,
.
.
PRIMARY KEY (COLUMN1,COLUMN2,...)
FOREIGN KEY RELATION_NAME(COLUMN3,COLUMN4,...)
REFERENCES TABLESPACE_NAME.TABLE_NAME
ON DELETE {RESTRICT|CASCADE|SET NULL})IN DATABASE_NAME.TABLESPACE_NAME
A table name can be simple or fully qualified. A simple table name has to begin with acharacter and cannot be more than 18 characters long. Except for the first character thename can contain any alphanumeric character including the special characters (-,@,#,$). Afully qualified table name is a simple table name prefixed by the owners ID . For e.g. ifthe user MKTG is creating a table CUSTOMER then the table name will beMKTG.CUSTOMER.e.g..
1> simple table CUSTOMER can be created with the following command;
CREATE TABLE CUSTOMER
- 56 -
8/8/2019 DB2 Basic Manual
57/87
(CUSTID SMALLINT NOT NULL WITH DEFAULT,
CNAME CHAR(15) NOT NULL WITH DEFAULT,
CITY CHAR(15) NOT NULL WITH DEFAULT,
PRIMARY KEY (CUSTID)
IN MKTGDATA.TABLSPC1
This command creates the CUSTOMER table which is having the CUSTID field as itsprimary key. The NOT null with default clause prevents the column from having nullvalues and specifies a default value to be used if data for the specific column is missing.The default values of a column depend on the datatype of the column. The table belowshows the default values different datatypes in DB2.
2>Table ORDER which is a child table of CUSTOMER and PARTS tables can be createdafter creating both the parent tables, with the following command:
CREATE TABLE ORDERTAB
(ORDID SMALLINT NOT NULL WITH DEFAULT,ORDERDT DATE NOT NULL WITH DEFAULT,
RATE DEC(8,2)NOT NULL WITH DEFAULT,
CUSTID SMALLINT NOT NULL WITH DEFAULT,
PARTTID SMALLINT NOT NULL WITH DEFAULT,
PRIMARY KEY (ORDID,CUSTID,PARTID),
FOREIGN KEY FKEY1(CUSTID) REFERENCES CUSTOMER
ON DELETE RESTRICT
FOREIGN KEY FKEY2(PARTID) REFERENCES PARTS
ON DELETE RESTRICT
IN MKTGDATA.TABLSPC1
The above command creates the table ORDER with ORDID, CUSTID and PARTID asthe primary keys. These keys ensure uniqueness of the record within the table. The foreignkey FKEY1 ensures that the CUSTID field in this table consists of only one of thosevalues which are existing in the primary key field of that table which appears in thereferences clause i.e. CUSTOMER . While The foreign key FKEY2 ensures that thePARTID field in this table consists of only one of those values which are existing in the
primary key field of that table which appears in the references clause i.e. PARTS.
Tables once created can be altered with the following command:
ALTER TABLE TABLE_NAME
[COLUMN1 DATATYPE {NOT NULL|NOT NULL WITH DEFAULT} ,]
[AUDIT (NONE|CHANGES|ALL)]
[PRIMARY KEY (COLUMN1,COLUMN2,...)]
[FOREIGN KEY RELATION_NAME(COLUMN3,COLUMN4,...)
REFERENCES TABLESPACE_NAME.TABLE_NAME]
[DROP PRIMARY KEY]
[DROP FOREIGN KEY (FOREIGN_KEY_NAME)]
e.g...
- 57 -
8/8/2019 DB2 Basic Manual
58/87
1>In the CUSTOMER table CREDITLIMIT field can be added with the followingcommand:
ALTER TABLE CUSTOMER
ADD CREDITLIMIT INTEGER NOT NULL
2> The primary key definition of the ORDER table can be modified in the following way:
ALTER TABLE ORDERTAB
ADD PARTNO SMALLINT NOT NULL
DROP PRIMARY KEY
PRIMARY KEY (PARTNO)
INDEXES
They are used for efficient access to data . It is an ordered set of pointers to the data in aDB2 table. The rows in a table are stored in a particular order . But when the data has to beaccessed, the entire table needs to be scanned due to which the performance of the systemwould be affected. Here indexes can prove very significant as they provide alternate meansof access to the data thereby decreasing the I/O and the processing time. A user cannot byany means tell the system when to use or when not use the indexes. It is only the DB2optimizer which makes the choice at BIND time.
An index is a physical entity which can be based on one or more columns of a table andcan be created any time after the table is built. There is no limit on the number of indexeswhich can be built on a table , but a partitioned table must have atleast one index . This iscalled as a PARTITIONING INDEX and is used to define the scope of each partition andthereby assign the rows of the table to their respective partitions.
The second type of index is a CLUSTER INDEX . When a cluster index is created on atable the rows of that table are physically stored in the order of those fields which have
been mentioned in the cluster index. The order of these rows can also be specified asascending or descending. There can be only one cluster index per table. If no such index iscreated, it loads the data in order of the first index field defined.
UNIQUE INDEXES in DB2 can be created to disallow the user from entering duplicate
values into a field constrained by the unique index. For e.g... the primary key field of atable must have unique values in all the rows, this relational datamodel concept is wellsupported by a Unique Index on the primary key field.
Indexes can be created with the following command :
CREATE [UNIQUE] INDEX INDEX_NAME
ON TABLE_NAME (INDEX_COLUMN_NAME [ASC|DESC] [,...])
[CLUSTER [(PART INTEGER VALUES (CONSTANT OR RANGE))]]
[SUBPAGES (1|2|4|8|16) ]
[BUFFERPOOL (BP0|BP1|BP2)]
e.g.
- 58 -
8/8/2019 DB2 Basic Manual
59/87
The following command creates a unique clustering index CUSTIDX on the fieldCUSTID of the CUSTOMER table:
CREATE UNIQUE INDEX CUSTIDX
ON CUSTOMER (CUSTID)
USING STOGROUP MKTGSUBPAGES 8
CLUSTER
Each indexpage physically consists of 4k bytes, which can be further divided into amaximum of 16 subpages . The subpage is a logical unit of locking an index. Thereforewhen a subpage is used ,smaller amount of storage is locked for individual update. Thusfewer users are kept waiting at a given point of time and this causes increase inconcurrency . The SUBPAGE clause defines the number of subpages into which one index
page can be divided. The CLUSTER clause can be used to create clustering index, whichis the fastest path for sequential access.
Definition of an index can be altered by the following command:
ALTER INDEX INDEX_NAME
[PART (INTEGER)]
BUFFERPOOL(BP1|BP1|BP2)]
INDEXSPACE
As table data is stored in a tablespace, index data is physically stored in indexspace. Thisindexspace contains an ESDS for every index . If the indexspace is partitioned ,then each
partition has its own indexspace. It is divided in pages of the size 4k and further thesepages are divided into 1 to 16 units which are called subpages. This subpage as discussedearlier is the unit of locking.Apart from this, unlike a tablespace an indexspace need not be explicitly defined as it getscreated automatically whenever an index is created.
Exercise
1. Create the following tables
SALESMAN CUSTOMER PARTS ORDER
Slid Custid Partid OrdidName Cname Description OrderDateRegion City Act_Rate Rate
Min_Stock UnitsPartidCustid
Slid
2. Create primary key indexes for all the above created tables so as to enforce
uniqueness for these keys.
- 59 -
8/8/2019 DB2 Basic Manual
60/87
3. Describe the different types of indexes and their advantages with the create indexcommand
4. Add a new numeric field credit limit in the customer table with the alter command.This field should have a default values and cannot be left null.
5. What will be the effect of the following drop table command on the other objects in
the database which are related to the table ORDERS.Drop Table Orders.
RETRIEVAL OPERATIONS
In this topic we will discuss SQL statements for performing the following operations onthe data described below :
SELECT
INSERT
UPDATE
DELETE
SAMPLE DATABASE OF ABC Inc.
CUSTOMER TABLE
CUSTID CNAME CITY
1005 Spectrum Mumbai
1004 Lloyds Madras
1001 Pentasys Delhi
1002 Hertz Mumbai1003 Procter Pune
PARTS TABLE
PARTID DESCRIPTION ACT_RATE MIN_STOCK
101 Nuts 4.00 50000
106 Bolts 2.50 15000
104 Nails 5.00 12500
105 Latches 15.00
103 Hinges 6.00 12000
102 Handle 15.00 8500
ORDERTAB TABLE
ORDID ORDERDATE RATE UNITS PARTID CUSTID
201 O1/12/96 3.00 300 101 1001
202 12/11/95 5.50 250 103 1001206 06/22/96 4.50 375 104 1002
- 60 -
8/8/2019 DB2 Basic Manual
61/87
204 04/1896 3.75 175 101 1004
205 05/05/96 13.50 235 102 1003
203 11/0896 5.50 180 103 1003
SELECT STATEMENT
SQLgives the SELECT statement to retrieve the data from the database.This statement hasdifferent clauses attached to it which have been explained further. Unlike the other SQLstatements INSERT, UPDATE, DELETE this statement can work on multiple tables atthe same time.The basic syntax of the SELECT statement
SELECT {ALL /DISTINCT/ scalar-expression/s}
FROM table/s
[ WHERE conditional-expressions]
[ GROUP BY column/s]
[ HAVING conditional-expressions]
[ ORDER BY column/s ]
Let us now proceed with the different types of SELECT statements :
5.1.1 SIMPLE SELECT
To retrieve all the columns of the CUSTOMER table the following SELECT statementwill have to be executed:
SELECT * FROM CUSTOMER
RESULT(screen dump)
The above result indicates that the statement has retrieved all the records of theCUSTOMER table and all the fields too. Whenever a * is given by default all thecolumns re selected in the sequence in which they have been defined while creating thetable. If one has to retrieve only specific column the SELECT statement can be modifiedas below :
SELECT CNAME, CITY
- 61 -
8/8/2019 DB2 Basic Manual
62/87
FROM CUSTOMER
RESULT(screen dump)
SELECT ELIMINATING UNIQUE / DISTINCT VALUESIf only unique values have to be retrieved from the database the word DISTINCT should be specified before the column list. For e.g. if different CITY values from theCUSTOMER tables have to be retrieved the following SELECT statement can help :
SELECT DISTINCT CITYFROM CUSTOMER
RESULT(screen dump)
Without DISTINCT i.e. by default the above SELECT statement would retrieve the sameCITY multiple times i.e. ALL
COMPUTED VALUES RETRIEVAL
Many a times it happens that the data which is in the database has to represented in adifferent way. For e.g. if one has to retrieve the actual value(RATE * QTY) of every partsold to every individual customer from the PART_CUST table the statement will be:
SELECT PARTID,CUSTID, RATE*UNITSFROM ORDERTAB
RESULT(screen dump)
- 62 -
8/8/2019 DB2 Basic Manual
63/87
Computed retrieval might also be required when an expression has to be used on any ofthe field or fields while retrieving . For e.g. If the DESCRIPTION value of the part has to
be in upper case while the value is stored in lower/mixed case the following SELECTstatement is valid:
SELECT PARTID, UPPER(DESCRIPTION)FROM PARTS
CONDITIONAL RETRIEVAL
For retrieving data of specific criteria or multiple criterias which can be combinedthrough one SELECT statement, the statement has to use a WHERE condition. Someexamples below will explain the different ways of using WHERE.
E.g 1
SELECT CNAME, CITYFROM CUSTOMERWHERE CITY = Mumbai
screen dump
E.g 2
- 63 -
8/8/2019 DB2 Basic Manual
64/87
SELECT ORDID, ORDERDTFROM ORDERTABWHERE CUSTID = 1001 AND
UNITS > 200
screen dump
The above SELECT statement shows the data satisfying multiple conditions on the
TABLE ORDERTAB.
E.g 3
SELECT *FROM PARTSWHERE ACT_RATE < 4 OR
MIN_STOCK > 10000
screen dump
The above results shows that those records which are satisfying any one of the twoconditions are selected.
The WHERE condition can have different operators to compare the value of the fields. thenumeric operators (, = , ,>=,
8/8/2019 DB2 Basic Manual
65/87
The BETWEEN condition is used to define a range for comparing numeric values.It can be considered as a shortcut rather than involving two conditions with AND.
SELECT *
FROM PARTSWHERE MIN_STOCK BETWEEN 10000 AND 20000
screen dump
The same result as shown in the above screen can also be obtained by the followingquery :
SELECT *FROM PARTSWHERE MIN_STOCK > 10000 ANDMIN_STOCK < 20000
With the BETWEEN operator a NOT condition will retrieve exactly the opposite data asshown below :
SELECT *FROM PARTSWHERE MIN_STOCK NOT 10000 AND 20000
IN
The IN operator is an alternative for the OR used for individual conditions. when afield has to be compared against a set of given values the IN operator is useful.
SELECT *FROM ORDERTAB
WHERE PARTID IN (101, 104)
- 65 -
8/8/2019 DB2 Basic Manual
66/87
screen dump
The above result can also be obtained without an IN operator with the following SELECTstatement :
SELECT *FROM ORDERTABWHERE PARTID = 101 OR
PARTID = 104
screen dump
The NOT IN operator is used for deselecting the above data and selecting the data whosePARTID value is anything except 101, 104.
SELECT *FROM ORDERTABWHERE PARTID NOT IN (101, 104)
screen dump
LIKE
- 66 -
8/8/2019 DB2 Basic Manual
67/87
LIKE operators are used for comparing a value which either begins with certaincharacters, or ends with certain characters or contains certain characters. the followingsexamples deal with different ways of using the LIKE operator
SELECT DESCRIPTIONFROM PARTSWHERE DESCRIPTION LIKE N%
screen dump
The result shows only those PART records which have a name starting with the alphabetN.
SELECT DESCRIPTIONFROM PARTSWHERE DESCRIPTION LIKE %S
This command will retrieve only those records from the PART table which has the lastalphabet as S irrespective of what the other alphabets in the DESCRIPTION field.
As we observed in the above examples, a % stands for any characters. Similarly _(underscore) stands for a single character. This can be used to search a particularcharacter in a string at a particular position.
SELECT *FROM CUSTOMERWHERE CNAME LIKE _e%
screen dump
This SELECT statement retrieves only those records which have the alphabet e in itssecond position of the CNAME field.
- 67 -
8/8/2019 DB2 Basic Manual
68/87
Using a NOT LIKE in place of LIKE in the last SELECT statement will retrieve all theother records from the CUSTOMER table except those displayed in the above screendump.
CHECKING FOR NULL VALUES
For retrieving values which contain a NULL the operator IS has to be used. Forchecking a NULL value, this is the only valid operator as any other operator like >, NULL
OR
WHERE MIN_STOCK < NULL
OR
WHERE MIN_STOCK = NULL
If one need to retrieve the records having MIN_STOCK value as NULL, the SELECTstatement should be as follows :
SELECT *FROM PARTSWHERE MIN_STOCK IS NULL
screen dump
To check the values which are not NULL, the word NOT can be used with the IS operatoras shown below :
SELECT *FROM PARTSWHERE MIN_STOCK IS NOT NULL
screen dump
- 68 -
8/8/2019 DB2 Basic Manual
69/87
JOINS
A SELECT statement has the ability to select from multiple tables at once. So thisfeature is called as a JOIN whereby two tables are selected through the same statement. Inother words they are JOINS.
DB2 offers different types of JOINS as explained below :
Simple Join
According to Relational Database concepts, a column is responsible for creating arelationship between two tables. As in our sample database the PARTID column relatesthe ORDER table to the PARTS table. Similarly the CUSTID column relates the ORDERtable to the CUSTOMER table. A JOIN can be used for selecting data from any of thesetables at the same time through one SELECT.
SELECT CNAME, RATE, UNITSFROM CUSTOMER, ORDERTABWHERE CUSTOMER.CUSTID = ORDERTAB.CUSTID
screen dump
With the above query, one gets the records belonging to the CUSTOMER table as well asthe ORDER table. The CUSTID field being the common fields in these two tables needsto be compared, while selections from these tables have to take place. It is actually the
WHERE condition which compares the data of these two tables having the same values intheir CUSTID fields. It is necessary that this field has the same datatype and datawidth in
- 69 -
8/8/2019 DB2 Basic Manual
70/87
both the tables, but the name of the field may differ in the two tables. Apart from this thestatement may also have some more WHERE conditions as explained below :
SELECT CNAME, RATE, UNITSFROM CUSTOMER, ORDERTAB
WHERE CUSTOMER.CUSTID = ORDERTAB.CUSTID AND CITY = Mumbai
screen dump
Both these statements above are checking for the equal value of CUSTID fields that theycan be termed as EQUIJOINS. When one SELECT statement retrieves the data from the
multiple tables, each pair of table should be bound by a JOIN between them. In short, theno. of JOINs in a SELECT statement are one less than the number of tables being selectedfrom.
Self Join
As one can select from two tables through one SELECT statement, it is alsopossible to select one table twice in the same SELECT statement. Here is an examplewhich finds all the CUSTOMER records belonging to the same CITY
SELECT A.CNAME, B.CNAME, A.CITY
FROM CUSTOMER A, CUSTOMER BWHERE A.CITY = B.CITY
screen dump
In the above SELECT it appears as though two different tables A, B are being selectedfrom. In reality A and B are nothing but aliases used for the same table CUSTOMER.This means the first selection of the CUSTOMER table is given a name A while the
second selection is given a name B.
- 70 -
8/8/2019 DB2 Basic Manual
71/87
We also observe in our screen dump that a value like Spectrum is selected twice fromselection A as well as selection B because each value shows up once in each alias and the
predicate of these values is symmetrical. the other records like Hertz, Procter are alsorepeated because of the same reason. To avoid this, one has to add a WHERE conditionwhich checks for non matching predicates as shown in the following SELECT statement :
SELECT A.CNAME, B.CNAME, A.CITYFROM CUSTOMER A, CUSTOMER BWHERE A.CITY = B.CITYAND A.CNAME< B.CNAME
screen dump
SUB-QUERIES
A SELECT statement can also appear as a clause of another SELECT statement.Such SELECT statement are called NESTED queries. The inner queries returns a valuewhich is compared with a value of the outer query. In other words the outer query isexecuted after the inner query. Such type of nesting can be done upto multiple levels.
The example below selects the CNAME and the CITY for those CUSTOMERS who haveordered for PART 103. Here the outer query selects from the CUSTOMER table as thedata to be displayed belongs to the same table. While the inner query finds the CUSTIDvalue from the ORDER table where the PARTID value is 103 :
SELECT CNAME, CITYFROM CUSTOMERWHERE CUSTID IN (SELECT CUSTID
FROM ORDERTAB
WHERE PARTID = 103)
screen dump
The difference in NESTED queries and JOINS is that one needs to give a JOIN only if thedata to be extracted belongs to multiple tables. Nested queries are generally used when the
data belongs to one table while the condition has to be checked in some other table.
- 71 -
8/8/2019 DB2 Basic Manual
72/87
Performance-wise JOINS are preferred as NESTED queries execution is slower thanJOINS (according to IBM Manuals ).
In our example the CUSTID is common between the two tables (ORDER, CUSTOMER).It is this fields value which is checked between the two queries.
The next example discusses nesting of queries upto multiple levels. Irrespective of thenumber of levels to which nesting is done, the execution of the queries remain the same. Inother words, it is always the innermost query which will be executed first and then theenclosing query and so on.
To select the details of the PARTS which are ordered by the CUSTOMER Hertz, thefollowing NESTED query can be given :
SELECT *FROM PARTS
WHERE PARTID IN (SELECT PARTIDFROM ORDERTAB
WHERE CUSTID = (SELECT CUSTIDFROM CUSTOMER
WHERE CNAME = Hertz))
screen dump
CORRELATED SUB QUERY
A Correlated sub query is the one whose value depends upon a variable whichreceives a value from some outer query. Such sub-query has to be repeatedly executed forevery new value of the variable. These types of queries can be used as an alternatesolutions for JOINS.
The following example selects all the CNAME values from the CUSTOMER table whohave ordered for PART 103.
SELECT CNAMEFROM CUSTOMERWHERE 103 IN (SELECT PARTID
FROM ORDERTABWHERE CUSTID = CUSTOMER.CUSTID)
screen dump
- 72 -
8/8/2019 DB2 Basic Manual
73/87
Execution of such correlated queries take place in the following way :
1. The currently selected row from the outer query is stored.2. The sub-query is performed where a field from the row stored in first step is used forselecting specific rows from the table mentioned in this query (Outer reference).
3. The value from the outer query is evaluated on the basis of the results of the sub-queryexecution taken place in step 2. After this evaluation whether the record from theouter query should be selected or no is determined.
4. The same three steps mentioned above are performed for the next row of the outerquery till all the rows of the table are evaluated.
CORRELATED QUERIES ON THE SAME TABLE
Correlated sub- queries can also be fired on the same table as the outer queriestable. These types of queries are useful in selecting data from a single table which isevaluated against some other data of the same table.
This example selects all the details from the PART table whose ACT_RATE ishigher than the ACT_RATE of the PARTS Nails.
SELECT *FROM PARTSWHERE PARTID IN (SELECT PARTID
FROM PARTS
WHERE DESCRIPTION = Nails)
screen dump
OTHER OPERATOR (EXISTS, ANY/ SOME, ALL )
EXISTS
This operator produces a Boolean results. Unlike the other operators where a valueis used, it takes only a sub-query as an argument. The sub-query is evaluated to true orfalse and only if the result is true, the outer query which has the EXISTS operator
produces certain output. The query given below extracts data from the PARTS table onlyif there are more than one part having MIN_STOCK value > 5000.
SELECT *FROM PARTS
WHERE EXISTS(SELECT *
- 73 -
8/8/2019 DB2 Basic Manual
74/87
FROM PARTSWHERE MIN_STOCK > 5000)
screen dump
ANY/SOME
The operators ANY and SOME are interchangeable as the result produced bythem is just the same. The operator ANY / SOME takes all the values produced by thesub-query and evaluates to true if ANY of these values are equal to the value of certainfield of the record selected in the outer query. It is necessary to select a field of the samedatatype as the one against which it is being compared in the main query.
This example selects all the records from the CUSTOMER table whose CUSTID appearsin the ORDER table.
SELECT *FROM CUSTOMERWHERE CUSTID = ANY
(SELECT CUSTIDFROM ORDERTAB)
ALL
As the ANY operator checks for the OR condition , the ALL operator can be checkingthe AND operator. In other words, it checks for each and every value which is selected bythe inner query for satisfying a condition on a field of the outer query.
The query below selects all those ORDERS where UNITS ordered are greater than all theUNITS ordered by the CUSTOMER 1003.
- 74 -
8/8/2019 DB2 Basic Manual
75/87
SELECT *FROM ORDERTABWHERE UNITS> ALL
(SELECT UNITS
FROM ORDERTABWHERE CUSTID = 1003)
screen dump
The above statement examines UNITS value of all the ORDERS placed by CUSTOMER1003. It then find out the ORDERS having UNITS more than the highest UNITS ofCUSTOMER 1003 i.e. 235.
The NOT operator can be attached to all the operators discussed above as in the operatorssuch as IN, LIKE, BETWEEN discussed in the sub- section of 5.1.
AGGREGATE FUNCTIONS
Aggregate functions are those which return a single value like MINIMUM,MAXIMUM, AVERAGE, SUM after evaluating multiple values of a certain column inthe table. These functions appear in the column list of the SELECT statement. The list
below gives the names of the functions and the value returned by it.AGGREGATE FUNCTIONS
FUNCTION DATA TYPE RETURNED
MAX NUMBER/CHAR Largest value in the columnMIN NUMBER/CHAR Least value in the column
COUNT NUMBER/CHAR Number of values in the columnSUM NUMBER/CHAR Sum of values in the columnAVG NUMBER/CHAR AVG of values in the column
These functions can also be used in nested sub-queries or in combination with distinctClause.
Some examples :
1. To find the total no. of CUSTOMERS SELECT COUNT (*) FROM CUSTOMER.2. To find the TOTAL UNITS to ORDER by CUSTOMER 1003 SELECT SUM
(UNITS ) FROM ORDER WHERE CUSTID = 1003.3. To find the ORDER WHERE no. of UNITS order is less than the Avg. .
- 75 -
8/8/2019 DB2 Basic Manual
76/87
SELECT *FROM ORDERTAB
WHERE UNITS < (SELECT AVG (UNITS)FROM ORDERTAB
4. To SELECT the no. of CUSTOMERS who have placed ORDERS SELECT COUNT(DISTINCT CUSTID) FROM ORDERTAB
GROUP BY HAVING
This is an optional clause of the SELECT statement. It groups the rows of a tablesurrounding to the value in the column appearing on which GROUPING is done. It has anoptional sub-clause HAVING which checks a condition on every group created byGROUP BY. Only those groups which satisfy the condition are passed to the columnfunction further. One can understand HAVING a filter on the group as the clause,WHERE acts as a filter on the final data being selected.
Group by clause allows only column functions or the those columns on which grouping isdone to be selected in the column list of the SELECT statement. The same is applicable tothe HAVING clause.
This example selects the maximum UNITS value for every CUSTOMER
SELECT MAX (UNITS), CUSTIDFROM ORDERTABGROUP BY CUSTID
screen dump
- 76 -
8/8/2019 DB2 Basic Manual
77/87
In the above example the execution of the query takes place in the following manner :
The GROUP BY clause puts all records which have the same CUSTID value together andcreates on set, the next set has records of the next CUSTID so on it continues to create thegroup till all CUSTIDs are considered. The result of which is shown below . Then the
maximum no. of units are selected from each group.
screen dumpof order by custid on order
We observe in the above result that a CUSTID who has placed a single order (1004) isplaced in a separate group. Let us assume the result is not supposed to show any max valuefor such groups having a single record. In such a case attaching the HAVING clause to theGROUP BY clause is necessary. Thus the previous query changes to -
SELECT MAX (UNITS), CUSTID
FROM ORDERGROUP BY CUSTIDHAVING COUNT (*)>1
ORDER BY
The final result being displayed by the SELECT statement can be sequenced in a particularway by using the ORDER BY clause. The data which is selected is displayed either in theorder of its selection or in the order of its physical storage. When this order has to be seton a certain field of the table one can use the ORDER BY clause.
In the statement below, we arrange all the records in the order of ORDERDATE field inthe ORDER table.
SELECT *FROM ORDERTAB
ORDER BY ORDERDT
screen dump
- 77 -
8/8/2019 DB2 Basic Manual
78/87
One can also order the data on multiple fields. The fields have to appear in the order byclause in the order of the required sequence. If the above e.g. one has to show the recordsor ORDER table in the sequence of CUSTID and within every single value of CUSTID,the records should be sequenced on the ORDERDATE field the query given will be
SELECT *FROM ORDERTABORDER BY CUSTID, ORDERDT
UNION
The UNION operators is used when records appearing in either or both the tablesreferred in the SELECT statement are to be retrieved. The operation is embedded in twoSELECT statements and both these SELECT statements must
1. Have the same no. of columns
2.3. Have the columns which belong to same datatype
the example below selects the PARTID value which have a MIN_STOCK above 10000 orthose parts which are ordered by
CUSTOMER 1001SELECT PARTIDFROM PARTSWHERE MIN_STOCK >10000UNION
SELECT PARTIDFROM ORDERTAB
- 78 -
8/8/2019 DB2 Basic Manual
79/87
WHERE CUSTID = 1001
screen dump
The result indicates that a PARTID selected only once. The UNION operator implicitlyeliminates the duplicate value unless an ALL is used with it as shown below :
SELECT PARTIDFROM PARTSWHERE MIN_STOCK >10000UNION ALLSELECT PARTIDFROM ORDERTABWHERE CUSTID = 1001
screen dump
AGGREGATE FUNCTIONS
MAX NUMBER/CHAR - Largest value in the columnMIN NUMBER/CHAR - Least value in the columnCOUNT NUMBER/CHAR - Numb