Professional Documents
Culture Documents
Course Objective
To illustrate DB2 Architecture To explain DB2 Objects Database, Table space, Table, Index Datatypes DB2 support Integrity Foreign keys Security Views Grant and Revoke
Course Objective
Embedded SQL Program Preparation Precompile, Bind DBRM's, Plans, and Packages Compile a Cobol-DB2 Program Concurrency and utilities
Course Plan
Day 1: Overview of DB2 Day 2: SPUFI, DCLGEN & Embedded SQL Day 3: Program Preparation and Execution Day 4: OLTP Issues Day 5: Project Evaluation and Final Test
Prerequisites
MVS and TSO Programming in COBOL RDBMS Concepts Working knowledge of SQL OLTP Concepts
Session Plan
To illustrate DB2 Architecture To explain DB2 Objects Database, Tablespace, Table, Index Datatypes DB2 support Integrity Foreign keys Security Views Grant and Revoke
RDBMS Review
Database Database Management System Database Models Relational Database Management System
Introduction to DB2
What is DB2? A subsystem of the MVS operating system An abbreviation for IBM Database 2 Was announced in June 1983 Supports SQL (Structured Query Language)
System Architecture
Major Components of DB2 SSAS (System Services Address Space) Thread creation, Program tracing, Logging DBAS (Database Services Address Space) Execution of SQLs, Database objects management, buffer management, Data read/write, etc. IRLM (IMS Resource Lock Manager) Locking
System Architecture
Major Components of DB2 DDF (Distributed data facility component ) is optional Distributed Database functionality SPAS (Stored Procedure Address Space) For the execution of the stored procedures Each of these components runs in a separate address space and is made up of numerous sub-components.
10
DB 2 System Architecture
DSNMSTR SSAS
Logging Database functions Attachment co-ordination Buffering
DSNDBM1 DBAS
IRLMPROC IRLM
Locking
Distributed Requests
Free Space
Free Space
Free Space
Free Space
11
Control and co-ordinate connections to other MVS subsystems such as CICS, IMS and TSO.
Manages the System log. The system log is a set of datasets that used to record information about every transaction.
12
The instrumentation facility gathers statistical information about the work done by DB 2 at user specified intervals of time.
This information is either Written to a System Management facility (SMF) or Generalized Trace Facility (GTF) dataset or Passed to a performance monitor program provided some third party vendor.
13
15
STAGE 1 PREDICATES
COLUMN_NAME operator VALUE COLUMN_NAME IS NULL COLUMN_NAME BETWEEN val1 AND val2 COLUMN_NAME IN List COLUMN_NAME LIKE pattern COLUMN_NAME LIKE :Host-variable A.COLUMN_NAME1 operator B.COLUMN_NAME2 COLUMN_NAME Operator (non correlated sub query) COLUMN_NAME Operator (non column expression)
16
STAGE 1 / 2 PREDICATES
CORRELATED SELECT EMP,LASTNAME FROM EMP A WHERE EXISTS (SELECT 1 FROM DEPT B WHERE A.DEPTNO=B.DEPTNO AND DEPTDESC=EDUCATION) NON CORRELATED SELECT EMP,LASTNAME FROM EMP A WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE DEPTDESC=EDUCATION)
17
Apply Stage 2 Predicates & Sort Data Apply Stage 1 Predicates VSAM Media Manager DATA
18 ER/CORP/CRS/DB01/003 Version No:2.0b
Data Manager
Buffer Manager
Copyright 2005, Infosys Technologies Ltd
The locking services are provided by a component called Integrated Resource Lock Manager (IRLM).
19
Is an optional component.
20
Threads
Are memory structures used by DB 2 to communicate with an application program.
DB 2 communicates the status of each SQL statement to the application using threads through SQLCA.
21
Threads
CICS PGM DB2 Utility TSO O/L PGM
DB2
IMS/DC PGM
QMF Or DB2I
ER/CORP/CRS/DB01/003 Version No:2.0b
DB2 Objects
DATABASE
TABLESPACE
TABLE
INDEX
SYNONYM
VIEW
Copyright 2005, Infosys Technologies Ltd 23 ER/CORP/CRS/DB01/003 Version No:2.0b
DB 2 Objects Hierarchy
STOGROUP DATABASE TABLESPACE VIEW ALIAS SYNONYM TABLE INDEX
COLUMN
24
Storage Groups
Are a collection of one or more (maximum of 133) DASD volumes of the same type.
25
ALTER STOGROUP STOUDB6 ADD VOLUMES (DAVP9F, DAVP8C, DAVP9E) REMOVE VOLUMES (DAVP7C, DAVP8C) PASSWORD infosys;
26
Database
Generally every application will have a unique database. This simplifies administrative tasks and also improves application performance.
27
Creating a Database
To create a database use CREATE DATABASE EMPDB; Maximum 8 characters name
28
Stores control and descriptive information about every object in the corresponding database.
To maintain database integrity the DBD is locked whenever an object is being created, updated or deleted in the corresponding database.
Instead of accessing DB2 catalog for object information , DBD which is housed in DB2 directory is accessed which is more efficient.
29
Tablespace
A Tablespace is one or more VSAM datasets
30
Creating a TS
Created under an existing DB using CREATE TABLESPACE EMPTS IN EMPDB PRIQTY 10000 SECQTY 1000 PCTFREE 10 FREEPAGE 63 LOCKSIZE ANY BUFFERPOOL BP0 SEGSIZE 64
31
More on TS
To remove a tablespace use DROP TABLESPACE EMPTS To change the definition use ALTER TABLESPACE EMPTS
ALTER TABLESPACE DBENR.ENR PRIQTY 200 SECQTY 200 ERASE YES LOCKSIZE ANY BUFFERPOOL BP1;
32
Simple TS
Can house one or more tables There is no limit for number of tables Rows from multiple tables can be interleaved on a page
33
Segmented TS
Can house one or more tables TS is divided into segments of 4 to 64 pages in increments of 4 One segment contains data from exactly one table One table can occupy many segments Good for performance and this is DB2 default
34
Partitioned tablespace
allows a table to be divided by rows into partitions Each partition can be placed on different storage devices
35
Table
A tablespace within a database can have any number of tables Create table using CREATE TABLE EMPLOYEE (EMP_NO EMP_NAME SMALLINT CHAR(15), WITH DEFAULT, NOT NULL,
36
More on Table
To change the definition use ALTER TABLE EMPLOYEE For example, to add a new field ALTER TABLE EMPLOYEE ADD EMP_SALARY INTEGER
37
Data types
Data Type
String
Datetime
Numeric
Character
Graphic
Date
Timestamp
Decimal
Single
Double
38
39
Varchar(n) max. 4046 bytes A structure containing PIC S9(4) COMP for length and PIC X(n) for the data
40
41
Nulls
DB2 adds an extra byte to all nullable columns This extra byte has the information whether the field contains NULL or not The NULL values do not participate while taking AVERAGE, SUM, etc. Need to have special care while inserting, updating, or retrieving nullable fields in the host language program
42
WITH DEFAULT
DB2 puts the default value of the data type in that field while inserting a record For character fields, spaces, for numeric fields, zeros, for date fields, current date for time fields, current time ... and so on
43
Index
Is a structure used for faster retrieval of data. Can be unique or non-unique. In DB 2, we need to explicitly create a unique index for the primary key. Is stored in B - Tree format.
44
Root Page Level 0 1000 2000 3000 4000 NL1 NL2 NL3 NL4
Non-Leaf Page2 Level 1 1100 1200 1300 1400 L11 L12 L13 L14
Index
Leaf Page 2 Level 2 101 DP10.1 102 DP10.2 ........... 151 DP20.1 Data Page 20 151 Bhavana 20 Copyright . . . . . . . . . . . . . .2005,. . . .
Infosys Technologies Ltd 45
Leaf Page 12 Level 2 1101 DP50.1 1102 DP50.2 ............ ............ Data Page 50 1101 Vijay 22 1102 Harish 25 ER/CORP/CRS/DB01/003 . . . . . . No:2.0b. . . . . . . . Version . . . .
Index (Contd)
Root Page Only one root page is available per Index.It should exist at the highest level of the hierarchy.It can be structured as Leaf pages or Non leaf pages.
Non Leaf pages Non Leaf pages are intermediate-level Index pages in the b-tree hierarchy. It need not exist.If they exist they contain the pointers to the Leaf pages.
Leaf Pages Leaf pages contain pointers to the data rows of a table.Leaf page must always exist. In a single page Index, the root page is a Leaf page
Copyright 2005, Infosys Technologies Ltd 46 ER/CORP/CRS/DB01/003 Version No:2.0b
Index (Contd)
Columns that are good for indexing
1. 2. 3. 4. Primary key and Foreign key columns, Column having unique values, Columns that are frequently used in the WHERE clause and Columns that frequently used in an ORDER BY, GROUP BY and DISTINCT clauses.
Creating Index 1 of 2
DB2 creates Index spaces for every index (one index space for one index)
Is a page set used to store information about one index. Only one index per index space. Index space pages are 4k pages. Contains value from indexed columns and an RID to the corresponding row.
48
Creating Index 2 of 2
An index and its associated table must be in the same database Index pages can be locked by sub page increments (1/2, 1/4, 1/8 and 1/16). The default is 1/4. Often the response time will be slow due to Indexspace lock contention rather than Tablespace lock contention. In such cases the response time can be improved by increasing the value of the SUBPAGE parameter.
Indexes are created using CREATE [UNIQUE] INDEX EMPNOINDX ON EMPLOYEE(EMP_NO ASC)
49
More on Index
50
Clustered index (1 of 2)
Data is physically ordered in the sequence of the index.
Good for columns used in BETWEEN , >, <, LIKE used in GROUP BY, ORDER BY, DISTINCT for PRIMARY and FOREIGN KEYS
51
CREATE INDEX CLUSTER_EMP_INX ON ENR.EMP (EMPNO ASC) PRIQTY 36 CLUSTER (PART 1 VALUES('H99'), PART 2 VALUES('P99'), PART 3 VALUES('Z99'), PART 4 VALUES('999')) BUFFERPOOL BP1;
52
Catalog Tables
When tables and other objects are created, DB2 records all these information into a set of system tables called catalog tables SYSDATABASE, SYSTABLESPACE, SYSTABLES, SYSCOLUMNS, SYSINDEXES, SYSVIEWS, etc. are examples If you have authority, you can query on these tables as any other table
53
Aliases can refer to both local as well as remote tables. In fact aliases were designed for distributed environment to avoid references to the location qualifier.
When a table is dropped all the synonyms get dropped automatically where as aliases on that table remains.
54
55
Integrity
56
Referential Integrity
Suppose you want to make sure that all the employee numbers in PROJ_ALLOCATION(PROJ_ID,EMP_NO) are valid employees and valid projects (i.e., they belong to EMPLOYEE(EMP_NO,EMP_NAME) and PROJECT(PROJ_ID,PROJ_NAME) master tables
You use referential integrity support DB2 provides to enforce this (through definition of foreign keys)
57
A unique key is defined as a column (or set of columns) where no two values same.
The columns of a unique key cannot contain null values. A table can have multiple unique keys. Unique keys are optional and can be defined in CREATE TABLE or ALTER TABLE statements.
58
Primary Key
A primary key is a unique key that is a part of the definition of the table. A table cannot have more than one primary key, and the columns of a primary key cannot contain null values.
Primary keys are optional and can be defined in CREATE TABLE or ALTER TABLE statements.
59
60
PRIMARY KEY(PROJ_ID,EMP_NO), FOREIGN KEY PROJ_FK(PROJ_ID) REFERENCES PROJECT(PROJ_ID), FOREIGN KEY EMP_FK(EMP_NO) EMPLOYEE(EMP_NO) ) REFERENCES
61
Delete Rules
What happens if one employee is deleted from employee master table?
DB2 supports 3 delete rules DELETE RESTRICT DELETE CASCADE DELETE SET NULL
When defining foreign keys give these rules FOREIGN KEY PROJ_FK(PROJ_ID) REFERENCES PROJECT(PROJ_ID) DELETE CASCADE
62
Referential Integrity
Employee Table
Invalid Record
1005
Primary key
Usha
Department Table
63
Empname Dept No Tim 1000 Jim 1000 Tom 2000 Mary 1000
64
Empname Dept No Tim 1000 Jim 1000 Tom 2000 Mary 1000
65
66
67
Security
68
Security
Security means the protection of the data in the data base against unauthorized disclosure, alteration or destruction.
69
Views
Unlike actual tables, views defined on a table are just definitions DB2 keeps all view definitions in SYSVIEWS catalog table All the views depend on one or more tables or views We can have views created using other views
70
Creating a View
CREATE VIEW RICH_EMP AS SELECT EMP_NO, EMP_NAME FROM WHERE EMPLOYEE SALARY > 25000
Now we can use SELECT EMP_NO, EMP_NAME FROM RICH_EMP in our program
71
72
More on View
Like any other object, use DROP VIEW RICH_EMP to delete a view When a table or a view is dropped all dependent views are automatically dropped DB2 does not allow you to create views with ORDER BY, FOR UPDATE OF, or UNION
73
GRANT
Use GRANT keyword to grant permissions on database objects GRANT SELECT ON EMPLOYEE TO PUBLIC; GRANT UPDATE (EMP_ADDRESS) ON EMPLOYEE TO SMITH; GRANT INSERT, DELETE ON EMPLOYEE TO TRAINEES; A table creator has implicit authority to alter, drop, create a view/index, select/insert/update/delete
74
REVOKE
Use REVOKE keyword to take away permissions on database objects
REVOKE SELECT ON EMPLOYEE FROM PUBLIC; REVOKE UPDATE (EMP_ADDRESS) ON EMPLOYEE FROM SMITH; REVOKE INSERT, DELETE ON EMPLOYEE FROM TRAINEES; REVOKE ALL ON EMPLOYEE FROM PUBLIC;
75
More on Permissions
You can GRANT a privilege to someone with GRANT option GRANT ALL ON EMPLOYEE TO PUBLIC WITH GRANT OPTION;
Suppose Jones grants select permission on view RICH_EMP to Smith with grant option; Smith in turn grants select permission to Adams on the same view; What happens if Jones revokes permission from Smith?
76
BUNDLED PRIVILEGES
SYSDM SYSCTRL DBADM DBCTRL DBMAINT SYSOPR
77
Summary
DB2 Architecture DB2 Objects Database, Tablespace, Table, Index Datatypes DB2 support Integrity Foreign keys Security Views Grant and Revoke
78
Thank You!
Copyright 2005, Infosys Technologies Ltd 79 ER/CORP/CRS/DB01/003 Version No:2.0b