Upload
aitel
View
236
Download
0
Embed Size (px)
Citation preview
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 1/157
CTS-PAC Version 1.1 1
DB2 - IBM’s Relational DBMS
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 2/157
CTS-PAC Version 1.1 2
Session 1
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 3/157
CTS-PAC Version 1.1 3
Topics to be covered in this session
• Introduction to databases - covers their advantages and
the types of databases (time : 30 min)
• Relational database concepts - covers Terminology,ER model , Normalisation, An Introduction to
Database objects, CODD‟s Relational Rules, An
Introduction to SQL.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 4/157
CTS-PAC Version 1.1 4
What is Data ?„A representation of facts or instruction in a form
suitable for communication‟ - IBM Dictionary
What is a Database ?
„Is a repository for stored data‟ - C.J.Date
Introduction to Databases
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 5/157
CTS-PAC Version 1.1 5
What is a database system ?An integrated and shared repository for stored data or
collection of stored operational data used by
application systems of some particular enterprise.
Or
„Nothing more than a computer-based record keeping
system‟.
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 6/157
CTS-PAC Version 1.1 6
Advantages of DBMS over File Mngt Sys
•Data redundancy
• Multiple views
• Shared data
• Data independence (logical/physical)
• Data dictionary
• Search versatility
• Cost effective
• Security & Control
• Recovery restart & Backup
• Concurrency
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 7/157
CTS-PAC Version 1.1 7
TYPES OF DATABASES (or Models)
• Hierarchical Model
• Network Model
• Relational Model
• Object-Oriented Model
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 8/157
CTS-PAC Version 1.1 8
contd...
• HIERARCHICAL
• Top down structure resembling an upside-down
tree
• Parent child relationship
• First logical database model
• Available in legacy systems on Mainframe
computers• Example - IMS
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 9/157
CTS-PAC Version 1.1 9
contd...
• NETWORK
• Does not distinguish between parent and child. Any
record type can be assocaited with any number of
arbitrary record types• Enhanced to overcome limitations of Network
model but in reality, there is minimal diffeence due
to frequent enhancements
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 10/157
CTS-PAC Version 1.1 10
contd...
• RELATIONAL
• Data stored in table in the form of tables and rows.
• Examples - DB2, Oracle, Sybase, Ingres etc
• OBJECT -ORIENTED MODEL
• Data attributes and methods that operate on those
attributes are encapsulated in structures calledobjects
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 11/157
CTS-PAC Version 1.1 11
RELATIONAL DB CONCEPTS
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 12/157
CTS-PAC Version 1.1 12
Relational Properties
• Why Relational ? - Relation is a mathematical
term for a table - Hence Relational database „is
perceived‟ by the users as a set of tables.
• All data values are atomic.
• Entries in columns are from the same domain
• Sequence of rows (T-B) is insignificant
• Each row is unique
• Sequence of columns (L-R) is insignificant
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 13/157
CTS-PAC Version 1.1 13
Relational Concepts (or Terminology)
• Relation : A table or File
• Tuple : Row contains an entry for each attribute
• Attributes : Columns or the characteristics that
define the entity
• Domain:. A range of values (or Pool)• Entity : Some object about which we wish to store
information
• Null : Represents an unknown value• Atomic : Smallest unit of data; the individual data
value
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 14/157
CTS-PAC Version 1.1 14
contd...
• Candidate key : Some attribute (or a set of
attributres) that may uniquely identify each
row(tuple) in the relation(table) This exists only for
a short period of time and the primary and attribute
key take its place.• Primary key : The candidate key that is chosen for
primary attributes to uniquely identify each row.
• Alternate key : The remaining candidate keys that
were not chosen as primary key
• Foreign key : An attrtibute of one relation that might
be a primary key of another relation.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 15/157
CTS-PAC Version 1.1 15
Entity Relationship Model
• E-R model is a logical representation of data for abusiness area
• Represented as entities, relationship between entities
and attributes of both relationships and entities• E-R models are outputs of analysis phase i.e they are
conceptual data models expressed in the form of an E-
R diagram
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 16/157
CTS-PAC Version 1.1 16
Normalisation (1NF - 5NF)
• It is done to bring the design of database to astandadized mode
• 1NF : All entities must have a unique identifier, or key,
that can be composed of one or more attributes. Allattributes must be atomic and non repeating.
• 2NF : Partial functional dependencies removed - all
attributes that are not a part of the key must depend on
the entire key for that entity.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 17/157
CTS-PAC Version 1.1 17
contd...
• 3NF : Transitive dependencies removed - attributes thatare not a part of the key must not depend on any non-
key attribute.
• 4NF : Multi valued dependencies removed• 5NF : Remaining anomalies removed
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 18/157
CTS-PAC Version 1.1 18
Types of Integrity
• Entity Integrity : Rule states that no column that ispart of a primary key can have a null value
• Referential Integrity : Rule states that every foreign
key in the first table must either match a primary keyvalue in the second table or must be wholly null
• Domain Integrity : Integrity of information allowed in
column
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 19/157
CTS-PAC Version 1.1 19
Example of a Relational Structure
CUSTOMER Places ORDERS
ORDERS Has PRODUCTS
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 20/157
CTS-PAC Version 1.1 20
The above relations can be interpreted as
follows :
• A Customer can place any number of orders (one-to-many)
• Each order relates to only one customer (one-to-one)
• One order can contain many products (one-to-many)• One Product can be a part of many orders(one-to-
many)
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 21/157
CTS-PAC Version 1.1 21
contd...
• In the above example Customer, Order & Product arecalled ENTITIES.
• An Entity may transform into table(s).
• The unique identity for information stored in anENTITY is called a PRIMARY KEY. Eg. Customer-
No uniquely identifies each customer
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 22/157
CTS-PAC Version 1.1 22
contd...
A table essentially consists of
• Attributes, which define the characteristics of the
table
• Primary key, which uniquely identifies each row of data stored in a table
• Secondary & Foreign Keys/indexes
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 23/157
CTS-PAC Version 1.1 23
contd... Table Definition :
Table „Customer‟ -
Attributes - Customer-No, Cust-name,
Cust-location, Cust-Id, Order-no...
Primary Key - Customer-No
Secondary Key - Cust-Id
Foreign-Key - Order-no
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 24/157
CTS-PAC Version 1.1 24
contd...
• The Relationships transform into Foreign Keys. For eg.Customer is related to Orders thru „Order - No‟ which is
the Foreign-key in Customer and Primary key in Order.
So basically the relationship „Places‟ is thru the Order -
No.
• As per the relational integrity the Primary-Key ,Order-
No, for the table „Orders‟ can never be Null, while it
can be so in the table „Customer ‟.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 25/157
CTS-PAC Version 1.1 25
contd...
• Tables exist in Tablespaces. A tablespace can containone or more tables
• Apart from the Primary Key, a table can have many
secondary keys/indexes, which exist in Indexspaces.• These tablespaces and indexspaces together exist in a
Database
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 26/157
CTS-PAC Version 1.1 26
contd...
• To do transformations as described above we need atool that will provide a way of creating the tables,
manipulate the data present in these, create
relationships,indexes,tablespace, indexspace and so on.
DB2 provides SQL which performs these functions.
The next part briefly deals with SQL and its functions.
A detailed explanation will be taken up later.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 27/157
CTS-PAC Version 1.1 27
CODDS RELATIONAL RULES
• 1. All information in a relational database isrepresented explicitly at the logical level and in exactly
one way - by values in tables
• 2. Each and every datum(atomic value) in a relational
database is guarenteed to be logically accessible by
resorting to a combination of tablename, primary key
value, and column name
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 28/157
CTS-PAC Version 1.1 28
contd...
• 3. Null values are supported for representing missinginformation in a systematic way irrespective of the
datatype.
• 4. The database description is represented at the logical
level in the same way as ordinary data, so that
authorised users can apply the same relational language
to its interrogation as they apply to the regular data.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 29/157
CTS-PAC Version 1.1 29
contd...
• 5.A relational system may support several languagesand various modes of terminal use. However there
must be one language whose statements can express all
of the following items: (1)data definitions (2)view
definitions (3)data manipulation(interactive and by
program)(4) integrity constraints (5) authorisation(6)
transaction boundaries(begin, commit,rollback)
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 30/157
CTS-PAC Version 1.1 30
contd...
• 6. All views are theoretically updatable, are alsoupdatable by the system
• 7. The capability of handling a base relation or aderived relation (view) as a single operand applies not
only to the retrierval of of data but also to the
insertion, updation and deletion of data
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 31/157
CTS-PAC Version 1.1 31
contd...
• 8. Application programs and terminal activities remainlogically unimpaired whenever any changes are made
in either storage representations or access methods
• 9. Application programs and terminal activities remain
logically unimpaired when information-preserving
changes of any kind that theoretically permit
unimpairment are made to the base tables.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 32/157
CTS-PAC Version 1.1 32
contd...
• 10. Integrity constraints specific to a particularrelational database must be definable in the relational
data sublanguage and storable in the catalog, not in the
application programs.
• 11. The data manipulation sublanguage of a relational
DBMS must enable application programs and inquiries
to remain logically the same whether and wheneverdata are physically centralized or distributed.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 33/157
CTS-PAC Version 1.1 33
contd...
• 12. If a relational system has a low-level(single-record-at-a-time)language, that low level cannot be
used to subvert or bypass the integrity rules and
constraints expressed in the higher-level relational
language(multiple-records-at-a-time)
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 34/157
CTS-PAC Version 1.1 34
An introduction to SQL
SQL or Structured Query Language is
• A Powerful language that performs the functions of
data manipulation(DML), data definition(DDL) and
data control or data authorization(DAL/DCL).
• A Non procedural language - the capability to acton a set of data and the lack of need to know the
how to retrieve it. An SQL can perform the
functions of more than a procedure.
• Very flexible
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 35/157
CTS-PAC Version 1.1 35
contd...
SQL - Features
• What you want and not how to get it
• Unlike COBOL or 4GL‟s, SQL is coded without
data-navigational instructions.The optimal access
paths are determined by the DBMS. This isadvantageous because the database knows better
how it has stored data than the user.
• Set level processing & multiple row processing
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 36/157
CTS-PAC Version 1.1 36
The following are the Operations that can be
performed by a SQL on the database tables :
• Select• Project
• Union
• Intersection• Difference
• Cartesian Product
• Join
• Divide
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 37/157
CTS-PAC Version 1.1 37
Session 2
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 38/157
CTS-PAC Version 1.1 38
Topics to be covered in this session
• SQL - this is to be dealt here because all other data
objects manipulation, creation and use, involve SQL‟s.
• DB2 objects - Database, Tablespaces & Indexspaces -creation & use, and other terminologies associated with
databases.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 39/157
CTS-PAC Version 1.1 39
Topics dealt with, in SQL
• Definition and Types
• usage of SQL‟s with examples, scalar and column
functions
• Subqueries and Multiple queries, DMLs
• Static & Dynamic SQLs
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 40/157
CTS-PAC Version 1.1 40
Structured Query Language - SQL
• Standard query language for RDBMS
• Non procedural lang : Programmer specifies what data
is needed but not how to retrieve it
• Used also to define data structures, control access tothe data and delete occurrences of data
• Uses set-level processing
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 41/157
CTS-PAC Version 1.1 41
SQL - Types - based on the functionality
• Data Definition Language (DDL) - CREATE, ALTER,DROP
• Data Manipulation Language (DML) - DELETE,
INSERT, SELECT, UPDATE
• Data Control Language (DCL) - GRANT, REVOKE
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 42/157
CTS-PAC Version 1.1 42
SQL - Types
• Production SQL or Ad-Hoc SQL
• Embedded SQL or Stand-alone SQL
• Static or Dynamic SQL
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 43/157
CTS-PAC Version 1.1 43
SQL - Selection & Projection
• Select retrieves a specific number of rows from a table
• Projection operation retrieves a specified subset of
columns(but all rows) from the table
Eg : Select Cust-no, Cust-name from Customer;The WHERE clause defines the Predicates for the SQL
operation.
The above WHERE clause can have multiple conditions
using AND & OR.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 44/157
CTS-PAC Version 1.1 44
Select distinct, select in range :
Select Cust-no, Cust-name, Cust-addr
where Cust-no BETWEEN 10000 AND 20000;
Select Cust-no, Cust-name, Cust-addr
where Cust-no NOT BETWEEN 1000 AND 2000;
Select Cust-no, Cust-name, Cust-addr
where Cust-no IN(1000, 2000);
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 45/157
CTS-PAC Version 1.1 45
contd...
Select Cust-no, Cust-name, Cust-addr
where Cust-id like/not like „425%‟
Note :- „_‟ for a single char ; „%‟ for a string of charsEscape „ \ ‟ - escape char;if precedes „_‟ or „%‟ overrides
their meaning
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 46/157
CTS-PAC Version 1.1 46
contd...
NULL : To check null the syntax is „IS NULL‟ or „IS NOT NULL‟.
Select Cust-no, Cust-name, order-nowhere order-no IS NULL;
However if there are null values for order-no, then these
are always evaluated as a „Not True‟ condition in a
Query.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 47/157
CTS-PAC Version 1.1 47
Order by and Group by clauses :
• Order by sorts retrieved data in the specified order;uses the WHERE clause
• Group by operator causes the table represented by the
FROM clause to be rearranged into groups, such that
within one group all rows have the same value for the
Group by column (not physically in the database). The
Select clause is applied to the grouped data and not to
the original table.Here „HAVING‟ is used to eliminate groups, just like
WHERE is used for rows.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 48/157
CTS-PAC Version 1.1 48
Example :-
Select Order-No, SUM(No-Prodts)
From ORDER
Group by Order-No
Having AVG(No-Prodts) < 10Order by Order-No ;
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 49/157
CTS-PAC Version 1.1 49
Functions
• Types are two :
• Column Function
• Scalar Function
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 50/157
CTS-PAC Version 1.1 50
Column Functions
• Compute from a group of rows aggregate value for aspecified column(s)
• AVG, COUNT, MAX, MIN, SUM
• Rules for column Functions - Refer Handout
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 51/157
CTS-PAC Version 1.1 51
Scalar Functions
• Are applied to a column or expression and operate on asingle value.
• CHAR, DATE, DAY(S), DECIMAL, DIGITS,
FLOAT, HEX, HOUR, INTEGER, LENGTH,
MICROSECOND, MINUTE, MONTH, SECOND,
SUBSTR, TIME, TIMESTAMP, VALUE,
VARGRAPHIC, YEAR
• Rules for Scalar Functions - Refer handout
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 52/157
CTS-PAC Version 1.1 52
Complex SQL‟s
• One terms a SQL to be complex when data that is
to be retieved comes from more than one table
• SQL provides two ways of coding a complex SQL
• Subqueries and
• Joins
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 53/157
CTS-PAC Version 1.1 53
Subqueries
• Nested select statements• specified using the IN(or NOT IN) predicate, equality
or non-equality predicate(„=„ or „<>„) and comparative
operator(<, <=, >, >=)
• When using the equality, non-equality or comparative
operators, the inner query should return only a single
value
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 54/157
CTS-PAC Version 1.1 54
contd...
• Select Cust-No, Cust-Name
From CUSTOMER Where Order-No IN
( Select Order-No From ORDER
Where No-Prdts <5);
• Select Cust-No, Cust-addr
From CUSTOMER Where Order-No =
( Select Order-No From ORDER Where No-Prdts=5);
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 55/157
CTS-PAC Version 1.1 55
contd...
• The nested loop statements gives the user the flexibilityfor querying multiple tables
• A specialized form is Correlated Subquery - the nested
Select stmt refers back to the columns in previous
select stmts
• It works on Top-Bottom-Top fashion
• Noncorrelated Subquery works in Bottom-to-Top
fashion
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 56/157
CTS-PAC Version 1.1 56
Eg - Correlated Subquery..
• SELECT A.Cust-name A.Cust-addrFROM CUSTOMER A WHERE A.Order-No IN
(SELECT Order-No FROM CUSTOMER B
WHERE A.Cust-id = B.Cust-id)ORDER BY A.Cust-id, A.Cust-no ;
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 57/157
CTS-PAC Version 1.1 57
Corelated Subquery using EXISTS clause :
SELECT Cust-No, Cust-name FROM CUSTOMER AWHERE EXISTS (SELECT * FROM ORDER B
WHERE B.Order-No = A.Order-No
AND B.Order-No = 5);
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 58/157
CTS-PAC Version 1.1 58
Multiple levels of Subquery
SELECT Cust-no, Cust-name, Cust-addrFROM CUSTOMER A
WHERE Order-no IN
(SELECT order-no FROM ORDER BWHERE Prod-id IN
(SELECT Prod-id FROM PRODUCTS
WHERE Prod-name = „NUTS‟));
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 59/157
CTS-PAC Version 1.1 59
Joins
OUTER JOIN : For one or more tables being joined, bothmatching and nonmatching rows are returned.
Duplicate columns may be eliminated
The nonmatching columns will have nulls in them.
INNER JOIN: Here there is a possibility one or more of
the rows from either or both tables being joined will
not be included in the table that results from the joinoperation
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 60/157
CTS-PAC Version 1.1 60
DML‟s
INSERT :
Eg: INSERT INTO Tablename(column1,
column2, column3 ,......)VALUES( value1, value2, value3 ,........)
If any column is omitted in an INSERT stmt and that
column is NOT NULL, then INSERT fails; if null it is
set to null
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 61/157
CTS-PAC Version 1.1 61
contd...
• If the column is defined as NOT NULL BYDEFAULT, it is set to that default value
• Omitting the list of columns is equivalent to specifying
all values
• SELECT - INSERT
INSERT INTO TEMP (A#, B)
SELECT A#, SUM(B) FROM TEMP1 GROUP
BY A# ;
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 62/157
CTS-PAC Version 1.1 62
contd...
UPDATE:
Eg: UPDATE tablename SET Columnname(s) =
scalar expression
WHERE [ condition ]
• Single or Multiple row updates
• Update with a Subquery
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 63/157
CTS-PAC Version 1.1 63
contd...
DELETE:
Eg: DELETE FROM Tablename WHERE
[condition ];
• Single or multiple row delete or deletion of all rows
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 64/157
CTS-PAC Version 1.1 64
Static SQL
• Hard-coded into an application program• cannot be modified during the program‟s execution
except for changes to the values assigned to the host
variables
• Cursors are used to access set-level data
• The general form is EXEC SQL
[SQL stmts]
END-EXEC.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 65/157
CTS-PAC Version 1.1 65
Dynamic SQL
• Stmts can change throughout the program‟s execution • When the SQL is bound, the application plan or
package that is created does not contain the same info
as that for a static SQL program
• The access paths cannot be determined before
execution
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 66/157
CTS-PAC Version 1.1 66
SQL Guidelines :
- Refer handout- Mullins, chapter 2
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 67/157
CTS-PAC Version 1.1 67
Topics dealt with, in DB2 objects
• Databases, stogroup, Tablespaces (types, creation and
modification)
• Indexspaces (creation and modification)• some more terms associated with tablespaces
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 68/157
CTS-PAC Version 1.1 68
DB2 Objects
• Databases - User & system(catalog)• A collection of logically related objects - like
Tablespaces, Indexspaces, Tables etc.
• not a physical kind of object - may occupy morethan one disk space
• A STOGROUP & BUFFERPOOL must be defined
for each database. Stogroup and user-defined VSAM
are the two storage allocations for a DB2 datasetdefn.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 69/157
CTS-PAC Version 1.1 69
Stogroup
• It is a collection of direct access volumes, all of thesame device type
• The option is defined as a part of tablespace definition
• When a given space needs to be extended, storage isacquired from the
appropriate stogroup
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 70/157
CTS-PAC Version 1.1 70
contd...
• In a given database, all the spaces need not have thesame stogroup
• These are, in a sense, the most physical of various
storage objects in DB2
• More than one volume can be defined in a stogroup.
DB2 keeps track of which volume was defined first &
uses that volume.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 71/157
CTS-PAC Version 1.1 71
VCAT Option
• User Defined VSAM datasets have to be definedexplicitly by the AMS utility IDCAMS
• Two types of VSAM datasets are used -ESDS & LDS.
Linear Data set is more efficiently used by DB2
• Vsam datasets defined here are different from the plain
vsam datasets - can access them only thru VSAM
Media Manager
bl
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 72/157
CTS-PAC Version 1.1 72
Tablespaces
• Logical address space on secondary storage to hold oneor more tables
• A „SPACE‟ is basically an extendable collection of
pages with each page of size 4K or 32K bytes.
• It is the storage unit for for recovery and reorganizing
purpose
• Three Type of Tablespaces - Simple, Partitioned &
Segmented
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 73/157
CTS-PAC Version 1.1 73
Simple Tablespaces
• Can contain more than one stored table• Depending on appln, storing more than one Table
might enable faster retrieval for joins using these tables
• Usually only one is preferred. This is because a singlepage can contain rows from all tables defined in the
database.
• LOAD with replace option deletes all data
S d T bl
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 74/157
CTS-PAC Version 1.1 74
Segmented Tablespaces
• Can contain more than one stored table, but in asegemented space
• A „Segment‟ consists of a logically contiguous set of n
pages
• No segement is allowed to contain records for more
than one table
• Sequential access to a particular table is more efficient
d
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 75/157
CTS-PAC Version 1.1 75
contd...
• Mass Delete is much more efficient than in any otherTablespace
• Reorganizing the tablespace will restore every table to
its clustered order
• Lock Table on table locks only the table, not the entire
tablespace
• If a table is dropped, the space for that table can be
reclaimed with minimum reorg
P i i d T bl
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 76/157
CTS-PAC Version 1.1 76
Partitioned Tablespaces
• Only one table in a partitioned TS; 1 to 64partitions/TS
• It is partitioned in accordance with value ranges for
single or a combination of columns. Hence these
column(s) cannot be updated
td
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 77/157
CTS-PAC Version 1.1 77
contd...
• Individual partitions can be independently recoveredand reorganized
• Different partitions can be stored on different storage
groups for efficient access.
Tablespace parameters to be specified for TS
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 78/157
CTS-PAC Version 1.1 78
p p p
creation
• Locksize - indicates the type of locking DB2 performsfor the given TS
• Page
•Table
• Tablespace
• ANY - DB2 decides the starting page
td
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 79/157
CTS-PAC Version 1.1 79
contd...
• USING - method of storage allocations - Stogroup orVcat
• PCTFREE - % of space available for future inserts
•FREEPAGE - no of pages after which an empty page is
available
• Bufferpool - BPQ, BP1, BP2 & BP32K
• CLOSE - Yes/No - whether the underlying vsam
datasets be closed each time the table is used.Max noof datasets that can be open in DB2 at a time is 10,000
td
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 80/157
CTS-PAC Version 1.1 80
contd...
• ERASE - Yes/No - whether physical DASD where theTS reside to be written with binary zeros when the TS
is dropped
• NUMPARTS - For Partitioned Tablespaces
• SEGSIZE - For Segmented Tablespaces
T bl P t f C ti
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 81/157
CTS-PAC Version 1.1 81
Table Parameters for Creation
• Column Definition• Format : CREATE TABLE TABLENAME (Column
Definitions)
•PRIMARY KEY(Columns) / FOREIGN KEY
*
• UNIQUE (Colname) (referential constraint)
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 82/157
CTS-PAC Version 1.1 82
contd...
• 1. LIKE Table name / View name• 2. IN Database Tablespace Name
• Foreign Key references dbname.table on „relation
condition for delete‟
• Table1 references table2(target) - Table2‟s Primary key
is the foreign key defined in Table1
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 83/157
CTS-PAC Version 1.1 83
contd...
• The Condn‟s are CASCADE, RESTRICT & SETNULL (referential constraint for the foreign key
definition)
• Inserting (or updating ) rows in the target is allowed
only if there are no rows in the referencing table
Alter & Drop stmts
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 84/157
CTS-PAC Version 1.1 84
Alter & Drop stmts
• ALTER : ALTER TABLE <Tablename>ADD Column Data-type [ not null with default]
• Alter allows primary & Foreign key specifications to
be changed
• It does not support changes to width or data type of a
column or dropping a column
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 85/157
CTS-PAC Version 1.1 85
contd...
• DROP : DROP TABLE <Tablename>• Similar stmts are there for INDEX.
Some general rules for RI & Table Parameters
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 86/157
CTS-PAC Version 1.1 86
Some general rules for RI & Table Parameters
• Avoid nulls in columns participating inArithmatic logic or comparisons
• Primary key cols cannot be nulls
• Limit referential structures to no more thanthree levels in a direction
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 87/157
CTS-PAC Version 1.1 87
contd...
• Use DB2‟s inherent features rather than pgm codedRI‟s.
• Do not use RI‟s on tables build from another RI system
• Consider using Fieldprocs or Editprocs or Validprocs
Index Parameters for Creation
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 88/157
CTS-PAC Version 1.1 88
• CREATE INDEX Indexname ON Tablename
(Colnames asc/desc)• CLUSTER
• SUBPAGES
• USING STOGROUP/VCAT (the corresponding name)
• PRIQTY / SECQTY ; ERASE Yes/No
• BUFFERPOOL
• CLOSE - Yes/No
• FREEPAGE
• PCTFREE
Index Guidelines What to do ?
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 89/157
CTS-PAC Version 1.1 89
Index Guidelines - What to do ?
1. Consider indexing on columns used inUNION,DISTINCT,GROUP BY, ORDER BY &
WHERE clauses.
2. Limit the indexing of frequently updated columns
3. Create explicitly, a clustering index
4. Create a unique index on the primary key and
indexes on foreign keys
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 90/157
CTS-PAC Version 1.1 90
contd...
5. overloading of index when row length of a table tobe accessed is short
6. Atleast one index must be defined for a table with
more than 100 pages
7. Use Multicolumn index rather than a multi-index
(appln dependent); however the latter requires more
DASD .
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 91/157
CTS-PAC Version 1.1 91
contd...
8. Create indexes before loading the table.9. Clustering reduces I/O; DB2 optimizer usually tries
to use an index on clustered column before using the
other indexes.
10. Optimize Subpages Parameter
11. Specify Indexspace freespace the same as
tablespace freespace
contd
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 92/157
CTS-PAC Version 1.1 92
contd...
12. Use the DEFER option while creating the index.RECOVER INDEX utility can then be used to populate
the index. Recover utility populates index entries
faster.
13. Use different STOGROUP‟s for Tablespaces &
indexspaces
14. Create Critical indexes in a different bufferpool than
the tablespaces.
Index Guidelines - What Not to do ?
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 93/157
CTS-PAC Version 1.1 93
Index Guidelines - What Not to do ?
1. Avoid indexing on Variable columns2. Limit the number of indexes on partitioned TS
3. Avoid indexes if the table is very small (< 10 pages) or
it has heavy inserts and deletes and is very small (< 20
pages) or it is accessed with a scan. Avoid defining
redundant indexes
Some more terms & concepts associated with
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 94/157
CTS-PAC Version 1.1 94
Tables
VIEWS:• It is a logical derivation of a table from other
table/tables. A View does not exist in its own right.
• They provide a certain amount if logical independence
• They allow the same data to be seen by different users
in different ways
• In DB2 a view that is to accept a update must be
derived from a single base table
Some more terms & concepts associated with
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 95/157
CTS-PAC Version 1.1 95
Tables
Aliases and Synonyms :Both mean „another name‟ for the table.
however the difference is a synonym is private to the
user who created it. Aliases are used basically foraccessing remote tables (in distributed data
processing), which add a location prefix to their
names.Using aliases creates a shorter name.
Some more terms & concepts associated with
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 96/157
CTS-PAC Version 1.1 96
Tables
Format:CREATE VIEW <Viewname> (<columns>)
AS Subquery (Subquery - Select from
other Table(s))
. CREATE ALIAS <Aliasname> FOR
<Tablename>
CREATE SYNONYM <Synonymname> FOR
<Tablename>
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 97/157
CTS-PAC Version 1.1 97
Session 3
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 98/157
CTS-PAC Version 1.1 98
Topic to be covered in this session
• The following topics will be covered in this session
• Application programming using DB2 - 1 day
• Data control Language, SPUFI, QMF, Appln pgmingGuidelines - 0.5 days
Application programming using DB2
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 99/157
CTS-PAC Version 1.1 99
Application programming using DB2
• Application environments supporting DB2 :• IMS(Batch/Online), CICS, TSO(Batch/Online)
• CAF - Call Attach Facility
• All DB2 application types can execute concurrently• Host Language support - Cobol, PL/1, C, Fortran or
Assembly lang
Steps involved in creating a DB2 application
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 100/157
CTS-PAC Version 1.1 100
Steps involved in creating a DB2 application
• Coding the application• using Host variables
• using Embedded SQL
• using Cursors• issue DCLGEN command
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 101/157
CTS-PAC Version 1.1 101
contd...
• Pre compile the program• Compile & Link edit the program
• Bind
Host Variables
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 102/157
CTS-PAC Version 1.1 102
Host Variables
• These are variables(or rather area of storage) defined inthe host language to use the predicates of a DB2 table.
These are referenced in the SQL stmt.
• A means of moving data from and to DB2 tables
• DCLGEN produces host variables, the same as the
columns of the table
Host Variables
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 103/157
CTS-PAC Version 1.1 103
Host Variables
Can be used in• „INTO‟ CLAUSE OF SELECT & FETCH
STATEMENTS
• AS INPUT OF „SET‟ CLAUSE OF UPDATE STMTS • AS INPUT FOR THE „VALUES‟ CLAUSE OF
INSERT STATEMENT
• IN WHERE CLAUSE OF SELECT, INSERT,
UPDATE & DELETE
• AS LITERALS IN SELECT LIST OF A SELECT
STATEMENT
Example
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 104/157
CTS-PAC Version 1.1 104
a p e
• SELECT Cust_No, Cust_name, Cust_addrFROM CUSTOMER
INTO :H-Cust-No, :H-Cust-name, :H-Cust-addr
WHERE Cust_No = :H_Cust_No;
Embedded SQL statements
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 105/157
CTS-PAC Version 1.1 105
Q
• It is like the file I/O• Normally the embedded SQL statements contain the
host variables coded in the INTO or SELECT .... as
shown above
• they are preceded by EXEC SQL
• SELECT, INSERT, UPDATE & DELETE stmts can
be coded inline
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 106/157
Cursors
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 107/157
CTS-PAC Version 1.1 107
• DECLARE : name assigned for a particular SQL stmt• OPEN : readies the cursor for row retrieval; sometimes
builds the result table.However it does not assign
values to the host variables
• FETCH : returns data from the results table one row at
a time and assigns the value to specified host variables
• CLOSE : releases all resources used by the cursor
DCLGEN
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 108/157
CTS-PAC Version 1.1 108
• issued for a single table• prepares the structure of the table in a COBOL
copybook
• The copybook contains a „SQL DECLARE TABLE‟
stmt along with a working storage host variable defn
for the table
Precompile
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 109/157
CTS-PAC Version 1.1 109
p
• searches all the SQL stmts and DB2 related INCLUDEmembers and comments out every SQL stmt in the
program
• the SQL stmts are replaced by a CALL to the DB2
runtime interface module, along with parameters.
• All SQL statements are extracted and put in a Database
Request Module (DBRM)
Contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 110/157
CTS-PAC Version 1.1 110
• places a time stamp in the modified source and theDBRM so that these are tied. If there is a mismatch in
this a runtime error of „-818„, timestamp mismatch, is
got
• all DB2 related INCLUDE stmts must be placedbetween EXEC SQL & END EXEC keywords for the
precompiler to recognize them
Compile & Link
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 111/157
CTS-PAC Version 1.1 111
p
• modified precompiler COBOL output is compiled• compiled source is link edited to an executable load
module
• appropriate DB2 host language interface module
should also be included in the link edit step(i.e
DSNALI)
Bind
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 112/157
CTS-PAC Version 1.1 112
• A type of compiler for SQL statements• It reads the SQL statements from the DBRM and
produces a mechanism to access data (in an efficient
manner) as directed by the SQL statements being
bound
• Checks syntax, checks for correctness of table &
column definitions against the catalog info & performs
authorization validation
Bind Types
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 113/157
CTS-PAC Version 1.1 113
yp
• BIND PLAN : accepts as input one or more DBRMsand outputs an application plan containing executable
logic representing optimized access paths to DB2 data.
• BIND PACKAGE : acceps as input a single DBRM
and produces a single package containing theoptimized access path. The PLAN in this case contains
a reference to the physical location of the package(s).
What is a Package ?
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 114/157
CTS-PAC Version 1.1 114
• It is a single bound DBRM with optimized access paths• It also contains a location identifier, a collection
identifier and a package identifier
• A package can have multiple versions, each with its
own version identifier
Advantages of Package
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 115/157
CTS-PAC Version 1.1 115
• Reduced bind time• can specify bind options at the programmer level
• versioning
• provides for remote data access(in version DB2 V2.3or higher)
Data Control language
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 116/157
CTS-PAC Version 1.1 116
• GRANT & REVOKE• GRANT : grants the table privileges, plan & package
privileges, collection privileges, database privileges,
use privileges and system privileges
• user with a SYSADM privilege will be responsible for
overall control of the system
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 117/157
CTS-PAC Version 1.1 117
• Format of GRANT :GRANT SELECT, UPDATE(NAME,NO)
ON TABLE EMPL
TO A, B, C(or PUBLIC);GRANT ALL ON EMPL TO PUBLIC;
GRANT EXECUTE ON PLAN PLANA TO USER;
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 118/157
CTS-PAC Version 1.1 118
• The table privileges allowed are SELECT, UPDATE,DELETE, INSERT, (both base tables & views),
ALTER(Table) & (Create)INDEX(only to base tables)
• There are no specific DROP privilages;the table can be
dropped by its owner or a SYSADM
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 119/157
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 120/157
CTS-PAC Version 1.1 120
• REVOKE : this stmt revokes the privileges given to auser. The user granting the privileges has the authority
to REVOKE also.
• It is not possible to be column specific when revoking
an UPDATE privilege
REVOKE SELECT ON TABLE EMPL FROM
USERA;
For the following refer handout
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 121/157
CTS-PAC Version 1.1 121
• List of common SQL return codes and solutions• JCL‟s for bind, compile of DB2 program
Application development guidelines
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 122/157
CTS-PAC Version 1.1 122
• Code modular DB2 programs and make them as smallas possible
• use unqualified SQL stmts;this enables movement from
one environment to another(test to prodn)
• Never use Select* in an embedded SQL program;
• use joins rather than subqueries
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 123/157
CTS-PAC Version 1.1 123
• use WHERE clause and filter out data• use cursors when fetching multiple rows, though they
add overheads
• use FOR UPDATE OF clause for UPDATE or
DELETE with cursor - this ensures data integrity.
• use INSERTs minimally ; use LOAD utility instead of
INSERT, if the inserts are not application dependent
QMF - Query Management Facility
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 124/157
CTS-PAC Version 1.1 124
• It is an MVS- and VM- based query tool• allows end users to enter SQL queries to produce a
variety of reports and graphs as a result of this query
• QMF queries can be formulated in several ways : by
direct SQL stmts, by means of relational prompted
query interface or by query-by-example (QBE). QBE is
similar to SQL in some ways but more user friendly
SPUFI
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 125/157
CTS-PAC Version 1.1 125
• supports the online execution of SQL statements from aTSO terminal
• used for developers to check SQL statements or view
table details
• Spufi menu contains the input file in which the SQL
statements are coded, option for default settings and
editing and the output file.
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 126/157
CTS-PAC Version 1.1 126
Session 4
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 127/157
CTS-PAC Version 1.1 127
Topics to be covered in this Session
The duration of this session is 0.5 days
• DB2 Utilities
• DB2 Security• DB2 catalog & Optimizer
• Performance tuning
DB2 System administration
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 128/157
CTS-PAC Version 1.1 128
• DB2 UTILITIES• CHECK
• COPY, MERGECOPY
• RECOVER
• LOAD
• REORG, RUNSTATS
• EXPLAIN
Check
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 129/157
CTS-PAC Version 1.1 129
• checks the integrity of DB2 data structures• checks the referential integrity between two tables and
also checks DB2 indexes for consistency
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 130/157
CTS-PAC Version 1.1 130
• can delete invalid rows and copies them to a exceptiontable
• Use CHECK DATA when loading a table without
specifying the „ENFORCE CONSTRAINTS‟ option or
after the partial recovery of tablespaces in a referentialset
Copy
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 131/157
CTS-PAC Version 1.1 131
• used to create an imagecopy for the completetablespace or a partition of the tablespace - full
imagecopy or incremental imagecopy
• every succesful execution of COPY utility places in the
table SYSIBM.SYSCOPY, atleast one row thatindicates the status of the imagecopy
Mergecopy
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 132/157
CTS-PAC Version 1.1 132
• The MERGECOPY utility combines multipleincremental image copy data sets into a new full or
incremental image copy data set
Recover
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 133/157
CTS-PAC Version 1.1 133
• Standard unit of recovery is a Tablespace• restore DB2 tablespaces and indexes to a specific
instance
• data can be recovered for single pages,pages that
contain I/O errors, a single partition or an entire
tablespace
• indexes are always recovered from the actual table
data, not from image copy and log data, as in the caseof tablespace recovery
Load
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 134/157
CTS-PAC Version 1.1 134
• to accomplish bulk inserts into DB2 table• can replace the current data or append to it .i.e. LOAD
DATA REPLACE or LOAD DATA RESUME(S)
• if a job terminates in any phase of LOAD REPLACE
the utility has to be terminated and rerun
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 135/157
CTS-PAC Version 1.1 135
• if a job terminates in any phase other thanUTILINIT(which sets up and initializes the LOAD
utility), the tablespace must be first restored using the
full RECOVER, if LOG NO option of the LOAD was
mentioned.. After the tablespace is restored, the error isto be corrected, the utility terminated and the job rerun.
Reorg
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 136/157
CTS-PAC Version 1.1 136
• to reorganize DB2 tables and indexes and therebyimproving their efficiency of access
• reclusters data, resets free space to the amount
specified in the „create ddl‟ statement and deletes and
redefines underlying vsam datasets for stogroupdefined objects
Runstats
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 137/157
CTS-PAC Version 1.1 137
• collects statistical information for DB2 tables,tablespaces, partitions, indexes, and columns.
• it can place this info in the catalog tables with DB2
optimizer statistics or DBA monitoring statistics or
with all statistics that have been gathered
• it can be used on specific SQL queries without updting
the current usable statistics
Reorg Job stream
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 138/157
CTS-PAC Version 1.1 138
• the total reorg schedule should include• a RUNSTATS job or step : to record current tablespace
and index statistics to DB catalog
• two copy steps for each tablespace being reorganized :
so that data is recoverable. The second copy job is
required after the REORG if it was performed with a
LOG NO option
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 139/157
CTS-PAC Version 1.1 139
• After a REORG is run with LOG NO option, DB2turns on the copy pending status flag for tablespaces
specified in the REORG.
• When LOG NO parameter is specified it is better to
take a imagecopy of the tablespace being reorganizedimmediately after reorg
• a REBIND job for all plans using tables in any of the
tblspaces being organized
Explain
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 140/157
CTS-PAC Version 1.1 140
• this feature can be detail the access paths chosen by theDB2 optimizer for SQL statements
• used for performance monitoring
• When EXPLAIN is requested the access paths that the
DB2 chooses are put in coded format into the table
PLAN_TABLE, which is created in the default
database
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 141/157
CTS-PAC Version 1.1 141
• To EXPLAIN a single SQL stmt precede that SQL stmtwith the EXPLAIN Command
EXPALIN ALL SET QUERYNO = integer
FOR SQL stmt
• the other method is specifying EXPLAIN YES with the
Bind command
• then PLAN_TABLE is to be queried to get the required
information.
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 142/157
CTS-PAC Version 1.1 142
• the information provided include the type of access of particualar tables used in the SQL or Package or Plan,
the order in which the tables or joined in a JOIN,
whether SORT is required and so on
• Since the EXPLAIN results are dependent on the DBcatalog, it is better to run RUNSTATS before running a
EXPLAIN
DB2 Security
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 143/157
CTS-PAC Version 1.1 143
• LOCKING SERVICES :These are provided by an MVS subsystem called the
IMS resource Lock Manager(IRLM).
It is used to control concurrent access DB2 data,
regardless of whether IMS is present in a system or not.
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 144/157
CTS-PAC Version 1.1 144
• The above is based on Transaction Processing - thesystem component that provides this is „A
TRANSACTION MANAGER‟
• COMMIT & ROLLBACK are key methods of
implementing this
Explicit locking facilities
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 145/157
CTS-PAC Version 1.1 145
• the SQL statement LOCK TABLE• the ISOLATION parameter on the BIND PACKAGE
command - the two possible values are RR(„Repeatable
Read‟) & CS(„Cursor Stability‟)
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 146/157
CTS-PAC Version 1.1 146
• the tablespace LOCKSIZE parameter - physically DB2locks data in terms of pages or tables or tablespaces.
This parameter is specified in „CREATE or ALTER
Tablespace‟ option „LOCKSIZE‟. The options are
„Tablespace‟, „Table‟, „Page‟ or „Any‟
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 147/157
CTS-PAC Version 1.1 147
• the ACQUIRE/RELEASE parameters on the BINDPLAN command specifies when table locks(which are
implicitly acquired by DB2) are to be acquired and
released.
• Types : ACQUIRE USE & ACQUIRE ALLOCATE• RELEASE USE & RELEASE ALLOCATE
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 148/157
CTS-PAC Version 1.1 148
Session 5
T i t b d i thi S i
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 149/157
CTS-PAC Version 1.1 149
Topics to be covered in this Session
The duration of this session is 0.5 days
• DB2 Catalog & Directory
Catalog Tables & the DB2 directory
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 150/157
CTS-PAC Version 1.1 150
• Repository for all DB2 objects - contains 43 tables
• Each table maintains data about an aspect of the DB2
environment
• The data refers to info about tablespaces, tables,
indexes, privileges, on utilities run on DB2 and so oneg : SYSIBM.SYSTABLES,
SYSINDEXES/SYSCOLUMNS ......‟
contd...
• Wh t d d DB2 SQL i d th DB2 t l i
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 151/157
CTS-PAC Version 1.1 151
• When standard DB2 SQL is used, the DB2 catalog is
either accessed or updated. eg. When a „CREATETABLE‟ stmt is issued the catalog tables
SYSIBM.SYSTABLES, SYSIBM.SYSCOLUMNS &
SYSIBM.SYSFIELDS are updated.
• However the DB2 catalog is semi active only. This isbecause updates to number of rows, the physical order
of the rows for a set of keys and the like are updated
only after running a RUNSTATS utility
• DB2 catalog is integrated - DB2 catalog and DB2
DBMS are inherently bound together
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 152/157
CTS-PAC Version 1.1 152
• It isnonsubvertible
- DB2 catalog cannot be updated behind DB2‟s back. i.e. if a table of 10 columns is
created, it is not possible to go and change the number
of columns directly on the catalog to 15. It has to be
done using the standard SQL statements for droppingand recreating the table
DB2 Optimizer
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 153/157
CTS-PAC Version 1.1 153
• Analyzes the SQL statements and determines the mostefficient way to access data - gives Physical data
independence
• It evaluates the following factors : CPU cost, I/O cost,
DB2 catalog statistics & the SQL statement• it estimates CPU time, cost involved in applying
predicates, traversing pages and sorting
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 154/157
CTS-PAC Version 1.1 154
•It estimates the cost of physically retrieving and writing
the data
• The information pertaining to the state of the tables that
will be accessed by the SQL statements are provided
by the Catalog
Performance Tuning
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 155/157
CTS-PAC Version 1.1 155
•The performance of an application can be monitored
and enhanced in the application, as well as database
level
• In application side the SQL‟s can be tuned to make
them more efficient, and avoid redundancy• It is better to structure the SQLs so that they perform
only the necessary operations
contd...
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 156/157
CTS-PAC Version 1.1 156
•On the database side, the major enhancements can be
done to the definitions of tables, indexes & the
distribution of tablespace and indexspace
• The application run statistics are obtained from
EXPLAIN or DB2PM monitor report
8/4/2019 DB2 - IBM s Relational DBMS
http://slidepdf.com/reader/full/db2-ibm-s-relational-dbms 157/157