IBM Systems Lab Services and Training
A Data Centric Approach
Development - Using SQ
Lifting
Quser January 15, 2013
Dan Cruikshank
[email protected]
[email protected]
ibm.com/systems/services/labservices
h to Application
QL to do the Heavy
3
© 2011 IBM Corporation
IBM Systems Lab Services and Training
Agenda
Quser Back in the Day
Data Centric Development
Transparent Migration from DDS to D
2 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
DDL © 2011 IBM Corporation
ices
IBM Systems Lab Services and Training
Quser Back in the Day
I served as an officer of Quser for 4 y
–I was preceded by Dick Jacobson
Notable Speakers
–Ken Kelley President and founder
• Makers of Abstract/Probe and S
• Topic: Object Oriented Program
–Paul Conte Author and DB2 for i D
• First professional speaking eng
• Topic: Relational Database De
Implicit/Explicit Record Format sharin
–Foundation for transparent DDS to
–Concepts were introduced at Quse
errors without recompiling
3 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
years from 1983-86
founder of Help/Systems
of Advanced System Concepts
SEQUEL (now part of Help/Systems)
mming
Database Guru
gagement
esign
ng
o DDL migration
er as technique to avoid level check
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Data Centric Development
Data Centric Programming and Set B
Defining Sets using SQL DDL
Accessing Sets using SQL DML
4 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
Based Processing
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
What is Data Centric Programming
Definition:
–Solving business problems using
–Getting the database managemen
behalf
–Implementing more of the busines
–Separating the business logic from
programs
5 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
g?
database capabilities
nt system to do more on your
ss logic in the database
m the high level application
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Application Centric Overview
Resu
HLL PGM
DDS
T1 T2
6 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ults
M
T3 T4
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Application Centric Example
More application logic
Small pipe (1 row at a time)
Not using much intelligence in the
High Level Language R
Program
Establish begin and
end dates
READ File
Convert numeric date
to real date
If real date is => begin date
And real date <= end date
Then
Do some work
End If
7 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
database
READ Data DDS Database
Numeric_Date
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Data Centric Overview
HLL PGM
Resu
DB2
T1 T2
8 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
M or Interface
ults
T3 T4
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Data Centric Programming Exampl
Less application logic
– Less application developme
– You can run and test SQL w
Big pipe (sets of many rows proce
Taking advantage of more intellige
HLL Program FETCH Data
Do some work
9 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
le
ent and testing
without any programming
essed at a time)
ence in the database
DB2 Database
SELECT *
FROM View
WHERE real_date
BETWEEN Begin_date
AND End_date
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Data Centric Programming Scalabi
Goal is to drive as much work down into
the database management system as
possible.
Define business rules as part of the
database
– Rules apply to all application
interfaces
Take advantage of SQL only
capabilities
Database evolves to:
– Meet new requirements
– Take advantage of new technology
10 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ility
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
SQL and Data Centric Programmin
SQL works very well
Data Centric Program
SQL
sets
11 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ng
for
mming
works best for processing
of data
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Set Based Thinking and SQL
SQL is declarative and not procedura
–Think in terms of defining the sets
DDL (Data Definition Language)
–Defines the sets
DML (Data Manipulation Language)
–Operates upon those sets
12 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
al
s and the operations on those sets
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Enabling Sets
Data Centric DBMS kno
ve
Program kno
13 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ows about data
ersus
ows about data Traditional
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
SQL and Data Centric Programmin
Proper data modeling re
in good “set” definitio
Proper
good
14 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ng
esults
ons
r SQL usage results in
set based operations
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Agenda
Data Centric Programming and Set
Accessing Sets using SQL DML
15 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
Based Processing
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Data Centric Constructs Used Whe
Constraints
–The basis for a solid foundation
Indexes
–The plumbing and wiring
SQL Views
–The walls, roof, doors and window
DB2 for i Auto Generated Column Su
–The “must have” appliances
16 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
en Defining the Database
ws © 2011 IBM Corporation
upport
ices
IBM Systems Lab Services and Training
What are the Data Integrity Rules?
Customers Ord
CustomerNo OrderNo
Name LineNo
Address Custom
State ItemNo
City Quantity
Zip Price
What happens if we are updating Orders but hav
What happens if we drop a customer row from C
What happens if we add an element to Items wit
17 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ders Items
o ItemNo
Description
merNo Color
Size
y
ve specified an invalid CustomerNo value?
Customers?
th an invalid size?
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Constraints
Unique key constraint
– the values of the key are valid only if they
– More than 1 per table
Primary key constraint
– the values of the key are valid only if they
– Only 1 per table
Referential constraint
– the values of the “foreign key” are valid on
• foreign key appear as values of a pare
• the foreign key is null
Check constraint
– the rule that limits the values allowed in a
18 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
are unique
are unique and not null-able
nly if one of the following conditions is met:
ent key
column or group of columns
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Enforcing the Rules
Customers Ord
CustomerNo OrderNo
Name LineNo
Address Custom
State Item_ID
City Quantity
Zip Price
Customer_ID
Constraints
All UNIQUE
applications PRIMARY KEY
must follow FOREIGN KEY
the rules
CHECK
19 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ders Items
o ItemNo
Description
mer_ID Color
D Size
y Item_ID
Rules
Identifies unique public key
Y Identifies parent key
Y Matches parent key. Defines
actions for insert, update and
delete events
Ensures proper values, e.g.
Quantity > 0
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Indexes
Indexes are needed for optimal perfo
–1 Index can service several SQL r
Indexes are not referenced directly in
UPDATE, etc.
–That’s the job of the query optimiz
use indexes
Indexes can contain:
–Expressions: CREATE INDEX X1
–Functions: CREATE INDEX X2 ON
–Aggregates: CREATE EVI E1 ON
–Non-keyed columns: CREATE IND
COLUMNS ID, NAME
–Row filtering: CREATE INDEX X4
‘D11’
20 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ormance
requests
n SQL statements like SELECT,
zer, it decides if and when and how to
ON T1 (QTY * PRICE)
N T1 (UPPER(NAME))
T1 (NAME) INCLUDE(SUM(QTY))
DEX X3 ON T1 (DEPT) ADD
4 ON T1 (NAME) WHERE DEPT =
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
INDEX Usage Examples
SELECT …. FROM T1 ORDER BY DEPT;
SELECT …. FROM T1 WHERE DEPT =
‘D11’ ORDER BY NAME;
SELECT …. FROM T1 WHERE DEPT =
‘D11’ AND SUBSTR(NAME,1,3) = ‘ABC’
ORDER BY NAME;
SELECT DEPT, SUM(QTY) FROM T1
GROUP BY DEPT
ORDER BY DEPT;
21 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
One index servicing two queries
CREATE INDEX X3 ON T1 (DEPT, NAME);
Multiple Indexes servicing 1 query
CREATE INDEX X3 ON T1 (DEPT, NAME);
CREATE INDEX X7 ON T1 (SUBSTR(NAME,1,3));
An aggregate Index supporting SUM
CREATE INDEX E7 ON T1 (DEPT)
INCLUDE SUM(QTY);
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
SQL Views
SQL views are used to show some su
isolate some complex operation
SQL views do not contain data or acc
Views have no implied order
Views are referenced just like tables
Views can be joined to other views
Views can be used to externally desc
22 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
ubset of the data or to capture and
cess paths
in SQL statements
cribe data structures © 2011 IBM Corporation
ices
IBM Systems Lab Services and Training
VIEW Usage Examples
Complex Query
SELECT … FROM T1, T2, T3,T4…
GROUP BY… ORDER BY…
Program Described Structure
T1_RESULT DS
A CHAR(10)
B DECIMAL(11,0)
C DATE
SELECT A, B, C INTO :T1_RESULT FROM T1
Multiple Views and Data Structures
T1_RESULT DS EXTNAME T1_VIEW
T2_RESULT DS EXTNAME T2_VIEW
SELECT * INTO :T1_RESULT FROM T1_VIEW
SELECT * INTO :T2_RESULT FROM T2_VIEW
WHERE T2.A = :T1_RESULT.A
23 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
Masking Complexity
CREATE VIEW COMPLEX_QUERY
AS SELECT … FROM T1, T2, T3,T4… GROUP BY…;
SELECT * FROM COMPLEX_QUERY ORDER BY…;
Externally Described Structure
CREATE VIEW T1_VIEW
AS SELECT A, B, C FROM T1;
T1_RESULT DS EXTNAME(T1_VIEW)
SELECT * INTO :T1_RESULT FROM T1_VIEW
Joined View One Structure
CREATE VIEW JOIN_VIEW AS
SELECT A, B, C, D, E, F FROM T1 JOIN T2 USING(A);
JOIN_RESULT DS EXTNAME(JOIN_VIEW)
SELECT * FROM JOIN_VIEW INTO :JOIN_RESULT
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Auto-generated Values
DB2 for i can automatically generate
A value can be auto-generated in 1 o
–Defined as a column attribute
–Defined as a column type (ROWID
–Extracted from a external object
The following SQL column attributes
–Row Change Timestamp
– Identity
A Sequence object contains a system
–Is external from a table
–Can be used on INSERT statemen
24 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
the value for a column
of following ways:
D)
allow auto-generation:
m generated value
nts to assign the value to a column
ices © 2011 IBM Corporation
IBM Systems Lab Services and Training
Auto-Generated Column Exampl
Row Change Timestamp
CREATE TABLE ORDERS
(ORDER_KEY BIGINT …,
ORDER_CHANGE_AUDIT TIMESTAMP
NOT NULL
FOR EACH ROW ON UPDATE AS ROW
CHANGE TIMESTAMP)
IMPLICITLY HIDDEN;
Identity Column
CREATE TABLE ORDERS
(ORDER_ID BIGINT NOT NULL
GENERATED BY DEFAULT AS IDENTITY
PRIMARY KEY IMPLICITLY HIDDEN,
25 IBM Systems Lab Services and Training – ibm.com/systems/services/labservi
les
Notes
IMPLICITLY HIDDEN prevents column from
being included in SQL * statements
ROW CHANGE TIMESTAMP function can be
used without column name knowledge
SELECT …. ROW CHANGE TIMESTAMP FOR
T1 AS RCTS FROM T1
UPDATE T1 ….. WHERE ORDER_KEY =
:v_ORDER_KEY AND ROW CHANGE
TIMESTAMP FOR T1 = :v_RCTS
ID Column Notes
Only 1 IDENTITY column per table
Ideal as meaningless PRIMARY KEY
GENERATED ALWAYS results in the
generation of a new value automatically.
Existing values are ignored
Can be an issue when copying or duplicating data
GENERATED BY DEFAULT only generates a
value if the auto-generated column is not
included in the data
ices © 2011 IBM Corporation