You are on page 1of 6

* Advantages of DB2

Accessing is easy and fast


Understanding data is easy
High Independency
High Concurency
Low Redendency
Works under different operating systems

* DB2 Storage structure -


Storage groups > Data Bases > Table Spaces > Tables > Views

* STORAGE GROUP is nothing but collection of same type of volumes (upto 133
volumes)
* DATABASE is structure to store tables, tables space and indexes
* TABLE SPACE is collecction of tables
* TABLE name can be 128 bytes long
Column name length can be upto 30 char
* VIEW - A Table which can be derived from one or more tables(max 15) based on
selection criteria

---

* DBRM - Database Request Module - where syntax free SQL are stored

* PRE compile (DSNHPC) SQL seperated into DBRM


> Compile(IGYCRCTL) obj module created
> Link edit (IEWL) takes load mod and sub program load mod and
generates final load mod
> Bind (IKJEFT01) binding DBRM SQL statements with DB2 catalog
entry and check access and check syntax - creates PACKAGE
> Run JCL (IKJEFT01)

* PACKAGE - component between DBRM and PLAN - containes OAP - Optimised Access
Path, generated by optimiser, a non executable component.
when any change in sub program just compile is enough, no need to bind again

* BIND package or plan JCL inputs -


//DBRMLIB (DBRM library) and
//SYSTSIN DD *
DSN SYSTEM (system name)
BIND PACKAGE (package name) -
[PLAN (plan name) - ]
MEMBER (DBRM name) -
ACTION (REPLACE/ADD) - --> specify when run the jcl
again should o/p be replaced or add
ISOLATION (CS/RR/RS/UR) -
VALIDATE (RUN/BIND) -
RELEASE (COMMIT/DEALLOCATE) -
EXPLAIN (YES/NO) -
OWNER (owner id) -
QUALIFIER (qualifier for tables, plan etc for remote system) -
ENCODING (ASCII/EDCDIC/UNICODE)
END
/*

* RUN COBOL+DB2 ex.


Batch process -
IKJEFT01 is used to run the program
SYSIN -
DSN SYSTEM(db2-region)
RUN PROG(PGM1) PLAN (PLAN1) -
LIB(DBRM library)

* ISOLATION parameter in BIND process - how the program execution is isolated for
accessing the database which is accessed by other program concurrently
CS - Cursor Stability - DEFAULT - THE row currently being refered
RR - Repeatable read - every row referenced by current transaction
UR - Uncomited read - row is locked only when a transaction tries to update or
delete

* HOST VARIABLES - DB2 is an external system and COBOL can not access the data
directly using SQL so intermediate variables are created
* DCLGEN is a tool used to generate COBOL copybook for a table

==COBOL== ==DB2==
S9(4) COMP SMALLINT
S9(9) COMP INT
S9(18) COMP BIGINT
S9(m)V9(n) COMP-3 DECIMAL(m+n,n), NUMERIC(m+n,n)
X(n) CHAR(n)
X(n)+2 VARCHAR(n)
X(10) DATE
X(8) TIME
X(26) TIMESTAMP

* SQLCA - SQL Communication Area will include return codes, error details and codes
after DB2 query execution
* SQLCODE - INTEGER - return codes of SQL statment (0 successfull, + successfull
but warning - error)
* SQLERRD(3) - INTEGER - contains number of rows executed by particular query
* Length of SQLCA is 136

---

* DDL - Data Definition Language - CREATE, ALTER, DROP database


objects
* DSL - Data Selection Language - SELECT data inside
table
* DML - Data Manipulation Language - INSERT, UPDATE, DELETE data inside
table
* DCL - Data Control Language - GRANT, REVOKE controlling
access
* TCL - Transaction Control Language - COMMIT and ROLLBACK to
save or revoke transactions applied to database

* DDL
-> CREATE STOGROUP <name>
VOLUMES (vol-1, vol-2, etc)
VCAT <vcat name>

-> CREATE DATABASE <db name>


STOGROUP <stogrp name>
BUFFERPOOL <buffer name> - multiples of 4 (default 4K)
INDEXBP <index name>
-> CREATE TABLESPACE <name>
IN <db name>
[USING STOGROUP <stgrp name>]
[SEGSIZE 20]
[NUMPARTS 2
(PART 1 USING STOGROUP <stgrp name>
PRIQTY 100
SECQTY 100
FREEPAGE 10
PCTFREE 10,
PART 2 USING STOGROUP <stgrp name>
PRIQTY 100
SECQTY 100
FREEPAGE 10
PCTFREE 10)
]
BUFFERPOOL <BP name>
MAXROWS 200
CLOSE YES

-> CREATE TABLE mobile_table


(brand_name CHAR(20) NOT NULL,
model_no NUMERIC(5,2) NOT NULL WITH DEFAULT 000.00,
PRIMARY KEY(model_no))
IN DATABASE1.TABLESPACE1

-> CREATE VIEW user_detail_v1 AS


(SELECT * FROM user_detail_table )
ON DATABSE1.TABLESPACE1
[READ ONLY]

-> PRIMARY KEY (column_1, column_2)


-> FOREIGN KEY (column_1, column_2) REFERENCES parent_table
CASCADE/RESTRICT/SET NULL

* DML
Select row/rows
SELECT column_1, column_2
INTO :column-1, :column-2
FROM table_1
WHERE column_3 = :column-3

(WHERE column_3 NOT = :column-3


WHERE column_3 BETWEEN(10,20) - only for numeric/date columns
WHERE column_3 IN(10,20) - only for numeric
WHERE column_3 LIKE '%DRA' - for searching DRA at end
WHERE column_3 LIKE 'SHY%' - for searching SHY at start
WHERE column_3 LIKE '%LEN%' - for searching LEN at middle)

Usin alias to concatinate


SELECT first_name || ' ' || middle_name || ' ' || last_name
full_name
FROM authors;

Select top 10 rows from 11th record


SELECT title, rating
FROM books
ORDER BY title
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;

Select next 10 rows from 11th record


SELECT title, rating
FROM books
ORDER BY title
LIMIT 10 OFFSET 10;

Remove duplicate rows


SELECT category
FROM product
GROUP BY category;

Select only duplicate rows


SELECT category
FROM product
GROUP BY category
HAVING COUNT(category) >1; <--- Note here WHERE clause can not
be used on agregate function COUNT so we have to use HAVING

Select top 2 books by rating per publisher - common table expression


WITH cte_books AS (
SELECT publisher_name,
ROW_NUMBER() OVER (
PARTITION BY publisher_name
ORDER BY rating DESC
) row_num,
rating
FROM
books
WHERE
publisher_name IS NOT NULL
)
SELECT *
FROM cte_books
WHERE row_num <= 2;

Retrieve top N salay


Select

Insert row
INSERT INTO table_1
SELECT column_1
FROM table_2
WHERE condition
Insert value
INSERT INTO table_1 (column_1, column_2)
VALUES (:column-1, :column-2)

Update
UPDATE table_1
SET column_1 = :column-1
column_2 = :column-2
WHERE condition

Delete
DELETE FROM table_1
WHERE condition

* DCL
GRANT SELECT ON TABLE1 TO PUBLIC
REVOKE SELECT ON TABLE1 TO PUBLIC

----

* SQL codes -
+000 successful
+100 row not found when SELECT/UPDATE/DELETE for a condition, reached end of
ROW in CURSOR FETCH

-204 ***** is an unidentified name (means table or column name in select


statement does not exist)

-305 null value exception (SELECT returning null values but null indicator
not specified in INTO clause)
-313 mismatch in number of host variables and selected columns

-413 length of number is more than the max length of INT,DECIMAL column
-433 length of char is more than the max length of char, varchar column

-803 Duplicate primary key


-811 multiple rows found (when cursor not defined)
-805 plan not found/ resource not available

-501 calling fetch when cursor not oppened


-502 cursor already opened - now trying to open again

-911 Deadlock with timeout


-913 Deadlock with rollback
-922 Access error

----

DECLARE cursor-1 CURSOR FOR


ROWSET POSITIONING FOR
SELECT
column_1
column_2
FROM table_1
WHERE condition
[FOR UPDATE OF column_3 --> is required when curor is used to update instead
of just fetch]

FETCH NEXT ROWSET FROM cursor-1 FOR 100 ROWS


INTO
:column-1
:column-2

----

* DEADLOCK - When multiple programs are using same resources or each other's
resource

You might also like