Documente Academic
Documente Profesional
Documente Cultură
Introduction to SQL
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
DB2 Precompile
DB2 BIND
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
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
Page 7 of 54
Introduction to SQL
Page 8 of 54
3
CREATE ALTER DROP
Introduction to SQL
Page 9 of 54
Introduction to SQL
Page 10 of 54
3
SELECT INSERT UPDATE DELETE
Introduction to SQL
Page 11 of 54
3
COMMIT ROLLBACK
Introduction to SQL
Page 12 of 54
Page 13 of 54
Select
Basics
Page 14 of 54
Example
select * from employee
Page 15 of 54
4
Example 2
Page 16 of 54
4
Addition Subtraction Multiplication Division Override rules
Arithmetic Operators
Concatenation Operators
For character strings only
Page 17 of 54
4
Examples 3 and 4
from employee ;
Page 18 of 54
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;
Page 19 of 54
Page 20 of 54
4
Conditions
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
Page 21 of 54
= <>
A=B A<> B
>
>= < <=
A>B
A >= B A<B A <= B
greater than
greater than or equal less than less than or equal
Page 22 of 54
4
Example 6
Page 23 of 54
4
Example 7
select e_no , lname , (payrate + 5) * 40 from employee where (payrate + 5) * 40 > 700;
Page 24 of 54
4
Example 8
Page 25 of 54
Page 26 of 54
Exercise 1
Page 27 of 54
Compound Conditions
Page 28 of 54
5
AND Operator
Compound Conditions
CONDITION
TRUE
FALSE NULL
TRUE
FALSE NULL
Page 29 of 54
5
Example 9
Compound Conditions
Page 30 of 54
5
OR Operator
Compound Conditions
CONDITION
TRUE
FALSE NULL
TRUE
TRUE TRUE
TRUE
TRUE
Page 31 of 54
5
Example 10
Compound Conditions
, phone
from customer where state = 'ny' or zip > '89999' or balance > 7000;
Page 32 of 54
Compound Conditions
NOT AND
Page 33 of 54
3.
OR
5
Example 11
Compound conditions
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';
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' );
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
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' ) ;
Page 38 of 54
Exercise 2
Page 39 of 54
Quiz 1
Relational Database Concepts
Page 40 of 54
Page 41 of 54
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
Page 42 of 54
Example 16
select c_no , company , state , balance from customer where state in ('md', 'ca', 'il') and balance > 4000;
Page 43 of 54
Example 17
Page 44 of 54
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
Page 45 of 54
Example 18
select c_no , company , balance from customer where balance between 772.00 and 987.00;
Page 46 of 54
Example 19
select c_no , company , balance from customer where balance NOT between 772.00 and 987.00
Page 47 of 54
LIKE Operator
Compares a CHARACTER string to a pattern string, checking whether the CHARACTER string fits the pattern
_
+
Page 48 of 54
Example 20
Page 49 of 54
Example 21
Page 50 of 54
Example 22
Page 51 of 54
Example 23
Page 52 of 54
IS NULL Operator
Provides a way to test column values for NULLITY A condition with this operator can only return TRUE or FALSE
Page 53 of 54
Example 24
Page 54 of 54