artificerpi

DB2

 

一、DB2 概述

1. SPUFI (SQL Processor Using file input)

主要需要了解实验上的一些操作

参考:https://www-01.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.apsg/src/tpc/db2z_executesqlspufi.dita

clip_image002

2. DB2 data Structure & data Object

Data Structure Overview

clip_image004

Buffer Pool

DB2 has four kinds of page size for data buffers:

clip_image006

Types of Table Spaces

  • Simple table spaces
  • Segmented table spaces
  • Partitioned table spaces
  • Universal table spaces
  • Large object table spaces (LOB)
  • XML table spaces

 

Data Type

clip_image008

datatype <——> cobol:

Integer

    bytes (5 if nullable) PIC S9(9) COMP

Smallint

    bytes (3 if nullable) PIC S9(4) COMP

Char( n )

     max. 254 bytes PICX( n )

 Varchar( n )

     max. 4046 bytes A structure containing structure containing PIC S9(4) COMP for length and PIC X( n ) for the data

Time

     3 bytes (4 if nullable) PICX(8) PIC X(8)

Date

     4bytes (5if nullable) 4 bytes (5 if nullable) PIC X(10)

null attribute

   default value.

DB2 Object map

clip_image010

 

3. DB2 Utilities

clip_image012

 

二、Program Structure

      ******************************************************************
      * Author:
      * Date:
      * Purpose:
      * Tectonics: cobc
      ******************************************************************
       IDENTIFICATION DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       PROGRAM-ID. YOUR-PROGRAM-NAME.
       ENVIRONMENT DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           EXEC SQL INCLUDE SQLCA END-EXEC
           EXEC SQL INCLUDE COPYBOOK-DEPTDL END-EXEC
      *-----------------------
       PROCEDURE DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       MAIN-PROCEDURE.
            MOVE 'A00' TO DEPTNO.
            EXEC SQL
            SELECT DEPTNO,DEPTNAME,MGRNO
               INTO :DEPTNO,:DEPTNAME,:MGRNO
            FROM ST565.DEPT  WHERE DEPTNO=:DEPTNO
            END-EXEC.
            IF SQLCODE=0
                DISPLAY 'DEPTNO IS:'
                DISPLAY DEPTNO
                DISPLAY 'DEPTNAME IS:'
                DISPLAY DEPTNAME
                DISPLAY 'MGRNO IS:'
                DISPLAY MGRNO
            ELSE IF SQLCODE = 100
                DISPLAY 'NOT FOUND!'
            ELSE DISPLAY 'SQL ERROR'
            END-IF.
            STOP RUN.

  

三、DB2 embedded SQL working principle

 

DBRM( Database Request Modules)

packages:

plans:

 

需要注意的过程: precompile, compile, link, bind

 

 

四、Cursor

cursor operations: declare, open, fetch, close

declare:

EXEC SQL
            DECLARE cursor-name [NO SCROLL/SCROLL]
                                                  [DYNAMIC/STATIC]
            CURSOR         [WITH/ WITHOUT HOLD]
            FOR select-statement
            [FOR UPDATE OF columns-lists/ FOR FETCH ONLY]
END-EXEC.
View Code

 

open:

EXEC  SQL
            OPEN Cursor-name USING host-variables
END-EXEC.
View Code

 

fetch:

EXEC SQL
                FETCH Cursor-name
                INTO
                :Host-variable-col1,
                :Host-variable-col2,
                .
                .
                :Host-variable-coln
END-EXEC.
View Code

 

null indictor

If NULL indicator value =       -1 (NULL existed in the retrieved column)
                              =        0 (Retrieved column has the proper value)
                              =        2 (Truncated value retrieved)
View Code

 

close:

EXEC SQL
          CLOSE cursor-name
END-EXEC.
View Code

 

static cursor VS dynamic cursor

sensitive cursor VS insensitive cursor

 

 

加深理解:

1. 基本表和视图之间的关系

基本表

是独立存在的表,不是由其它的表导出的表

 

视图

是一个虚拟的表,是从一个或几个基本表导出的表;

它本身不独立存在数据库中,数据库中只存放视图的定义而不存放对应的数据;

当基本表中的数据发生变化时,从视图中查询出来的数据也随之改变。

 

2. Table space有哪些类型,它们的联系和区别,SEGSIZE的含义是什么

Simple table spaces

Segmented table spaces                

Partitioned table spaces

Universal table spaces

LOB (Large object table spaces)

Xml table spaces

 

SEGSIZE: Segmented Table spaces are divided into segments:

  • l  Each segment is dedicated to a table
  • l  Segment can consist of 4/8/16/32/64 pages
  • l  Segsize specified by creator

 

3. 静态SQL和动态SQL的特点

Static SQL 经过程序的编译后,在程序运行之前已经确定下来。

1 EXEC SQL
2 
3            SELECT NAME, DEPT INTO :NAME, :DEPT
4 
5            FROM STAFF WHERE EMPNO = :EMPID
View Code

 

Dynamic SQL在程序运行的过程中构造并执行。

EG:

1 strcpy(query, “select tabname From syscat.table”);
2 
3            strcpy(query,”where tabname <>?);
4 
5            EXEC SQL PREPARE stmt From :query
View Code

 

Static sql:

  静态SQL语句中要访问的表名和列名是必须存在的;

  开销较小;

  数据访问计划是在预编译时候生成的.

 

Dynamic SQL:

         动态SQL语句结构在预编译的时候是未知的,SQL的结构不固定,表和列可能随时变化

         SQL语句存储在某个变量中并在程序运行中被引用,更为灵活

         数据访问计划在运行中生成

 

4. DB2常用的Utilities及它们各自的功能

见上文

 

5. 什么是package、Plan, 它们的区别和联系

 

Bind Package(collection_id) Member(DBRM NAME| Package_id )

Bind Plan(plan_Name) Pklist(Package_Name1, Package_Name2..)

 

6. 敏感和不敏感游标的区别,静态游标和动态游标的区别

不敏感的游标意味着在游标打开后,不能看到其他应用对基础表的更改,不敏感的游标是只读的,结果表中行的数量和内容在游标打开的整个过程中不变,不敏感的游标不能用于数据更新和删除。

敏感的游标意味着在游标打开后,能看到其他应用对基础表的所作的更改,结果表中行的数量(其他应用插入或删除)在游标打开的整个过程中不变,但是行的内容可变。敏感的游标能由于数据更新和删除。

 

静态游标打开后,结果集的大小不变,结果集中行的数据顺序也不变。

动态游标打开后,结果集的大小可变,结果集中行的数据顺序也可变,即其他游标可以对结果集进行插入,更新或删除操作。

 

7. 理解Multiple Rows Processing程序

程序示例:

 1 IDENTIFICATION DIVISION.
 2 PROGRAM-ID.    EXAMPLE.
 3 ENVIRONMENT    DIVISION.
 4 DATA           DIVISION.
 5 WORKING-STORAGE SECTION.
 6 01 WS-DEPT.
 7     10 WS-DEPTNO  PIC X(3) OCCURS 2 TIMES.
 8     10 WS-MGRNO   PIC X(6) OCCURS 2 TIMES.
 9     EXEC SQL INCLUDE SQLCA END-EXEC.
10 PROCEDURE      DIVISION.
11 PROC-START.
12     EXEC SQL 
13         DECLARE C1 SCROOL CURSOR 
14             WITH ROSET POSITIONING FOR
15             SELECT DEPTNO,MGRNO FROM DEPT
16     END-EXEC.
17     
18     EXEC SQL OPEN C1 END-EXEC.
19 
20     EXEC SQL 
21         FETCH FIRST ROWSET FROM C1 FOR 2 ROWS
22         INTO :WS-DEPTNO, :WS-MGRNO
23     END-EXEC.
24         IF SQLCODE = 0
25             DISPLAY 'EXECUTED SUCCESSFULLY!'
26             DISPLAY 'DEPTNO1 IS:'
27             DISPLAY WS-DEPTNO(1)
28             DISPLAY 'LOCATION1 IS:'
29             DISPLAY WS-MGRNO(1)
30             DISPLAY 'DEPTNO2 IS:'
31             DISPLAY WS-DEPTNO(2)
32             DISPLAY 'LOCATION2IS:'
33             DISPLAY WS-MGRNO(2)
34         ELSE IF SQLCODE= 100
35             DISPLAY 'SQL ERROR!'
36         END-IF.
37 
38     EXEC SQL CLOSE C1 END-EXEC.
39     STOP RUN.
View Code

 

8. DB2常用SQL语句

数据定义语言(DDL), 数据操作语言(DML), 数据控制语言(DCL)

1 CREATE STOGROUP STOUDB6
2 
3            VOLUMES (DAVP7C, DAVP8C, DAVP9E)
4 
5            PASSWORD UESTC;
View Code

 

 

1 ALTER STOGROUP STOUDB6
2 
3            ADD VOLUMES (DAVP9F, DAVP8C, DAVP9E)
4 
5            REMOVE VOLUMES (DAVP7C, DAVP8C)
6 
7            PASSWORD UESTC;
View Code

 

 

 

1 CREATE TABLESPADCE DSN8S91D IN DSN8D91A
2 
3 USING STOGROUP DSN8G910
4 
5 PRIQTY 52 SECQTY 20
6 
7 LOCKSIZE PAGE
8 
9 BUFFERPOOL BP1;
View Code

 

1 CREATE UNIQUE INDEX DSN8910.XDEPT ON
2 
3 DSN8910.DEPT (DEPTNO ASC)
4 
5 PADDED USING STOGROUP DSN8G910
6 
7 PRIQTY 512 SECQTY 64
8 
9 BUFFERPOOL BP1;
View Code

 

1  
2 ALTER TABLESPACE DSN8D91A.DSN8S91D
3 
4 BUFFERPOOL BP2 LOCKSIZE PAGE;
View Code

 

1 ALTER INDEX DSN8910.XPROJ1
2 
3 BUFFERPOOL BP1 COPY YES PIECESIZE;
View Code

 

1 CREATE TABLE BRANCH(
2 
3            BRAN_ID CHAR (10) NOT NULL,
4 
5            BRAN_NAME VARCHAR (20) NOT NULL,
6 
7            BRAN_STATUS CHAR(2) NOT NULL
8 
9 ) IN BANKDB.BANKTS;
View Code

 

1 ALTER TABLE BRANCH ADD CONSTRAINT PK_BRAND1
2 
3 PRIMARY KEY(BRAN_ID);
View Code

 

1 ALTER TABLE BRANCH
2 
3            ADD CONSTRAINT CK_BRANST1
4 
5            CHECK (BRAN_STATUS=’AC’  AND BRAN_STATUS=’EP’);
View Code

 

参考:

以上内容来自SCUT IBM 课程和IBM官方学习网站,请勿随意转发。

 

posted @ 2015-11-11 16:50  artificerpi  阅读(691)  评论(0编辑  收藏  举报

Copyright ©2017 artificerpi