Вы находитесь на странице: 1из 51

DB2 – SQL

Basic Programming
B

© Ingram Micro Inc. -5


Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
1
1405002 rev 6.27.14

DB2 – SQL : Course Outline

 Database Overview
 DB2 Database
 SQL Programming Language
 SQL Tools: SPUFI / Platinum Interactive SQL
 COBOL DB2 Programming
 References
 Questions
 Recap
 Exercises

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
2
1405002 rev 6.27.14

Database Overview
What is Database ?
A database provides for the storing and control of business information,
independent from (but not separate from the processing requirements of)
one or more applications.

Why Use Database ?


• Reduce programming effort
• Manage data more efficiently
• Easy to separate confidential/sensitive info
• Provide a greater level of security
• Access & update simultaneously
• Ensure consistency
• Provide backup and recovery
• Utilities to monitor and tune
• Structure change does not impact existing developments

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
3
1405002 rev 6.27.14

Database Overview (cont.)


What Database we have from our operating system ?

 Hierarchical databases, such as IMS


IMS database is a hierarchical database where data is stored at different
levels and each entity is dependent on higher level entities.

 Relational database management system (RDBMS),


such as DB2
- DB2 is a database product from IBM. It is a Relational Database
Management System (RDBMS). DB2 is designed to store, analyze and
retrieve the data efficiently. DB2 product is extended with the support of
Object-Oriented features and non-relational structures with XML.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
4
1405002 rev 6.27.14

Database Overview (cont.)


Hierarchical Database Structure

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
5
1405002 rev 6.27.14

Database Overview (cont.)


Relational DB2 Database Structure consist of the
following ;
• Table
All data in DB2 Database is presented in tables. It has a logical structure
composed of rows (records) and columns. All tables that holds
persistent/permanent data is called Base Table . Table that holds temporary
data is a “Temporary Table”

• Index(es)
an ordered set of pointers to rows of a table (ensures uniqueness) and it is
stored separately from the table.

• Keys
One or more columns that are identified as such in the creation of a table
or used for referential integrity

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
6
1405002 rev 6.27.14

Database Overview (cont.)


Relational DB2 Database Structure…
• Keys
- Primary Key - a column or group of columns that uniquely identify
a row. Every table should have a primary key. And a table cannot
have more than one primary key.
- Foreign Key - is a column or set of columns in one table whose
values must have matching values in the primary key of another
(or the same) table. A foreign key is said to reference its primary key

• View
Is an alternative way of representing data that exists in one or more tables.
A view can include all or some of the columns from one or more base
tables.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
7
1405002 rev 6.27.14

DB2 - Database
DB2 Data Types
At the time DB2 table is created, each “column” correspond to a Data type.
String Data Type - support the character, graphic and binary string

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
8
1405002 rev 6.27.14

DB2 - Database
DB2 Data Types…
Numeric Data Type - use numeric columns rather than the string columns –
requires less space than string.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
9
1405002 rev 6.27.14

SQL Programming Language

What is SQL?

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute)
standard

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
10
1405002 rev 6.27.14

SQL Programming Language (cont.)

What Can SQL do?

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a databaseSQL can set
permissions on tables, procedures, and views

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
11
1405002 rev 6.27.14

SQL Programming Language (cont.)

RDBMS

• RDBMS stands for Relational Database Management


System.
• RDBMS is the basis for SQL, and for all modern database
systems such as MS SQL Server, IBM DB2, Oracle,
MySQL, and Microsoft Access.
• The data in RDBMS is stored in database objects called
tables.
• A table is a collection of related data entries and it consists
of columns and rows.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
12
1405002 rev 6.27.14

SQL Programming Language (cont.)

Database Tables
A database most often contains one or more tables. Each
table is identified by a name. Tables contain records (rows)
with data.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
13
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL Statements

Most of the actions you need to perform on a database are


done with SQL statements.

Example:
SELECT FIRST_NAME FROM PERSONS;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
14
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL Commands

• SELECT - extracts data from a database


• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
15
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL SELECT Statement

The SELECT statement is used to select data from a


database.
The result is stored in a result table, called the result-set.

Syntax:
SELECT * FROM table_name;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
16
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL UPDATE Statement

The UPDATE statement is used to update existing records in


a table.

Syntax:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
17
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records


in a table.

Syntax:
INSERT INTO table_name
VALUES (value1,value2,value3,...);

INSERT INTO table_name (column1,column2,column3,...)


VALUES (value1,value2,value3,...);

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
18
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL DELETE Statement

The DELETE statement is used to delete records in a table.

Syntax:
DELETE FROM table_name
WHERE some_column=some_value;

Important: The WHERE clause specifies which record or


records that should be deleted. If you omit the WHERE
clause, all records will be deleted!

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
19
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL WHERE Clause

The WHERE clause is used to filter records.

Syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name = operator value;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
20
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL AND & OR Operators

• The AND operator displays a record if both the first


condition AND the second condition are true.
• The OR operator displays a record if either the first
condition OR the second condition is true.

Syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name1 = operator value
AND column name2 = operator value;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
21
1405002 rev 6.27.14

SQL Programming Language (cont.)

SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by


one or more columns.

Syntax:
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
22
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

SPUFI and Platinum are DB2 Tools available in TN3270


Rumba. Both can be used to run Queries against the database
and view several other key information about the database.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
23
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

Navigation

• Log into TSO


• Select USER MENU
• Select DB2 for MVS/ESA
• Select CA Platinum Tools for DB2 (V17) for Platinum
• Select Perform DB2 Interactive Functions for SPUFI

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
24
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

Platinum
To run queries here, select Interactive SQL (I).

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
25
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

Platinum
To enter a query, Select Edit SQL (E).

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
26
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

Platinum
Type in the query you need to be executed.
Then go back (PF3)

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
27
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum


Platinum
To execute the query, select SQL Execution (S).
A new screen will display the results.

Note: For insert/update/delete, make sure the Commit is selected if you want to keep the changes.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
28
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

SPUFI

To use this tool select SPUFI (1).

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
29
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

SPUFI
• Enter a Data Set name that will serve as the Input data.
• Enter another Data Set name that will serve as the Output data.
• Press ENTER.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
30
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum

SPUFI

• Type in the query that you need to be executed.


• Go back to the previous screen (PF3).

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
31
1405002 rev 6.27.14

SQL Tools – SPUFI / Platinum


SPUFI
• Screen will go back to the previous one, but will show a note that the
input has been modified.
• Press ENTER, a new screen will display the results.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
32
1405002 rev 6.27.14

COBOL Database Interface


As of now, we have learned the use of files in COBOL.
Now, we will discuss how a COBOL program interacts
with DB2. It involves the following terms:

• Table Declaration
• Host Variables
• DCLGEN
• SQLCA
• Embedded SQL
• Cursors
• Common SQL Codes

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
33
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Table Declaration

Your COBOL program should declare the tables and views that it accesses.
This needs to be declared in the WORKING-STORAGE section or LINKAGE
section.

Example:
EXEC SQL
DECLARE DSNXXX.EMPLOYEE
( EMPID CHAR(10) NOT NULL,
EMPNAME CHAR(30) NOT NULL,
DEPARTMENT CHAR(2) NOT NULL,
SALARY DECIMAL(10,2) NOT NULL,
DESIGNATION CHAR(4) NOT NULL )
END-EXEC.
Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
34
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Host Variables

• are used for receiving data from a table or inserting data in a table. Host
variables must be declared for all values that are to be passed between the
program and the DB2. They are declared in the Working-Storage Section.
• cannot be group items, but they may be grouped together in host structure.
They cannot be Renamed or Redefined. Using host variables with SQL
statements, prefix them with a colon (:)

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
35
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Host Variables Declaration

Following is the syntax to declare host variables and include tables in Working-
Storage section:

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
36
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

DCLGEN (DECLARATIONS GENERATOR)

Your COBOL program should declare the tables and views that it accesses.
However, you can use DCLGEN to produce these declarations, so that you do
not need to code the statements yourself. DCLGEN also generates
corresponding host variable structures.

Syntax:
EXEC SQL
INCLUDE < copybookname >
END-EXEC.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
37
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

SQLCA

• SQLCA is a SQL communication area through which DB2 passes the feedback
of SQL execution to the program. It tells the program whether an execution
was successful or not. There are a number of predefined variables under
SQLCA like SQLCODE which contains the error code. The value '000' in
SQLCODE states a successful execution.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
38
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

SQLCA Syntax

Following is the syntax to declare an SQLCA in the Working-Storage


section:

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
39
1405002 rev 6.27.14

COBOL Database Interface (cont.)

Embedded SQL statements

Statements used in COBOL programs to perform standard SQL operations.


Embedded SQL statements are preprocessed by SQL processor before the
application program is compiled. COBOL is known as the Host Language.
COBOL-DB2 applications are those applications that include both COBOL and
DB2.

Embedded SQL statements work like normal SQL statements with some minor
changes. For example, that output of a query is directed to a predefined set of
variables which are referred as Host Variables. An additional INTO clause is
placed in the SELECT statement.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
40
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Embedded SQL statements

Following are rules to be followed while coding a COBOL-DB2 program:

• All the SQL statements must be delimited between EXEC SQL and END-
EXEC.
• SQL statements must be coded in Area B.
• All the tables that are used in a program must be declared in the Working-
Storage Section. This is done by using the INCLUDE statement.
• All SQL statements other than INCLUDE and DECLARE TABLE must appear
in the Procedure Division

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
41
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Cursors

Cursors are used to handle multiple row selections at a time. They are
data structures that hold all the results of a query. They can be defined in
the Working-Storage Section or the Procedure Division.

Following are the operations associated with Cursor:


• Declare
• Open
• Close
• Fetch

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
42
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Cursors - DECLARE

Cursor declaration can be done in the Working-Storage Section or the


Procedure Division. The first statement is the DECLARE statement
which is a non-executable statement.

Syntax:
EXEC SQL
DECLARE STUDCUR CURSOR FOR
SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS
FROM STUDENT
WHERE STUDENT-ID >:WS-STUDENT-ID
END-EXEC.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
43
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Cursors - OPEN

Before using a cursor, Open statement must be performed. The Open


statement prepares the SELECT for execution.

Syntax:
EXEC SQL
OPEN STUDCUR
END-EXEC.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
44
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Cursors - FETCH

Fetch statement identifies the cursor and puts the value in the INTO
clause. A Fetch statement is coded in loop as we get one row at a time.

Syntax:
EXEC SQL
FETCH STUDCUR
INTO :WS-STUDENT-ID
, :WS-STUDENT-NAME
, WS-STUDENT-ADDRESS
END-EXEC.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
45
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Cursors - CLOSE

Close statement releases all the memory occupied by the cursor. It is


mandatory to close a cursor before ending a program.

Syntax:
EXEC SQL
CLOSE STUDCUR
END-EXEC.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
46
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

SQL Codes

When DB2® executes an SQL statement, it returns information about the


statement execution. This information includes the SQL return code
(SQLCODE).

SQL Codes are very integral in understanding the root cause of a


ABEND or an unexpected flow of a process.

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
47
1405002 rev 6.27.14

COBOL - Database Interface (cont.)

Common SQL Codes

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
48
1405002 rev 6.27.14

COBOL - Questions ??

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
49
1405002 rev 6.27.14

COBOL - References

 Link for SQL Tutorial 


http://www.w3schools.com/sql/

 Useful Link as reference for COBOL Verb, Statements and


corresponding Syntax 
http://web.cse.ohio-state.edu/~sgomori/314/langref.html

 Link for Basic Cobol Tutorial 


http://www.tutorialspoint.com/cobol/

 List of SQLCODEs 
http://www.caliberdt.com/tips/sqlcode.htm

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
50
1405002 rev 6.27.14

COBOL - Exercises

Two parts of Exercises will be given to the Trainee

1. Exercises Question and Answer = 20%


2. Exercises supplied by Technical Specification = 80%

Proprietary information of Ingram Micro Inc. — Do not distribute or duplicate without Ingram Micro's express written permission.
51

Вам также может понравиться