SlideShare a Scribd company logo
DB2 for OS/390 Fundamentals
Introduction to DB2 The Sample Database Advanced SQL Embedded SQL Performance Topics Tools of the trade DB2 for OS/390 Fundamentals
Introduction to DB2 Universal Database
Introduction to DB2 History Workings Address spaces Threads Attachment Facilities SQL DB2 Authorities DB2 Objects Referential Integrity Commands and Utilities
A brief DB2 History 1983 DB2 v1 ships 1993 DB2 for OS/2 and Aix 1994 DB2 for MVS v4 introduces Data Sharing 1996 DB2 UDB - multimedia, web-ready, cross-platform (non-mainframe) 1999 DB2 UDB for OS/390 (v6.1) 2001 DB2 UDB for OS/390 and z/OS (v7.1) 2004 DB2 UDB for z/OS (v8.1) 2004 DB2 UDB for LUW (v8.2)
The Guts of DB2 Directory Catalog Tables Logs BSDS DB2 User Thread EDM Pool Sort Pool Buffer Pools RID Pool Temporary Database Working Storage Program Logic
The Guts of DB2 Address Space Name Functions DBAS Database Services Address Space SSAS System Services Address Space IRLM Intersystem Resource Lock Manager DDF Distributed Data Facility SPAS Stored Procedure Address Space(s)
Threads DB2 CICS Programs DB2 Utilities TSO Online Programs TSO Batch Programs QMF/DB2I (SPUFI) thread threads thread thread thread Distributed Programs thread
Attachment Facilities DB2 TSO Address Space DSN Processor ISPF DB2I SPUFI QMF Rexx
Data Sharing Shared data between subsystems Increased availability to data Expanded capacity Increased flexibility Easier upgrade path Parallelism exploited
Parallelism Partitioned Tablespaces DEGREE(ANY) BIND parameter SET CURRENT DEGREE = ‘ANY’; SQL statement Types of Parallelism I/O multiple read engines CP multiple processor and read engines Sysplex multiple data sharing subsystems
SQL SQL DCL DDL DML GRANT REVOKE CREATE ALTER DROP SELECT UPDATE INSERT DELETE
Results Sets DB2 Request sent to database Multiple rows returned as a results set
Authorities SYSADM Systems Administrator Can do anything to everything DBADM Database Administrator Can do anything to a database User-based Select, Update, Insert, Delete access on permissions tables; Execute, Bind access on packages; utilities/operations upon objects. SYSMAINT/SYSCTRL Other administrative DBMAINT/DBCTRL functions in DB2 }{
DB2 Objects STOGROUP DATABASE TABLESPACE TABLE VIEW INDEX ALIAS SYNONYM COLUMN
DB2’s VSAM data sets M enu  O ptions  V iew  U tilities  C ompilers  H elp  ----------------------------------------------------------------------------- DSLIST - Data Sets Matching FIGDB2.DSNDBC.DFIGE0H.*ABORT*  Row 1 of 6 Command ===>  Scroll ===>  CSR  Command - Enter "/" to select action  Tracks %Used XT  Device ----------------------------------------------------------------------------- FIGDB2.DSNDBC.DFIGE0H.SABORT.I0001.A001  FIGDB2.DSNDBD.DFIGE0H.SABORT.I0001.A001  1950  ?  31  3390 FIGDB2.DSNDBC.DFIGE0H.XABORT0.I0001.A001  FIGDB2.DSNDBD.DFIGE0H.XABORT0.I0001.A001  120  ?  6  3390 FIGDB2.DSNDBC.DFIGE0H.IABORT1.I0001.A001  FIGDB2.DSNDBD.DFIGE0H.IABORT1.I0001.A001  16  ?  15  3390 *************************** End of Data Set list **************************** Partition number  Tablespace/Index name Database name High-level qualifier
Tablespaces Simple only  when you want to mix data from different tables on one page. Segmented handles space more efficiently separates data from multiple tables in the same tablespace Partitioned utilise parallelism very large amounts of data isolate data in dedicated page sets LOB (Large Object) to store LOB data only
Tables Rows Columns
Columns Requires a name Cannot have duplicate names within a table Requires a data type Other attributes NULLS ALLOWED/NOT NULL DEFAULT VALUE PRIMARY KEY CHECK CONSTRAINT
Data types SMALLINT/INTEGER CHARACTER/VARCHAR GRAPHIC/VARGRAPHIC/DBCLOB CHARACTER LARGE OBJECT/CLOB BINARY LARGE OBJECT/BLOB DECIMAL/NUMERIC FLOAT/REAL/DOUBLE DATE TIME TIMESTAMP ROWID
User-Defined Data types The user-defined DISTINCT TYPE is based on one of the previous basic data types. E.g. CREATE DISTINCT TYPE AUDIO AS BLOB (1M); CREATE DISTINCT TYPE VIDEO AS BLOB (1M); Then, your CREATE TABLE statement might look like this: CREATE TABLE VIDEO_CATALOG (VIDEO_NUMBER CHAR(6) NOT NULL, VIDEO_SOUND  AUDIO, VIDEO_PICS  VIDEO, ROW_ID  ROWID NOT NULL GENERATED ALWAYS);
Functions Column Functions Scalar Functions User-defined Functions
Routines User-defined Functions Sourced External Stored Procedures See next foil...
Stored Procedures Calling Program MVS Schedule SP SELECT INSERT UPDATE DELETE DB2 Start Proc SELECT INSERT UPDATE DELETE End Proc SPAS Call Results
Other features Relational Integrity Joining tables relationally Triggers firing actions to other tables (or calling stored procedures which can call other programs) when tables are modified Constraints build data rules into the database Labels
Check Constraints Data value restrictions on the contents of a column. EXTN CHAR(4) CONSTRAINT NUMBER CHECK (EXTN BETWEEN ‘0000’ AND ‘9999’) CONSTRAINT BYPASS CHECK(BYPASS IN ('Y', 'N')  AND (BYPASS = 'N' OR  (BYPASS = 'Y’ AND   REASON IS NOT NULL)))
Triggers CREATE TRIGGER UPD_SALARY BEFORE UPDATE OF SALARY ON EMP  FOR EACH ROW MODE DB2SQL WHEN (NEW.SALARY>(OLD.SALARY*1.5)) BEGIN ATOMIC SIGNAL SQLSTATE ‘75001’ (‘Raise exceeds 50%’); END; CREATE TRIGGER RISK.GRT_DEL AFTER DELETE ON RISK.GENERIC  FOR EACH ROW MODE DB2SQL BEGIN ATOMIC INSERT INTO RISK.AUDIT_TRAIL (TABLE_NAME, ACTION, GRT,    GRTNAME, GRTDESC) VALUES ('GENERIC','D', OLD.GRT,   OLD.GRTNAME, OLD.GRTDESC); END;
Relational Theory N th   normal form Primary Keys Unique Indexes Foreign Keys
Referential Integrity EMP EMPNO FIRSTNAME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE JOB SEX BIRTHDATE SALARY BONUS DEPT DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION
Indexes A structure that orders the values of columns in a table Administers Uniqueness Keeps data within the table in order Improves performance of SELECTS, UPDATES and DELETES Too many can degrade performance of INSERTS, some UPDATES and some DELETES
Indexes INDEXSPACE TABLESPACE TABLE DEPT INDEX on DEPTNO A00 B01 C01 D01
DB2 Commands and Utilities Data Consistency Utilities Backup and Recovery Utilities Data Organisation Utilities Catalog Manipulation Utilities Offline Utilities Sample Programs DB2 Environment Commands DSN Commands
Some DB2 Utilities LOAD COPY REBUILD REORG RUNSTATS CHECK DATA REPAIR DIAGNOSE MERGECOPY MODIFY QUIESCE RECOVER REPORT STOSPACE
Some DB2 Commands DISPLAY DATABASE (db) {SPACENAM(space)} BUFFERPOOL {ACTIVE | (*) | (bpname)} THREAD UTILITY START DATABASE (db) {SPACENAM(space)} STOP DATABASE (db) {SPACENAM(space)} TERM UTILITY (util) CANCEL THREAD (token) ALTER BUFFERPOOL(bpname)
TSO DB2 Commands DSN  {SYSTEM(subsystem)} BIND PLAN PACKAGE DCLGEN FREE PLAN PACKAGE REBIND PLAN PACKAGE RUN PROGRAM (progname) PLAN(planname) END
Sample Database
Sample Database 1 2 3 4 5 6 7 8 9 10 11 DEPT EMP PROJ EMPPROJACT EMP_ PHOTO_ RESUME ACT PROJACT
Sample Database ACTNO  ACTKWD  ACTDESC ACT DEPT DEPTNO  DEPTNAME MGRNO  ADMRDEPT LOCATION EMP EMPNO  FIRSTNME  MIDINIT  LASTNAME  WORKDEPT  PHONENO  HIREDATE  JOB  EDLEVEL  SEX  BIRTHDATE SALARY  BONUS  COMM  EMP_ PHOTO_ RESUME EMPNO  EMP_ROWID  PSEG_PHOTO BMP_PHOTO  RESUME  EMPPROJACT EMPNO  PROJNO  ACTNO  EMPTIME  EMSTDATE EMENDATE PROJACT PROJNO  ACTNO  ACSTAFF  ACSTDATE ACENDATE PROJ PROJNO  PROJNAME DEPTNO  RESPEMP  PRSTAFF  PRSTDATE PRENDATE MAJPROJ
Sample Database Create SQL Data Set Copy DDL members Ponder your DDL Execute it Play with it Now wash your hands
Sample Database Database Tablespace Simple Segmented Partitioned LOB Tables Bog standard Auxiliary Aliases Synonyms Indexes Unique Clustering Partitioning Views Simple Complex RI Primary Keys Foreign Keys
Sample Database Tables ACT  DEPT  EMP  EMP_PHOTO_RESUME EMPPROJACT  PROJ  PROJACT AUX_BMP_PHOTO  AUX_EMP_RESUME AUX_PSEG_PHOTO Views VACT  VASTRDE1  VASTRDE2  VDEPMG1  VDEPT  VEMP  VEMPDPT1  VEMPLP  VEMPPROJACT VFORPLA  VHDEPT  VPHONE  VPROJ  VPROJACT  VPROJRE1  VPSTRDE1  VPSTRDE2  VSTAFAC1  VSTAFAC2
The Joy of SQL Simple English Intuitive Flexible Complex Powerful You specify what, but don’t care how (DB2 takes care of that!)
The Joy of SELECTs SELECT {  ALL  | DISTINCT } [ * | [ [ expression | function | constant | column | host-variable | special-register | case-expression { { AS } column } ]  |  table.* | view.* | correlation.*], ] FROM [table | view | correlation | subselect  [{INNER | LEFT OUTER | FULL OUTER } JOIN joined-table  ON join-condition] WHERE [predicate | {NOT} = | < | > | <= | => | EXISTS |  IN (in-list | subselect) | LIKE { ‘value%’ | ‘%value%’ | ‘%value’ }  value | subselect] GROUP BY [columns,] HAVING [predicate operator {value | subselect} ]
Exercise Using simple SQL… 1. What is the full name and birthday of employee ‘000030’? 2. Which people have a salary of between $30,000 and $50,000? 3. Which people work in department ‘B01’ or department numbers beginning with ‘C’? 4. What department is ‘E21’?
Advanced SQL
Advanced SQL Built-in Functions Joins Unions Subqueries and Nested Table Expressions Sorting Aggregation CASE expressions Date / Time Arithmetic Global Temporary Tables Existence
Column Functions Only in SELECT statements Operates upon a group of rows Apply to a named column or expression Returns a single value for the set of selected rows Returned value is of the same data type If clause finds no data, a null is returned, not SQLCODE+100 Can be used to aggregate data with the GROUP BY clause
Scalar Functions Apply to a column or expression Operate on a single value (not a set of data) May transform data type of original column Over 80 functions available User defined functions can be defined
Special Registers Date/Time CURRENT DATE CURRENT TIME CURRENT TIMESTAMP Environment CURRENT DEGREE CURRENT SQLID CURRENT SERVER CURRENT PACKAGESET USER …and a few more uninteresting ones…
Joins Combining data from multiple tables based on the values of one or more columns in each table Cartesian Product no matching columns specified Inner Join where all columns match Left Outer Join all matching rows all unmatching rows from Left table Full Outer Join all matching rows all unmatching rows
Inner Join SELECT EMPNO,LASTNAME, WORKDEPT FROM  EMP WHERE  WORKDEPT LIKE ‘D%'; SELECT DEPTNO,DEPTNAME FROM  DEPT WHERE  DEPTNO LIKE 'D%'
Inner Join SELECT EMPNO, LASTNAME, WORKDEPT DEPTNAME FROM  EMP JOIN  DEPT ON  WORKDEPT = DEPTNO WHERE  DEPTNO LIKE 'D%' ;
Outer Join SELECT EMPNO, LASTNAME, WORKDEPT DEPTNAME FROM  EMP RIGHT OUTER JOIN DEPT ON  WORKDEPT = DEPTNO WHERE  DEPTNO LIKE 'D%' ;
Unions Combining data from multiple results sets composed of all the rows in both of the two original sets Must contain the same number of columns In the same order Of the same data type (or convertible) Contains no duplicates But UNION ALL overcomes this
Subqueries Nested SELECT statements IN (or NOT IN) EXISTS (or NOT EXISTS) Comparative, equality/inequality operators (=, <>, <, <=, >, >+) Correlated subqueries Non-correlated subqueries
Nested Table Expressions Allows the FROM clause to access another SELECT statement Wherever you see a table name, it can be another SELECT statement Sometimes called  inline views Can be replaced by VIEWs, but are easier to understand Must  be enclosed by parentheses Must  be given a correlation name Reaches the parts other statements cannot reach
Sorting By use of the ORDER BY clause ORDER BY column1 ASC, column2 DESC specify the columns explicitly ORDER BY 1, 2, 3 specify the columns as they appear in the SELECT statement Can be avoided by using an index which already specifies the columns and their order
Aggregation “ Bunching up” Using GROUP BY and HAVING clauses GROUP BY collates resultant rows to apply functions that consolidate the data can apply statistical functions on a column HAVING eliminates non-pertinent groups of data from the aggregated results set
Relational Division Very useful, but very complex The relational division of two tables is the operation of returning rows whereby column values in one table match column values for  every  corresponding row in the other table A combination of correlated subqueries Example (SQL19) retrieves all projects that require every activity listing in the activity table (No rows returned because no projects in the sample database require all activities) Very powerful - SQL will out-perform any application-based solution using separate cursors to process three individual SELECT statements (I hardly ever use it!)
CASE Expressions Uses the value of an expression to select one statement among many for execution Similar to programming language usage Simplifies UNION coding Improves performance Very flexible Can be used in SELECT clause and WHERE clause
Date and Time Arithmetic Special data types DATE TIME TIMESTAMP One column instead of two Greater granularity Column functions to transform data types YEAR, MONTH, DAY, HOUR, MINUTE, SECOND Arithmetic functions easy But don’t mix dates with non-dates!
Global Temporary Tables Stores intermediate SQL results Lasts as long as the process that uses it CREATE GLOBAL TEMPORARY TABLE Does not physically exist, but is instantiated when first referenced Locking, logging and recovery do not apply No indexes - table scan access No constraints or RI No updates allowed, only SELECTs, INSERTs and DELETEs Great for Stored Procedures Performance benefits
Existence Not a David Cronenberg film A much faster way of seeing if qualified data exists than actually selecting it. Less I/O to the data Not used often enough! More than one way to skin a cat With an AK-47 sub-machine gun With a spoon With a blunt knife With a scalpel
Exercise 5. Rewrite member SQL13 to use an IN list instead of a UNION. 6. Who is Bruce Adamson’s boss? 7. Which employees have worked on project ‘OPERATION’? 8. How many people work for department ‘E21’ and what department is it? 9. Project ‘IF2000’ is allocated to which department? 10. What is the average salary for each department? 11. Which employee is paid the most? 12. Which employee is paid the least? 13. Who is the oldest employee? 14. Which projects are over/under budget, and by how much? 15. How many people work for each department?
Embedded SQL
Embedded SQL Static More efficient Takes longer to code Access path evaluation at Bind time Hard coded Dynamic More flexible Can be modified  during  program execution Requires PREPARE statements evaluated at execution time Access path evaluation at Execution time
Static Program Preparation DB2 Catalog DB2 Directory Source Program DCLGEN Precompiler Modified Source DBRM Bind Compile Link edit Package Plan Load Module
Collections in HUON Source Program DBRM Load Module Bind Package in FIGE1 collection FIGE1 tables Package in FIGE2 collection FIGE2 tables Package in FIGE3 collection FIGE3 tables Bind Bind
Host variables Output areas in the INTO clause of SELECTs and FETCHes Input areas for the SET clause of UPDATEs Input areas for the VALUES clause if INSERTs Search fields in the WHERE clause Literals in the SELECT list of a SELECT statement Include indicator variables for possible null values
Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns
Programming with cursors DECLARE C1 CURSOR FOR SELECT DEPTNO, DEPTNAME FROM DEPT  WHERE ADMRDEPT = :ADMRDEPT
Programming with cursors OPEN C1 Results Set
Programming with cursors First fetch... FETCH C1 INTO  :DEPTNO, :DEPTNAME Results Set
Programming with cursors Next one… FETCH C1 INTO  :DEPTNO, :DEPTNAME Results Set
Programming with cursors And again… FETCH C1 INTO  :DEPTNO, :DEPTNAME Results Set
Programming with cursors And again… FETCH C1 INTO  :DEPTNO, :DEPTNAME Results Set
Programming with cursors …and so on until the last line of the results set… …the next FETCH returns an SQLCODE of +100, with no rows returned. Host variables may or may not contain valid data at this point, so ALWAYS check your SQLCODE before processing your data! Results Set . . .
Programming with cursors Always clean up your cursors! Data processing is finalised, the cursor is closed and all your results set storage is cleared up once you issue the CLOSE C1 statement, after your FETCH processing gets the +100 SQLCODE signifying the end of the results set.
Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns
Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns WITH HOLD
Explicitly declare all DB2 tables DCLGEN SQLCA SQLDA Error handling Check SQLCODE and SQLSTATE  every time! Use standard error routine if available More Programming Tips
Performance: The Black Arts
Performance Commitment control COMMIT ROLLBACK Locking Concurrency Isolation Levels BIND parameters  (package level) the WITH clause  (statement level) SQL use indexes careful predicates
Performance Best Coding Practice Online environments plan for multiple user concurrency Batch environments frequent commits restartability Tools DB2 Traces, MainView, Strobe EXPLAIN PLAN_TABLE
Commitment UNIT OF WORK COMMIT EXEC CICS SYNCPOINT Externalises updated data since Unit of Work began Releases locks ROLLBACK EXEC CICS ROLLBACK Undoes updated data since Unit of Work began Releases locks Automatic rollback on error/failure
Unit of Work time Program begins Update Insert Delete COMMIT Log
Unit of Work time Error! Program begins Update Insert Delete ROLLBACK
Locking Ensures data integrity Prevents multiple conflicting updates Level of Locking Table, Tablespace Page Row Locks released on COMMIT or ROLLBACK Locks consume resources Lock escalation LOCK TABLE command
Concurrency Page Lock Matrix
Isolation Levels Repeatable Read (RR) I’m reading it. You can’t have it. Bog off. Read Stability (RS) I’m reading it, but you can add more pages at the end. Cursor Stability (CS) I’m reading it, but you can update what I’ve read once I’ve turned the page. Uncommitted (dirty) Read (UR) I’m reading it, but you can change it while I’m reading it, and I don’t care if you back it out before I’ve finished.
Example Locking Commit/Rollback Concurrency Isolation Levels
SQL Performance The No. 1 biggest performance headache Why? Badly coded SQL Is your SQL the best method of getting your data? Is there an alternative way of coding your SQL?  Badly designed or missing predicates Especially high-order columns Available indexes not used Check first Badly designed database Indexes incorrectly specified The No. 1 easiest and quickest win
Best Coding Practice Online environments plan for multiple user concurrency keep UPDATES, INSERTS and DELETES to a minimum, and close together, if possible COMMIT a.s.a.p. afterwards Batch environments frequent commits (every 1000 updates or so) plan for restartability Use UR with caution can bring massive benefits can bring disastrous results
Performance Tools DB2 Traces Performance Audit Statistics DB2 Explain BMC AppTune MainView CICS DB2 z/OS Strobe DB2 activity CICS activity COBOL statements RMF system delays disk/data set contention CANDLE Workbench
EXPLAIN EXPLAIN ALL SET QUERY=1 FOR SELECT FIRSTNME, MIDINIT,   LASTNAME FROM EMP WHERE EMPNO=‘000240’; Access Paths UDF Resolution Cost Estimates Explain Plan, Package or SQL statement DB2 Optimizer PLAN_TABLE DSN_STATEMENT_TABLE DSN_FUNCTION_TABLE
Tools of the trade
Tools Data Entry and Modification SPUFI File Aid for DB2 CA Pro-Edit BMC Catalog Manager Catalog Navigation SPUFI BMC Catalog Manager Query Reporting SPUFI QMF Performance Candle Explain
Tools File Aid for DB2 Browsing and Editing tables Loading and Unloading tables BMC Catalog Manager Browsing and Editing tables DB2 object dependencies Catalog Navigation JCL and Utility generation SPUFI Data modification Reporting Catalog Navigation Candle Explain
Introduction to DB2 The Sample Database Advanced SQL Embedded SQL Performance Topics Tools of the trade DB2 for OS/390 Fundamentals
Further help is available from: Manuals Books The DB2 Developer’s Guide DB2 Answers The Internet http://www.ibm.com/software/data/db2/ More courses(!) Your friendly DBA DB2 for OS/390 Fundamentals
Ad

More Related Content

What's hot (20)

Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
Nitesh Singh
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)
Jennifer Berk
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
Ram Sagar Mourya
 
SQL Views
SQL ViewsSQL Views
SQL Views
baabtra.com - No. 1 supplier of quality freshers
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
Uttam Singh Chaudhary
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Amin Choroomi
 
Partitioning on Oracle 12c - What changed on the most important Oracle feature
Partitioning on Oracle 12c - What changed on the most important Oracle featurePartitioning on Oracle 12c - What changed on the most important Oracle feature
Partitioning on Oracle 12c - What changed on the most important Oracle feature
Luis Marques
 
STRUCTURED QUERY LANGUAGE
STRUCTURED QUERY LANGUAGESTRUCTURED QUERY LANGUAGE
STRUCTURED QUERY LANGUAGE
SarithaDhanapal
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
Richie Rump
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
Sunita Milind Dol
 
Oraclesql
OraclesqlOraclesql
Oraclesql
Priya Goyal
 
Create table
Create tableCreate table
Create table
Nitesh Singh
 
Sql queries
Sql queriesSql queries
Sql queries
narendrababuc
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
Sabana Maharjan
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
Chhom Karath
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
Balqees Al.Mubarak
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
webhostingguy
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 

Viewers also liked (13)

Moda perakendesinde iş modeli seçimi ima - burak günbal - 15-10-11
Moda perakendesinde iş modeli seçimi   ima - burak günbal - 15-10-11Moda perakendesinde iş modeli seçimi   ima - burak günbal - 15-10-11
Moda perakendesinde iş modeli seçimi ima - burak günbal - 15-10-11
Burak Günbal
 
Waisman Center, 08.18.10
Waisman Center, 08.18.10Waisman Center, 08.18.10
Waisman Center, 08.18.10
JacquiSakowski
 
Coherence - Pitch SV
Coherence - Pitch SVCoherence - Pitch SV
Coherence - Pitch SV
Jakub Bares
 
Seminario PresentacióN
Seminario PresentacióNSeminario PresentacióN
Seminario PresentacióN
Ellen Sanz
 
Tainted LOB
Tainted LOBTainted LOB
Tainted LOB
Marcus Davage
 
Social Media Brian Johnson Program, 07.14.10
Social Media Brian Johnson Program, 07.14.10Social Media Brian Johnson Program, 07.14.10
Social Media Brian Johnson Program, 07.14.10
JacquiSakowski
 
RTL101: Perakende Sistemlerine Giriş
RTL101: Perakende Sistemlerine GirişRTL101: Perakende Sistemlerine Giriş
RTL101: Perakende Sistemlerine Giriş
Burak Günbal
 
Meals of the_world
Meals of the_worldMeals of the_world
Meals of the_world
Ellen Sanz
 
Moda Perakendesi Yönetimi Eğitimi İçerik Sunumu
Moda Perakendesi Yönetimi Eğitimi İçerik SunumuModa Perakendesi Yönetimi Eğitimi İçerik Sunumu
Moda Perakendesi Yönetimi Eğitimi İçerik Sunumu
Burak Günbal
 
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu) ima- burak günbal - 0...
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu)   ima- burak günbal - 0...Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu)   ima- burak günbal - 0...
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu) ima- burak günbal - 0...
Burak Günbal
 
CHRISTMAS VOCABULARY
CHRISTMAS VOCABULARYCHRISTMAS VOCABULARY
CHRISTMAS VOCABULARY
Ellen Sanz
 
Models of Strategic Thinking
Models of Strategic ThinkingModels of Strategic Thinking
Models of Strategic Thinking
Burak Günbal
 
Zara: Fast Fashion
Zara: Fast FashionZara: Fast Fashion
Zara: Fast Fashion
Burak Günbal
 
Moda perakendesinde iş modeli seçimi ima - burak günbal - 15-10-11
Moda perakendesinde iş modeli seçimi   ima - burak günbal - 15-10-11Moda perakendesinde iş modeli seçimi   ima - burak günbal - 15-10-11
Moda perakendesinde iş modeli seçimi ima - burak günbal - 15-10-11
Burak Günbal
 
Waisman Center, 08.18.10
Waisman Center, 08.18.10Waisman Center, 08.18.10
Waisman Center, 08.18.10
JacquiSakowski
 
Coherence - Pitch SV
Coherence - Pitch SVCoherence - Pitch SV
Coherence - Pitch SV
Jakub Bares
 
Seminario PresentacióN
Seminario PresentacióNSeminario PresentacióN
Seminario PresentacióN
Ellen Sanz
 
Social Media Brian Johnson Program, 07.14.10
Social Media Brian Johnson Program, 07.14.10Social Media Brian Johnson Program, 07.14.10
Social Media Brian Johnson Program, 07.14.10
JacquiSakowski
 
RTL101: Perakende Sistemlerine Giriş
RTL101: Perakende Sistemlerine GirişRTL101: Perakende Sistemlerine Giriş
RTL101: Perakende Sistemlerine Giriş
Burak Günbal
 
Meals of the_world
Meals of the_worldMeals of the_world
Meals of the_world
Ellen Sanz
 
Moda Perakendesi Yönetimi Eğitimi İçerik Sunumu
Moda Perakendesi Yönetimi Eğitimi İçerik SunumuModa Perakendesi Yönetimi Eğitimi İçerik Sunumu
Moda Perakendesi Yönetimi Eğitimi İçerik Sunumu
Burak Günbal
 
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu) ima- burak günbal - 0...
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu)   ima- burak günbal - 0...Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu)   ima- burak günbal - 0...
Perakende nedir? Perakendeci kimdir? (Tanisma Sunumu) ima- burak günbal - 0...
Burak Günbal
 
CHRISTMAS VOCABULARY
CHRISTMAS VOCABULARYCHRISTMAS VOCABULARY
CHRISTMAS VOCABULARY
Ellen Sanz
 
Models of Strategic Thinking
Models of Strategic ThinkingModels of Strategic Thinking
Models of Strategic Thinking
Burak Günbal
 
Ad

Similar to MDI Training DB2 Course (20)

Database Foundation Training
Database Foundation TrainingDatabase Foundation Training
Database Foundation Training
Franky Lao
 
Module02
Module02Module02
Module02
Sridhar P
 
Getting Started with SQL Language.pptx
Getting Started with SQL Language.pptxGetting Started with SQL Language.pptx
Getting Started with SQL Language.pptx
Cecilia Brusatori
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
Cathie101
 
Training on Microsoft SQL Server(older version).pptx
Training on Microsoft SQL Server(older version).pptxTraining on Microsoft SQL Server(older version).pptx
Training on Microsoft SQL Server(older version).pptx
naibedyakar00
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
Om Vikram Thapa
 
Module 02 teradata basics
Module 02 teradata basicsModule 02 teradata basics
Module 02 teradata basics
Md. Noor Alam
 
Complete SQL in one video by shradha.pdf
Complete SQL in one video by shradha.pdfComplete SQL in one video by shradha.pdf
Complete SQL in one video by shradha.pdf
rahulashu699
 
presentasi romi-java-06-database-october2013.pptx
presentasi romi-java-06-database-october2013.pptxpresentasi romi-java-06-database-october2013.pptx
presentasi romi-java-06-database-october2013.pptx
steeveenn
 
lovely
lovelylovely
lovely
love0323
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
Srinath Maharana
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Serban Tanasa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
Odtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for youOdtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for you
Luc Bors
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
Rumman Ansari
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
MariaDB plc
 
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
demomki4
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
Eric Nelson
 
Database Foundation Training
Database Foundation TrainingDatabase Foundation Training
Database Foundation Training
Franky Lao
 
Getting Started with SQL Language.pptx
Getting Started with SQL Language.pptxGetting Started with SQL Language.pptx
Getting Started with SQL Language.pptx
Cecilia Brusatori
 
Training on Microsoft SQL Server(older version).pptx
Training on Microsoft SQL Server(older version).pptxTraining on Microsoft SQL Server(older version).pptx
Training on Microsoft SQL Server(older version).pptx
naibedyakar00
 
Module 02 teradata basics
Module 02 teradata basicsModule 02 teradata basics
Module 02 teradata basics
Md. Noor Alam
 
Complete SQL in one video by shradha.pdf
Complete SQL in one video by shradha.pdfComplete SQL in one video by shradha.pdf
Complete SQL in one video by shradha.pdf
rahulashu699
 
presentasi romi-java-06-database-october2013.pptx
presentasi romi-java-06-database-october2013.pptxpresentasi romi-java-06-database-october2013.pptx
presentasi romi-java-06-database-october2013.pptx
steeveenn
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Serban Tanasa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
Odtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for youOdtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for you
Luc Bors
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
MariaDB plc
 
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
Java Database Connectivity (JDBC) with Spring Framework is a powerful combina...
demomki4
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
Eric Nelson
 
Ad

MDI Training DB2 Course

  • 1. DB2 for OS/390 Fundamentals
  • 2. Introduction to DB2 The Sample Database Advanced SQL Embedded SQL Performance Topics Tools of the trade DB2 for OS/390 Fundamentals
  • 3. Introduction to DB2 Universal Database
  • 4. Introduction to DB2 History Workings Address spaces Threads Attachment Facilities SQL DB2 Authorities DB2 Objects Referential Integrity Commands and Utilities
  • 5. A brief DB2 History 1983 DB2 v1 ships 1993 DB2 for OS/2 and Aix 1994 DB2 for MVS v4 introduces Data Sharing 1996 DB2 UDB - multimedia, web-ready, cross-platform (non-mainframe) 1999 DB2 UDB for OS/390 (v6.1) 2001 DB2 UDB for OS/390 and z/OS (v7.1) 2004 DB2 UDB for z/OS (v8.1) 2004 DB2 UDB for LUW (v8.2)
  • 6. The Guts of DB2 Directory Catalog Tables Logs BSDS DB2 User Thread EDM Pool Sort Pool Buffer Pools RID Pool Temporary Database Working Storage Program Logic
  • 7. The Guts of DB2 Address Space Name Functions DBAS Database Services Address Space SSAS System Services Address Space IRLM Intersystem Resource Lock Manager DDF Distributed Data Facility SPAS Stored Procedure Address Space(s)
  • 8. Threads DB2 CICS Programs DB2 Utilities TSO Online Programs TSO Batch Programs QMF/DB2I (SPUFI) thread threads thread thread thread Distributed Programs thread
  • 9. Attachment Facilities DB2 TSO Address Space DSN Processor ISPF DB2I SPUFI QMF Rexx
  • 10. Data Sharing Shared data between subsystems Increased availability to data Expanded capacity Increased flexibility Easier upgrade path Parallelism exploited
  • 11. Parallelism Partitioned Tablespaces DEGREE(ANY) BIND parameter SET CURRENT DEGREE = ‘ANY’; SQL statement Types of Parallelism I/O multiple read engines CP multiple processor and read engines Sysplex multiple data sharing subsystems
  • 12. SQL SQL DCL DDL DML GRANT REVOKE CREATE ALTER DROP SELECT UPDATE INSERT DELETE
  • 13. Results Sets DB2 Request sent to database Multiple rows returned as a results set
  • 14. Authorities SYSADM Systems Administrator Can do anything to everything DBADM Database Administrator Can do anything to a database User-based Select, Update, Insert, Delete access on permissions tables; Execute, Bind access on packages; utilities/operations upon objects. SYSMAINT/SYSCTRL Other administrative DBMAINT/DBCTRL functions in DB2 }{
  • 15. DB2 Objects STOGROUP DATABASE TABLESPACE TABLE VIEW INDEX ALIAS SYNONYM COLUMN
  • 16. DB2’s VSAM data sets M enu O ptions V iew U tilities C ompilers H elp ----------------------------------------------------------------------------- DSLIST - Data Sets Matching FIGDB2.DSNDBC.DFIGE0H.*ABORT* Row 1 of 6 Command ===> Scroll ===> CSR Command - Enter &quot;/&quot; to select action Tracks %Used XT Device ----------------------------------------------------------------------------- FIGDB2.DSNDBC.DFIGE0H.SABORT.I0001.A001 FIGDB2.DSNDBD.DFIGE0H.SABORT.I0001.A001 1950 ? 31 3390 FIGDB2.DSNDBC.DFIGE0H.XABORT0.I0001.A001 FIGDB2.DSNDBD.DFIGE0H.XABORT0.I0001.A001 120 ? 6 3390 FIGDB2.DSNDBC.DFIGE0H.IABORT1.I0001.A001 FIGDB2.DSNDBD.DFIGE0H.IABORT1.I0001.A001 16 ? 15 3390 *************************** End of Data Set list **************************** Partition number Tablespace/Index name Database name High-level qualifier
  • 17. Tablespaces Simple only when you want to mix data from different tables on one page. Segmented handles space more efficiently separates data from multiple tables in the same tablespace Partitioned utilise parallelism very large amounts of data isolate data in dedicated page sets LOB (Large Object) to store LOB data only
  • 19. Columns Requires a name Cannot have duplicate names within a table Requires a data type Other attributes NULLS ALLOWED/NOT NULL DEFAULT VALUE PRIMARY KEY CHECK CONSTRAINT
  • 20. Data types SMALLINT/INTEGER CHARACTER/VARCHAR GRAPHIC/VARGRAPHIC/DBCLOB CHARACTER LARGE OBJECT/CLOB BINARY LARGE OBJECT/BLOB DECIMAL/NUMERIC FLOAT/REAL/DOUBLE DATE TIME TIMESTAMP ROWID
  • 21. User-Defined Data types The user-defined DISTINCT TYPE is based on one of the previous basic data types. E.g. CREATE DISTINCT TYPE AUDIO AS BLOB (1M); CREATE DISTINCT TYPE VIDEO AS BLOB (1M); Then, your CREATE TABLE statement might look like this: CREATE TABLE VIDEO_CATALOG (VIDEO_NUMBER CHAR(6) NOT NULL, VIDEO_SOUND AUDIO, VIDEO_PICS VIDEO, ROW_ID ROWID NOT NULL GENERATED ALWAYS);
  • 22. Functions Column Functions Scalar Functions User-defined Functions
  • 23. Routines User-defined Functions Sourced External Stored Procedures See next foil...
  • 24. Stored Procedures Calling Program MVS Schedule SP SELECT INSERT UPDATE DELETE DB2 Start Proc SELECT INSERT UPDATE DELETE End Proc SPAS Call Results
  • 25. Other features Relational Integrity Joining tables relationally Triggers firing actions to other tables (or calling stored procedures which can call other programs) when tables are modified Constraints build data rules into the database Labels
  • 26. Check Constraints Data value restrictions on the contents of a column. EXTN CHAR(4) CONSTRAINT NUMBER CHECK (EXTN BETWEEN ‘0000’ AND ‘9999’) CONSTRAINT BYPASS CHECK(BYPASS IN ('Y', 'N') AND (BYPASS = 'N' OR (BYPASS = 'Y’ AND REASON IS NOT NULL)))
  • 27. Triggers CREATE TRIGGER UPD_SALARY BEFORE UPDATE OF SALARY ON EMP FOR EACH ROW MODE DB2SQL WHEN (NEW.SALARY>(OLD.SALARY*1.5)) BEGIN ATOMIC SIGNAL SQLSTATE ‘75001’ (‘Raise exceeds 50%’); END; CREATE TRIGGER RISK.GRT_DEL AFTER DELETE ON RISK.GENERIC FOR EACH ROW MODE DB2SQL BEGIN ATOMIC INSERT INTO RISK.AUDIT_TRAIL (TABLE_NAME, ACTION, GRT, GRTNAME, GRTDESC) VALUES ('GENERIC','D', OLD.GRT, OLD.GRTNAME, OLD.GRTDESC); END;
  • 28. Relational Theory N th normal form Primary Keys Unique Indexes Foreign Keys
  • 29. Referential Integrity EMP EMPNO FIRSTNAME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE JOB SEX BIRTHDATE SALARY BONUS DEPT DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION
  • 30. Indexes A structure that orders the values of columns in a table Administers Uniqueness Keeps data within the table in order Improves performance of SELECTS, UPDATES and DELETES Too many can degrade performance of INSERTS, some UPDATES and some DELETES
  • 31. Indexes INDEXSPACE TABLESPACE TABLE DEPT INDEX on DEPTNO A00 B01 C01 D01
  • 32. DB2 Commands and Utilities Data Consistency Utilities Backup and Recovery Utilities Data Organisation Utilities Catalog Manipulation Utilities Offline Utilities Sample Programs DB2 Environment Commands DSN Commands
  • 33. Some DB2 Utilities LOAD COPY REBUILD REORG RUNSTATS CHECK DATA REPAIR DIAGNOSE MERGECOPY MODIFY QUIESCE RECOVER REPORT STOSPACE
  • 34. Some DB2 Commands DISPLAY DATABASE (db) {SPACENAM(space)} BUFFERPOOL {ACTIVE | (*) | (bpname)} THREAD UTILITY START DATABASE (db) {SPACENAM(space)} STOP DATABASE (db) {SPACENAM(space)} TERM UTILITY (util) CANCEL THREAD (token) ALTER BUFFERPOOL(bpname)
  • 35. TSO DB2 Commands DSN {SYSTEM(subsystem)} BIND PLAN PACKAGE DCLGEN FREE PLAN PACKAGE REBIND PLAN PACKAGE RUN PROGRAM (progname) PLAN(planname) END
  • 37. Sample Database 1 2 3 4 5 6 7 8 9 10 11 DEPT EMP PROJ EMPPROJACT EMP_ PHOTO_ RESUME ACT PROJACT
  • 38. Sample Database ACTNO ACTKWD ACTDESC ACT DEPT DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION EMP EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE JOB EDLEVEL SEX BIRTHDATE SALARY BONUS COMM EMP_ PHOTO_ RESUME EMPNO EMP_ROWID PSEG_PHOTO BMP_PHOTO RESUME EMPPROJACT EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE PROJACT PROJNO ACTNO ACSTAFF ACSTDATE ACENDATE PROJ PROJNO PROJNAME DEPTNO RESPEMP PRSTAFF PRSTDATE PRENDATE MAJPROJ
  • 39. Sample Database Create SQL Data Set Copy DDL members Ponder your DDL Execute it Play with it Now wash your hands
  • 40. Sample Database Database Tablespace Simple Segmented Partitioned LOB Tables Bog standard Auxiliary Aliases Synonyms Indexes Unique Clustering Partitioning Views Simple Complex RI Primary Keys Foreign Keys
  • 41. Sample Database Tables ACT DEPT EMP EMP_PHOTO_RESUME EMPPROJACT PROJ PROJACT AUX_BMP_PHOTO AUX_EMP_RESUME AUX_PSEG_PHOTO Views VACT VASTRDE1 VASTRDE2 VDEPMG1 VDEPT VEMP VEMPDPT1 VEMPLP VEMPPROJACT VFORPLA VHDEPT VPHONE VPROJ VPROJACT VPROJRE1 VPSTRDE1 VPSTRDE2 VSTAFAC1 VSTAFAC2
  • 42. The Joy of SQL Simple English Intuitive Flexible Complex Powerful You specify what, but don’t care how (DB2 takes care of that!)
  • 43. The Joy of SELECTs SELECT { ALL | DISTINCT } [ * | [ [ expression | function | constant | column | host-variable | special-register | case-expression { { AS } column } ] | table.* | view.* | correlation.*], ] FROM [table | view | correlation | subselect [{INNER | LEFT OUTER | FULL OUTER } JOIN joined-table ON join-condition] WHERE [predicate | {NOT} = | < | > | <= | => | EXISTS | IN (in-list | subselect) | LIKE { ‘value%’ | ‘%value%’ | ‘%value’ } value | subselect] GROUP BY [columns,] HAVING [predicate operator {value | subselect} ]
  • 44. Exercise Using simple SQL… 1. What is the full name and birthday of employee ‘000030’? 2. Which people have a salary of between $30,000 and $50,000? 3. Which people work in department ‘B01’ or department numbers beginning with ‘C’? 4. What department is ‘E21’?
  • 46. Advanced SQL Built-in Functions Joins Unions Subqueries and Nested Table Expressions Sorting Aggregation CASE expressions Date / Time Arithmetic Global Temporary Tables Existence
  • 47. Column Functions Only in SELECT statements Operates upon a group of rows Apply to a named column or expression Returns a single value for the set of selected rows Returned value is of the same data type If clause finds no data, a null is returned, not SQLCODE+100 Can be used to aggregate data with the GROUP BY clause
  • 48. Scalar Functions Apply to a column or expression Operate on a single value (not a set of data) May transform data type of original column Over 80 functions available User defined functions can be defined
  • 49. Special Registers Date/Time CURRENT DATE CURRENT TIME CURRENT TIMESTAMP Environment CURRENT DEGREE CURRENT SQLID CURRENT SERVER CURRENT PACKAGESET USER …and a few more uninteresting ones…
  • 50. Joins Combining data from multiple tables based on the values of one or more columns in each table Cartesian Product no matching columns specified Inner Join where all columns match Left Outer Join all matching rows all unmatching rows from Left table Full Outer Join all matching rows all unmatching rows
  • 51. Inner Join SELECT EMPNO,LASTNAME, WORKDEPT FROM EMP WHERE WORKDEPT LIKE ‘D%'; SELECT DEPTNO,DEPTNAME FROM DEPT WHERE DEPTNO LIKE 'D%'
  • 52. Inner Join SELECT EMPNO, LASTNAME, WORKDEPT DEPTNAME FROM EMP JOIN DEPT ON WORKDEPT = DEPTNO WHERE DEPTNO LIKE 'D%' ;
  • 53. Outer Join SELECT EMPNO, LASTNAME, WORKDEPT DEPTNAME FROM EMP RIGHT OUTER JOIN DEPT ON WORKDEPT = DEPTNO WHERE DEPTNO LIKE 'D%' ;
  • 54. Unions Combining data from multiple results sets composed of all the rows in both of the two original sets Must contain the same number of columns In the same order Of the same data type (or convertible) Contains no duplicates But UNION ALL overcomes this
  • 55. Subqueries Nested SELECT statements IN (or NOT IN) EXISTS (or NOT EXISTS) Comparative, equality/inequality operators (=, <>, <, <=, >, >+) Correlated subqueries Non-correlated subqueries
  • 56. Nested Table Expressions Allows the FROM clause to access another SELECT statement Wherever you see a table name, it can be another SELECT statement Sometimes called inline views Can be replaced by VIEWs, but are easier to understand Must be enclosed by parentheses Must be given a correlation name Reaches the parts other statements cannot reach
  • 57. Sorting By use of the ORDER BY clause ORDER BY column1 ASC, column2 DESC specify the columns explicitly ORDER BY 1, 2, 3 specify the columns as they appear in the SELECT statement Can be avoided by using an index which already specifies the columns and their order
  • 58. Aggregation “ Bunching up” Using GROUP BY and HAVING clauses GROUP BY collates resultant rows to apply functions that consolidate the data can apply statistical functions on a column HAVING eliminates non-pertinent groups of data from the aggregated results set
  • 59. Relational Division Very useful, but very complex The relational division of two tables is the operation of returning rows whereby column values in one table match column values for every corresponding row in the other table A combination of correlated subqueries Example (SQL19) retrieves all projects that require every activity listing in the activity table (No rows returned because no projects in the sample database require all activities) Very powerful - SQL will out-perform any application-based solution using separate cursors to process three individual SELECT statements (I hardly ever use it!)
  • 60. CASE Expressions Uses the value of an expression to select one statement among many for execution Similar to programming language usage Simplifies UNION coding Improves performance Very flexible Can be used in SELECT clause and WHERE clause
  • 61. Date and Time Arithmetic Special data types DATE TIME TIMESTAMP One column instead of two Greater granularity Column functions to transform data types YEAR, MONTH, DAY, HOUR, MINUTE, SECOND Arithmetic functions easy But don’t mix dates with non-dates!
  • 62. Global Temporary Tables Stores intermediate SQL results Lasts as long as the process that uses it CREATE GLOBAL TEMPORARY TABLE Does not physically exist, but is instantiated when first referenced Locking, logging and recovery do not apply No indexes - table scan access No constraints or RI No updates allowed, only SELECTs, INSERTs and DELETEs Great for Stored Procedures Performance benefits
  • 63. Existence Not a David Cronenberg film A much faster way of seeing if qualified data exists than actually selecting it. Less I/O to the data Not used often enough! More than one way to skin a cat With an AK-47 sub-machine gun With a spoon With a blunt knife With a scalpel
  • 64. Exercise 5. Rewrite member SQL13 to use an IN list instead of a UNION. 6. Who is Bruce Adamson’s boss? 7. Which employees have worked on project ‘OPERATION’? 8. How many people work for department ‘E21’ and what department is it? 9. Project ‘IF2000’ is allocated to which department? 10. What is the average salary for each department? 11. Which employee is paid the most? 12. Which employee is paid the least? 13. Who is the oldest employee? 14. Which projects are over/under budget, and by how much? 15. How many people work for each department?
  • 66. Embedded SQL Static More efficient Takes longer to code Access path evaluation at Bind time Hard coded Dynamic More flexible Can be modified during program execution Requires PREPARE statements evaluated at execution time Access path evaluation at Execution time
  • 67. Static Program Preparation DB2 Catalog DB2 Directory Source Program DCLGEN Precompiler Modified Source DBRM Bind Compile Link edit Package Plan Load Module
  • 68. Collections in HUON Source Program DBRM Load Module Bind Package in FIGE1 collection FIGE1 tables Package in FIGE2 collection FIGE2 tables Package in FIGE3 collection FIGE3 tables Bind Bind
  • 69. Host variables Output areas in the INTO clause of SELECTs and FETCHes Input areas for the SET clause of UPDATEs Input areas for the VALUES clause if INSERTs Search fields in the WHERE clause Literals in the SELECT list of a SELECT statement Include indicator variables for possible null values
  • 70. Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns
  • 71. Programming with cursors DECLARE C1 CURSOR FOR SELECT DEPTNO, DEPTNAME FROM DEPT WHERE ADMRDEPT = :ADMRDEPT
  • 72. Programming with cursors OPEN C1 Results Set
  • 73. Programming with cursors First fetch... FETCH C1 INTO :DEPTNO, :DEPTNAME Results Set
  • 74. Programming with cursors Next one… FETCH C1 INTO :DEPTNO, :DEPTNAME Results Set
  • 75. Programming with cursors And again… FETCH C1 INTO :DEPTNO, :DEPTNAME Results Set
  • 76. Programming with cursors And again… FETCH C1 INTO :DEPTNO, :DEPTNAME Results Set
  • 77. Programming with cursors …and so on until the last line of the results set… …the next FETCH returns an SQLCODE of +100, with no rows returned. Host variables may or may not contain valid data at this point, so ALWAYS check your SQLCODE before processing your data! Results Set . . .
  • 78. Programming with cursors Always clean up your cursors! Data processing is finalised, the cursor is closed and all your results set storage is cleared up once you issue the CLOSE C1 statement, after your FETCH processing gets the +100 SQLCODE signifying the end of the results set.
  • 79. Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns
  • 80. Programming with cursors Rows vs. Results sets Syntax rules Retrieval Modification When to use cursors (and when not to) Gotchas SQLCODE -811 Table materialisation (OPEN or FETCH) Specify required columns WITH HOLD
  • 81. Explicitly declare all DB2 tables DCLGEN SQLCA SQLDA Error handling Check SQLCODE and SQLSTATE every time! Use standard error routine if available More Programming Tips
  • 83. Performance Commitment control COMMIT ROLLBACK Locking Concurrency Isolation Levels BIND parameters (package level) the WITH clause (statement level) SQL use indexes careful predicates
  • 84. Performance Best Coding Practice Online environments plan for multiple user concurrency Batch environments frequent commits restartability Tools DB2 Traces, MainView, Strobe EXPLAIN PLAN_TABLE
  • 85. Commitment UNIT OF WORK COMMIT EXEC CICS SYNCPOINT Externalises updated data since Unit of Work began Releases locks ROLLBACK EXEC CICS ROLLBACK Undoes updated data since Unit of Work began Releases locks Automatic rollback on error/failure
  • 86. Unit of Work time Program begins Update Insert Delete COMMIT Log
  • 87. Unit of Work time Error! Program begins Update Insert Delete ROLLBACK
  • 88. Locking Ensures data integrity Prevents multiple conflicting updates Level of Locking Table, Tablespace Page Row Locks released on COMMIT or ROLLBACK Locks consume resources Lock escalation LOCK TABLE command
  • 90. Isolation Levels Repeatable Read (RR) I’m reading it. You can’t have it. Bog off. Read Stability (RS) I’m reading it, but you can add more pages at the end. Cursor Stability (CS) I’m reading it, but you can update what I’ve read once I’ve turned the page. Uncommitted (dirty) Read (UR) I’m reading it, but you can change it while I’m reading it, and I don’t care if you back it out before I’ve finished.
  • 91. Example Locking Commit/Rollback Concurrency Isolation Levels
  • 92. SQL Performance The No. 1 biggest performance headache Why? Badly coded SQL Is your SQL the best method of getting your data? Is there an alternative way of coding your SQL? Badly designed or missing predicates Especially high-order columns Available indexes not used Check first Badly designed database Indexes incorrectly specified The No. 1 easiest and quickest win
  • 93. Best Coding Practice Online environments plan for multiple user concurrency keep UPDATES, INSERTS and DELETES to a minimum, and close together, if possible COMMIT a.s.a.p. afterwards Batch environments frequent commits (every 1000 updates or so) plan for restartability Use UR with caution can bring massive benefits can bring disastrous results
  • 94. Performance Tools DB2 Traces Performance Audit Statistics DB2 Explain BMC AppTune MainView CICS DB2 z/OS Strobe DB2 activity CICS activity COBOL statements RMF system delays disk/data set contention CANDLE Workbench
  • 95. EXPLAIN EXPLAIN ALL SET QUERY=1 FOR SELECT FIRSTNME, MIDINIT, LASTNAME FROM EMP WHERE EMPNO=‘000240’; Access Paths UDF Resolution Cost Estimates Explain Plan, Package or SQL statement DB2 Optimizer PLAN_TABLE DSN_STATEMENT_TABLE DSN_FUNCTION_TABLE
  • 96. Tools of the trade
  • 97. Tools Data Entry and Modification SPUFI File Aid for DB2 CA Pro-Edit BMC Catalog Manager Catalog Navigation SPUFI BMC Catalog Manager Query Reporting SPUFI QMF Performance Candle Explain
  • 98. Tools File Aid for DB2 Browsing and Editing tables Loading and Unloading tables BMC Catalog Manager Browsing and Editing tables DB2 object dependencies Catalog Navigation JCL and Utility generation SPUFI Data modification Reporting Catalog Navigation Candle Explain
  • 99. Introduction to DB2 The Sample Database Advanced SQL Embedded SQL Performance Topics Tools of the trade DB2 for OS/390 Fundamentals
  • 100. Further help is available from: Manuals Books The DB2 Developer’s Guide DB2 Answers The Internet http://www.ibm.com/software/data/db2/ More courses(!) Your friendly DBA DB2 for OS/390 Fundamentals