Sunteți pe pagina 1din 54

DB2 TRANSPARENCIES (Unit 3)

DB2_Tr Ver. 1.0.0 04/12/1998 Page 1 of 54

Introduction to SQL

Objectives - Student will


Be aware of what environments SQL may be executed in Know the steps of the bind process Know a definition of SQL Know the components of SQL

DB2_Tr Ver. 1.0.0 04/12/1998

Page 2 of 54

Introduction to SQL

SQL Environments
TSO Batch - Static SQL On-Line - Dynamic SQL - SPUFI or QMF IMS Batch On-Line CICS On-Line
DB2_Tr Ver. 1.0.0 04/12/1998 Page 3 of 54

Introduction to SQL

Static SQL
Either Batch or On-Line Must be embedded in a host language program COBOL PLI FORTRAN ASSEMBLER Must go through BIND process in advance to executing the PLAN
DB2_Tr Ver. 1.0.0 04/12/1998 Page 4 of 54

Introduction to SQL

Source Data Set Host Language Source

DB2 Precompile

DCLGEN Include Library DBRM SQL Code DB2 Catalog

Compile and Link

DB2 BIND

DB2_Tr Ver. 1.0.0 04/12/1998

MVS Load Module

Program Execution
Page 5 of 54

DB2 PLAN

Introduction to SQL
Dynamic SQL BIND
Verify Syntax Check DB2 Authorization Determine Access Strategy Obtain Necessary Storage Execute

DB2_Tr Ver. 1.0.0 04/12/1998

Page 6 of 54

Introduction to SQL

Structured Query Language Standard Data Access Language of the Relational World Set-at-a-Time Language Handles Three-Value logic
True False Unknown

DB2_Tr Ver. 1.0.0 04/12/1998

Page 7 of 54

Introduction to SQL

DCL - Data Control Language


GRANT Used to provide access to DB2 objects REVOKE Used to remove access to DB2 objects

DB2_Tr Ver. 1.0.0 04/12/1998

Page 8 of 54

3
CREATE ALTER DROP

Introduction to SQL

DDL - Data Definition Language

Used to Manipulate DB2 Objects

DB2_Tr Ver. 1.0.0 04/12/1998

Page 9 of 54

Introduction to SQL

DDL - Data Definition Language cont

DB2_Tr Ver. 1.0.0 04/12/1998

Page 10 of 54

3
SELECT INSERT UPDATE DELETE

Introduction to SQL

DML - Data Manipulation Language

Used to Manipulate DB2 Data

DB2_Tr Ver. 1.0.0 04/12/1998

Page 11 of 54

3
COMMIT ROLLBACK

Introduction to SQL

Transaction Control Statements

DB2_Tr Ver. 1.0.0 04/12/1998

Page 12 of 54

The Basic Select

Objectives - Student will


Understand components of SELECT statement using SELECT, FROM and WHERE Be able to code and execute simple SQL statements in SPUFI Be able to make use of conditions and comparison operators

DB2_Tr Ver. 1.0.0 04/12/1998

Page 13 of 54

The Basic Select

Select

Basics

DB2_Tr Ver. 1.0.0 04/12/1998

Page 14 of 54

The Basic Select

Example
select * from employee

DB2_Tr Ver. 1.0.0 04/12/1998

Page 15 of 54

4
Example 2

The Basic Select

select 'SALARY and COMMISSION: ' , payrate , com from employee ;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 16 of 54

4
Addition Subtraction Multiplication Division Override rules

The Basic Select


+ * / () ||

Arithmetic Operators

Concatenation Operators
For character strings only

DB2_Tr Ver. 1.0.0 04/12/1998

Page 17 of 54

4
Examples 3 and 4

The Basic Select

select fname || lname , payrate * 40

select fname || ' ' || lname , payrate * 40 from employee ;

from employee ;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 18 of 54

The Basic Select

Example 5
select fname || ' ' || lname , payrate + 5.00 * 40 + payrate + 5.00 * 40 * com , (payrate + 5.00) * 40 + ((payrate + 5.00) * 40 * com) , (payrate + 5.00 * 40) + (payrate + 5.00 * 40 * com) from employee;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 19 of 54

The Basic Select

The WHERE Clause - Predicate


Determines which rows are returned Is optional One parameter which is a condition

DB2_Tr Ver. 1.0.0 04/12/1998

Page 20 of 54

4
Conditions

The Basic Select

Evaluate to: TRUE - The predicate clause succeeded FALSE - The predicate clause failed UNKNOWN - The effect of nullity May contain expressions or subqueries, each separated by operators Used in WHERE clause and HAVING clause

DB2_Tr Ver. 1.0.0 04/12/1998

Page 21 of 54

The Basic Select

= <>

A=B A<> B

equal not equal

>
>= < <=

A>B
A >= B A<B A <= B

greater than
greater than or equal less than less than or equal

Condition is UNKNOWN if either A or B is NULL

DB2_Tr Ver. 1.0.0 04/12/1998

Page 22 of 54

4
Example 6

The Basic Select

select company , state , balance from customer where state = 'ca';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 23 of 54

4
Example 7

The Basic Select

select e_no , lname , (payrate + 5) * 40 from employee where (payrate + 5) * 40 > 700;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 24 of 54

4
Example 8

The Basic Select

select e_no , lname , city , st from employee

where st <> 'md';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 25 of 54

The Basic Select

SQL Interactive Environments


SPUFI - Mainframe SQL Wizard - XDB - Micro Focus

DB2_Tr Ver. 1.0.0 04/12/1998

Page 26 of 54

Exercise 1

DB2_Tr Ver. 1.0.0 04/12/1998

Page 27 of 54

Compound Conditions

Objectives - Student will


Know the order of processing for AND, OR and NOT Understand how to use parentheses to change the order of processing

DB2_Tr Ver. 1.0.0 04/12/1998

Page 28 of 54

5
AND Operator

Compound Conditions

Combines the values of two conditions, resulting in a single conditional value

CONDITION AND TRUE FALSE NULL

CONDITION

TRUE
FALSE NULL

TRUE

FALSE NULL

FALSE FALSE FALSE NULL FALSE NULL

DB2_Tr Ver. 1.0.0 04/12/1998

Page 29 of 54

5
Example 9

Compound Conditions

select company , state , balance from customer where state = 'ca'

and balance > 0;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 30 of 54

5
OR Operator

Compound Conditions

Combines the values of two conditions, resulting in a single conditional value

CONDITION OR TRUE FALSE NULL

CONDITION

TRUE
FALSE NULL

TRUE
TRUE TRUE

TRUE

TRUE

FALSE NULL NULL NULL

DB2_Tr Ver. 1.0.0 04/12/1998

Page 31 of 54

5
Example 10

Compound Conditions

select c_no , company

, phone
from customer where state = 'ny' or zip > '89999' or balance > 7000;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 32 of 54

Compound Conditions

Parenthetical Conditions NOT Operator


Normal order of evaluation can be overridden by placing those conditions needing to be evaluated first within parentheses.

Normal Order of Evaluation 1. 2.


DB2_Tr Ver. 1.0.0 04/12/1998

NOT AND
Page 33 of 54

3.

OR

5
Example 11

Compound conditions

select lname , dept , payrate , com from employee

where payrate > 12.00


or dept = '1050' and st = 'va';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 34 of 54

5
Example 12

Compound Conditions

select lname , dept , payrate , com from employee where (payrate > 12.00 or dept = '1050' ) and st = 'va';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 35 of 54

Compound Conditions

Example 13
select lname , dept

, payrate
, com from employee where payrate > 12.00 or (dept = '1050'

and st = 'va' );

DB2_Tr Ver. 1.0.0 04/12/1998

Page 36 of 54

5
NOT Operator

Compound Conditions

Will reverse the value of a condition The resulting value is the opposite of the value of the original condition, exclusive of NULLS

DB2_Tr Ver. 1.0.0 04/12/1998

Page 37 of 54

5
Example 14/15

Compound Conditions

select lname , dept , payrate , com from employee where NOT or ( payrate > 12.00 (dept = '1050'

select lname , dept , payrate , com from employee where payrate < 12.00 and (dept<> '1050'

and st = 'va' ) );

or st <> 'va' ) ;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 38 of 54

Exercise 2

DB2_Tr Ver. 1.0.0 04/12/1998

Page 39 of 54

Quiz 1
Relational Database Concepts

DB2_Tr Ver. 1.0.0 04/12/1998

Page 40 of 54

Special Comparison Operators

Objectives - Student will


Know what the special comparison operators are and when to use each one Know how to use IS NULL

DB2_Tr Ver. 1.0.0 04/12/1998

Page 41 of 54

Special Comparison Operators

IN Operator
Tests for inclusion of a value within a set of values Evaluates to TRUE when equality is found NOT IN will test the entire list of values

DB2_Tr Ver. 1.0.0 04/12/1998

Page 42 of 54

Special Comparison Operators

Example 16

select c_no , company , state , balance from customer where state in ('md', 'ca', 'il') and balance > 4000;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 43 of 54

Special Comparison Operators

Example 17

select c_no , company , state , balance from customer

where state NOT in ('md', 'ca', 'il')


and balance > 2000;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 44 of 54

Special Comparison Operators

BETWEEN Operator
Tests whether the value of one expression is in the range of values represented by two other expressions Range is inclusive; that is it includes the extreme values as well

DB2_Tr Ver. 1.0.0 04/12/1998

Page 45 of 54

Special Comparison Operators

Example 18

select c_no , company , balance from customer where balance between 772.00 and 987.00;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 46 of 54

Special Comparison Operators

Example 19

select c_no , company , balance from customer where balance NOT between 772.00 and 987.00

and state in ('md', 'nj');

DB2_Tr Ver. 1.0.0 04/12/1998

Page 47 of 54

Special Comparison Operators

LIKE Operator
Compares a CHARACTER string to a pattern string, checking whether the CHARACTER string fits the pattern

Represents any number (including 0) of unspecified characters

_
+

(Underscore) Represents a single unspecified character


Apostrophe permits use of an in a string Escape character to permit use of % in a string

DB2_Tr Ver. 1.0.0 04/12/1998

Page 48 of 54

Special Comparison Operators

Example 20

select * from products where description like 'vga%';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 49 of 54

Special Comparison Operators

Example 21

select company from customer where company like '%systems';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 50 of 54

Special Comparison Operators

Example 22

select * from staff where lname like 'M_SSON';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 51 of 54

Special Comparison Operators

Example 23

select * from staff where lname like '%''CONNOR';

DB2_Tr Ver. 1.0.0 04/12/1998

Page 52 of 54

Special Comparison Operators

IS NULL Operator
Provides a way to test column values for NULLITY A condition with this operator can only return TRUE or FALSE

DB2_Tr Ver. 1.0.0 04/12/1998

Page 53 of 54

Special Comparison Operators

Example 24

select empid , lname , salary , commission from staff

where commission is null


and salary > 20000;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 54 of 54

S-ar putea să vă placă și