You are on page 1of 168

V5.

cover

Front cover

AI Certification Preparation for Exam 730


(Course code CE500)

Student Notebook
ERC 1.0

Student Notebook

Trademarks
IBM is a registered trademark of International Business Machines Corporation. The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both: AIX DB2 Informix OS/390 z/OS AT Distributed Relational Database Architecture iSeries OS/400 Database 2 DRDA MVS Rational

Windows and Windows NT are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. Other company, product, or service names may be trademarks or service marks of others.

February 2008 edition


The information contained in this document has not been submitted to any formal IBM test and is distributed on an as is basis without any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer responsibility and depends on the customers ability to evaluate and integrate them into the customers operational environment. While each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk. Copyright International Business Machines Corporation 2008. All rights reserved. This document may not be reproduced in whole or in part without the prior written permission of IBM. Note to U.S. Government Users Documentation related to restricted rights Use, duplication or disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.

V5.1
Student Notebook

TOC

Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Course description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi The ease of working with a relational database . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Data in a DBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiv DB2 is a Relational Database Management System . . . . . . . . . . . . . . . . . . . . . . . . xv Table creation English . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvi The language of Relational Database Management Systems . . . . . . . . . . . . . . . . xvii Creating tables in DB2 We must provide ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii From an Editor that talks to DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix A BIG picture look at running an application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How DB2 handles an SQL Change request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Finish the Change request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Part 1. DB2 Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 DB2 Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 DB2 components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5 CLP Command Line Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 CLP syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 Using the CLP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8 Roadmap to the GUI tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Control Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 Command Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Journal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Configuration Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Visual Explain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14 Developer Workbench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15 Memory Visualizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16 Storage Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17 IBM Data Warehouse Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19 Part 2. Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic client-server-host configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DB2 authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Authentication within the DBM configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DB2 authentication types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DB2 Authorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1 2-3 2-4 2-5 2-6 2-7 2-8 2-9

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Contents

iii

Student Notebook

DB2 Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-10 Explicit authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-11 LBAC (Label Based Access Control) query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-12 Example implementation of LBAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-13 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-14 Part 3. Accessing DB2 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-3 What makes up a DB2 database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-4 DB2 object hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-5 Default table space containers with Automatic Storage . . . . . . . . . . . . . . . . . . . . . .3-6 Creating your first database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-7 Control Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8 Create Database Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-9 CREATE DATABASE syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-10 CREATE DATABASE examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-11 Database Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-12 CREATE TABLESPACE syntax (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-13 CREATE TABLESPACE syntax (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-14 Cataloging your DB2 database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-15 Configuration Assistant (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-16 Configuration Assistant (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-17 Manipulating DB2 objects with the Control Center . . . . . . . . . . . . . . . . . . . . . . . . .3-18 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-19 Part 4. Working with DB2 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-3 The parts of speech of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-4 Types of SQL statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-5 Data Manipulation Language (DML) SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-6 Data Manipulation Language (DML) WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-7 Data Manipulation Language (DML) ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . .4-8 Data Manipulation Language (DML) Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-9 Data Manipulation Language (DML) Set operators . . . . . . . . . . . . . . . . . . . . . . .4-10 Data Manipulation Language (DML) GROUP BY . . . . . . . . . . . . . . . . . . . . . . . .4-11 Data Manipulation Language (DML) INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . .4-12 Data Manipulation Language (DML) UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . .4-13 Data Manipulation Language (DML) DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . .4-14 Data Manipulation Language (DML) MERGE . . . . . . . . . . . . . . . . . . . . . . . . . . .4-15 Data Manipulation Language (DML) Reference . . . . . . . . . . . . . . . . . . . . . . . . .4-16 Units of work and savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-17 Creating and calling an SQL procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-18 Creating and using SQL user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . .4-19 User-defined table function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-20 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-21

iv

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V5.1
Student Notebook

TOC

Part 5. Working with DB2 objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 Numeric data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 String data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Datetime data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 XML data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8 User-defined data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Create Trigger statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10 Create Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11 Set current schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12 DB2 Extenders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13 Tables CREATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14 Tables ALTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15 Tables DROP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Tables NOT NULL, DEFAULT, and GENERATED . . . . . . . . . . . . . . . . . . . . . . 5-17 Constraints Unique constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18 Constraints Referential integrity constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Constraints Table check constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22 Creating indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23 Clustering indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24 Using included columns in indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-26 Part 6. Data concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Simple workload consisting of three transactions . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 Comparing successful and unsuccessful transactions . . . . . . . . . . . . . . . . . . . . . . 6-5 Lock mode compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 Concurrency and isolation levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 Repeatable read isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Read stability isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9 Cursor stability isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10 Uncommitted read isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11 Lock attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12 Table lock modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13 Row lock modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14 Lock conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15 Lock escalation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16 Lock wait and timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17 Deadlock causes and detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Contents

Student Notebook

Part 7. Introducing XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-1 Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-2 Certification Exam (730) objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-3 Sample XML document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-4 XQuery navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-5 Sample data for the clients table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-6 Simple XML data retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-7 Specifying a single filtering predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-8 Converting XML to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-9 Conditional logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-10 Hybrid queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-11 Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12 Unit 8. Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-1 Review of units covered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-2 Part 1: DB2 planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-3 Part 2: Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4 Part 3: Accessing DB2 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-5 Part 4: Working with DB2 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6 Part 5: Working with DB2 objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-7 Part 6: Data concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-8 Part 7: Introducing XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9

vi

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V5.1
Student Notebook

TMK

Trademarks
The reader should recognize that the following terms, which appear in the content of this training document, are official trademarks of IBM or other companies: IBM is a registered trademark of International Business Machines Corporation. The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both: AIX DB2 Informix OS/390 z/OS AT Distributed Relational Database Architecture iSeries OS/400 Database 2 DRDA MVS Rational

Windows and Windows NT are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. Other company, product, or service names may be trademarks or service marks of others.

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Trademarks

vii

Student Notebook

viii

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V5.1
Student Notebook

pref

Course description
AI Certification Preparation for Exam 730 Duration: 1 day Purpose
This series of eight lectures are used in combination with the DB2 9 Fundamentals self-study tutorials. (See: http://www.ibm.com/developerworks/offers/lp/db2cert/db2-cert730. html) Both the self-study tutorials and lectures will help students prepare for DB2 Family Fundamentals Exam 730. This education series provides a solid base for each section of the exam.

Audience
Individuals who want to prepare for the DB2 9 Fundamentals Exam 730 or individuals needing an introductory knowledge of DB2.

Prerequisites
Before this course begins, the student should review the DB2 9 Fundamentals exam 730 prep tutorials listed here: http://www.ibm.com/developerworks/offers/lp/db2cert/db2-cert730. html Part 1: DB2 planning Part 2: Security Part 3: Accessing DB2 data Part 4: Working with DB2 data Part 5: Working with DB2 objects Part 6: Data concurrency Part 7: Introducing XQuery

During the course, the instructor will present slides coinciding with each Tutorial unit. These are short tutorials, averaging about 1 hour.

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Course description

ix

Student Notebook

Objectives
After completing this course, students should be able to: Write the DB2 Family Fundamentals, Exam 730 - Section 1 Planning (14%) - Section 2 Security (11%) - Section 3 Working with Databases and Database Objects (17%) - Section 4 Working with DB2 Data using SQL (23.5%) - Section 5 Working with DB2 Tables, Views and Indexes (23.5%) - Section 6 Data Concurrency (11%) (See: http://www-03.ibm.com/certify/certs/08003503.shtml) EXAM INFORMATION: The exam is made up of 64 questions. The breakdown of questions associated with each topic area is identified below. Each student is given 90 minutes to write the exam. Each student must achieve a score of 59% to pass the test. Section 1 Planning (14%) Section 2 Security (11%) Section 3 Working with Databases and Database Objects (17%) Section 4 Working with DB2 Data using SQL (23.5%) Section 5 Working with DB2 Tables, Views and Indexes (23.5%) Section 6 Data Concurrency (11%) (See: http://www-03.ibm.com/certify/certs/08003503.shtml)

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V5.1
Student Notebook

pref

Agenda
Day 1
Welcome Part 1: DB2 planning Part 2: Security Part 3: Accessing DB2 data Part 4: Working with DB2 data Part 5: Working with DB2 objects Part 6: Data concurrency Part 7: Introducing XQuery Summary

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Agenda

xi

Student Notebook

xii

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

The ease of working with a relational database

I need to call Larry Jones in the company directory... now, where is his phone number?

Phone book COMPANY DIRECTORY

Copyright IBM Corporation 2008

Figure 1. The ease of working with a relational database

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

-xiii

Student Notebook

Data in a DBMS

DB2, manage the company phone directory


Phone book COMPANY DIRECTORY

DB2
Integrity and Recovery

Tables - R - Us

Continuous Operation

DBA
Optimizer

Logging

Lock Manager

Security

Copyright IBM Corporation 2008

Figure 2. Data in a DBMS

CE5001.0

Notes:

-xiv AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

DB2 is a Relational Database Management System

"RELATION"
OWNER1.TABLE_A
COL1 ABC DEF GHI JKL MNO COL2 COL3 7 4 6 7 0 1987-11-14 1963-01-01 1946-07-12 1951-12-30 1992-08-12 "Relational Database" is a "Table Database"

TABLE
PROD.PARTS
PROD BOLT NUT SCREW BOARD WIRE QTY SUPPLIER 7 4 6 7 0 ABC CORP THE NUT CO FASTEN INC ECM CORP BINDINGS

(Data stored in columns and rows)

Copyright IBM Corporation 2008

Figure 3. DB2 is a Relational Database Management System

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

-xv

Student Notebook

Table creation English


DB2
Tables - R - Us

DB2, build me a table. Here are the rules to enforce on the table. Name the table COMPANY.DIRECTORY. Name the first column LASTNAME. Ensure that only character data be allowed in that column. Also, ensure that every phone listing has a value in the LASTNAME column. Now, for the second column...

DBA

Copyright IBM Corporation 2008

Figure 4. Table creation English

CE5001.0

Notes:

-xvi AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

The language of Relational Database Management Systems

Q Flexible
Notes:

TRUCTURED

WHAT to do, NOT how to do it

UERY

ANGUAGE

Copyright IBM Corporation 2008

Figure 5. The language of Relational Database Management Systems

CE5001.0

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

-xvii

Student Notebook

Creating tables in DB2 We must provide ...

Table name
Describe the columns (give each column 3 attributes) COLUMN NAME DATATYPE / LENGTH NULL CHARACTERISTIC

DBA DBA

Copyright IBM Corporation 2008

Figure 6. Creating tables in DB2 We must provide ...

CE5001.0

Notes:

-xviii AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

From an Editor that talks to DB2

CREATE TABLE COMPANY.DIRECTORY ( ID LASTNAME FIRSTNAME ADDRESS ~ INTEGER CHAR(15) CHAR(15) VARCHAR(45) ~ NOT NULL WITH DEFAULT, ~ NOT NULL , , ,

NOT NULL

Column description area


)

Copyright IBM Corporation 2008

Figure 7. From an Editor that talks to DB2

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

-xix

Student Notebook

A BIG picture look at running an application


DB2
Tables - R - Us Integrity & Recovery

Application Manager
Logging Optimizer

Continuous Operation

Lock Manager

Security

Application Application

Application

Application

Application Application

Application Programs

Data Tables

Copyright IBM Corporation 2008

Figure 8. A BIG picture look at running an application

CE5001.0

Notes:

-xx

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

How DB2 handles an SQL Change request


User wants to change a SALARY
User's application connects to DB2 Passes user's ID to DB2
Logging Optimizer Tables - R - Us

DB2
Integrity & Recovery

Lock

Continuous Operation

Lock Manager

Lock Manager

Security

Read into DB2 buffer

Data

Is user permitted to update salary?


Catalog

Copyright IBM Corporation 2008

Figure 9. How DB2 handles an SQL Change request

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

-xxi

Student Notebook

Finish the Change request


After the update
Tables - R - Us

DB2
Integrity & Recovery

Release the lock

Continuous Operation

Logging Optimizer

Write
Lock Manager Security

Data Write/Commit

Logging

LOG
Copyright IBM Corporation 2008

Figure 10. Finish the Change request

CE5001.0

Notes:

-xxii AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 1. DB2 Planning


What this unit is about
This unit describes: The different versions of DB2, and the various DB2 products The tools that are included with DB2 How to use the Control Center to manage systems, DB2 instances, databases, database objects, and more How the Configuration Assistant lets you maintain a list of databases to which your applications can connect, manage, and administer. All of the standalone tools in the Control Center and the Configuration Assistant What data warehousing is, and the DB2 products available to assist with data warehousing

What you should be able to do


After completing this unit, you should be able to: Identify the different versions of DB2, and the various DB2 products. List the tools that are included with DB2. Use the Control Center to manage systems, DB2 instances, databases, database objects, and more. Identify how the Configuration Assistant lets you maintain a list of databases to which your applications can connect, manage, and administer. Use all of the standalone tools in the Control Center and the Configuration Assistant. Identify what data warehousing is, and the DB2 products available to assist with data warehousing. Also note that throughout this tutorial, you will see commands and syntax that are generally used on either a Linux, Windows, or UNIX platform.

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-1

Student Notebook

Unit objectives
After completing this unit, you should understand:

Identify the different versions of DB2, and the various DB2


products.

List the tools that are included with DB2. Use the Control Center to manage systems, DB2 instances,
databases, database objects, and more.

Identify how the Configuration Assistant lets you maintain a


list of databases to which your applications can connect, manage, and administer.

Use all of the standalone tools in the Control Center and the
Configuration Assistant.

Identify what data warehousing is, and the DB2 products


available to assist with data warehousing.
Copyright IBM Corporation 2008

Figure 1-1. Unit objectives

CE5001.0

Notes:

1-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Knowledge of the features or functions available in DB2 tools
(just tools that come with product, that is, Control Center, Configuration Advisor, Configuration Assistant, Command Line Processor)

Knowledge of database workloads (OLTP versus


warehousing)

Copyright IBM Corporation 2008

Figure 1-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-3

Student Notebook

DB2 Editions
Massively Parallel Processor (MPP)

Cluster

DB2

Enterprise Server Edition with Data Partitioning Feature

Enterprise Server Edition Workgroup Server Edition Express Edition


Copyright IBM Corporation 2008

Figure 1-3. DB2 Editions

CE5001.0

Notes:

1-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

DB2 components
DRDA Application Requester

Data Server Client

Data Server Runtime Client

Communication Support DB2 DB2 Connect

DRDA Application Server


Copyright IBM Corporation 2008

Figure 1-4. DB2 components

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-5

Student Notebook

CLP Command Line Processor

Copyright IBM Corporation 2008

Figure 1-5. CLP Command Line Processor

CE5001.0

Notes:

1-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

CLP syntax

db2 option-flag db2-command sql-statement ? phrase message sql-state class-code

Copyright IBM Corporation 2008

Figure 1-6. CLP syntax

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-7

Student Notebook

Using the CLP


Non-interactive mode db2 CONNECT TO musicdb db2 SELECT * FROM syscat.tables | more

(double quotes may be required)


Interactive mode

db2 db2=> CONNECT TO musicdb db2=> SELECT * FROM syscat.tables


Copyright IBM Corporation 2008

Figure 1-7. Using the CLP

CE5001.0

Notes:

1-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Roadmap to the GUI tools


IBM DB2

Information

Command Line Tools

General Administration Tools Control Center (db2cc) Journal (db2journal) Replication Center (dbrc) Task Center (db2tc) License Center

Monitoring Tools

Setup Tools

Information Center (db2ic)

Command Editor (db2ce) Command Line Processor (db2) Command Window (db2cw)

Event Analyzer (db2eva) Health Center (db2hc) InDoubt Transaction Manager (db2indbt) Activity Monitor (db2am) Memory Visualizer (db2memvis)

Configuration Assistant (db2ca) First Steps (db2fs) Register Visual Studio Add-ins (db2vsregister) Satellite Synchronizer

Copyright IBM Corporation 2008

Figure 1-8. Roadmap to the GUI tools

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-9

Student Notebook

Control Center

Object Tree Pane

Contents Pane

Object Detail Pane

Copyright IBM Corporation 2008

Figure 1-9. Control Center

CE5001.0

Notes:

1-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Command Editor

SQL Assist

Copyright IBM Corporation 2008

Figure 1-10. Command Editor

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-11

Student Notebook

Journal

Copyright IBM Corporation 2008

Figure 1-11. Journal

CE5001.0

Notes:

1-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Configuration Assistant

Copyright IBM Corporation 2008

Figure 1-12. Configuration Assistant

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-13

Student Notebook

Visual Explain

Copyright IBM Corporation 2008

Figure 1-13. Visual Explain

CE5001.0

Notes:

1-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Developer Workbench

Copyright IBM Corporation 2008

Figure 1-14. Developer Workbench

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-15

Student Notebook

Memory Visualizer

Copyright IBM Corporation 2008

Figure 1-15. Memory Visualizer

CE5001.0

Notes:

1-16 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Storage Management

Copyright IBM Corporation 2008

Figure 1-16. Storage Management

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-17

Student Notebook

IBM Data Warehouse Edition


An integrated data warehouse for greater business insight Identify opportunities to pursue - before the competition does Identify risks to be minimized or eliminated - before they become public

Copyright IBM Corporation 2008

Figure 1-17. IBM Data Warehouse Edition

CE5001.0

Notes:

1-18 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit summary
Having completed this unit, you should understand:

Identify the different versions of DB2, and the various DB2


products.

List the tools that are included with DB2. Use the Control Center to manage systems, DB2 instances,
databases, database objects, and more.

Identify how the Configuration Assistant lets you maintain a


list of databases to which your applications can connect, manage, and administer.

Use all of the standalone tools in the Control Center and the
Configuration Assistant.

Identify what data warehousing is, and the DB2 products


available to assist with data warehousing.
Copyright IBM Corporation 2008

Figure 1-18. Unit summary

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 1. DB2 Planning

1-19

Student Notebook

1-20 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 2. Security
What this unit is about
This unit describes: The structure of the entire DB2 environment, which includes client, servers, gateways, and hosts Authentication, authorization, and privileges How to use the db2 catalog database command on the gateway and client The basics of the SYSADM, SYSCTRL, SYSMAINT, and SYSMON authorities, which are set in the DBM CFG file The basics of the DBADM, LOAD, and SECADM authorities, which are set using the GRANT command and revoked using the REVOKE command What command each authority is allowed to run The different types of privileges and what they allow a user to do How a privilege is obtained/revoked explicitly, implicitly, or (for packages only) indirectly Basics of Label-Based Access Control, and how to define different types of policies based on this new security concept

What you should be able to do


After completing this unit, you should be able to: Identify the structure of the entire DB2 environment, which includes client, servers, gateways, and hosts Work with authentication, authorization, and privileges. Use the db2 catalog database command on the gateway and client List the basics of the SYSADM, SYSCTRL, SYSMAINT, and SYSMON authorities, which are set in the DBM CFG file List the basics of the DBADM, LOAD, and SECADM authorities, which are set using the GRANT command and revoked using the REVOKE command Identify what command each authority is allowed to run

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-1

Student Notebook

Identify the different types of privileges and what they allow a user to do Identify how a privilege is obtained/revoked explicitly, implicitly, or (for packages only) indirectly Identify the basics of Label-Based Access Control, and how to define different types of policies based on this new security concept

2-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit objectives
After completing this unit, you should be able to: Identify the structure of the entire DB2 environment, which includes client, servers, gateways, and hosts. Use the db2 catalog command on the gateway and client. Use the basics of the SYSADM, SYSCTRL, SYSMAINT, and SYSMON authorities, which are set in the DBM CFG file. List the basics of the DBADM, LOAD, and SECADM authorities, which are set using the GRANT command and revoked using the REVOKE command. Identify what command each authority is allowed to run. Identify the different types of privileges and what they allow a user to do. Identify how a privilege is obtained/revoked explicitly, implicitly, or (for packages only) indirectly. Identify the basics of Label-Based Access Control, and how to define different types of policies based on this new security concept.
Copyright IBM Corporation 2008

Figure 2-1. Unit objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-3

Student Notebook

Certification Exam (730) objectives Knowledge of restricting data access Ability to identify and connect to DB2 servers and databases Knowledge of DB2 products (client, server, etc.) Knowledge of different privileges and authorities Knowledge of encryption options (data and network) Given a DDL SQL statement, knowledge to identify results
(grant/revoke/connect statements)

Copyright IBM Corporation 2008

Figure 2-2. Certification Exam (730) objectives

CE5001.0

Notes:

2-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Basic client-server-host configuration


DB2 clients DB2 on the host

Linux

DB2 server or DB2 Connect Server (Gateway)

Windows

AIX

Copyright IBM Corporation 2008

Figure 2-3. Basic client-server-host configuration

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-5

Student Notebook

DB2 authentication Implicit security checking:


ATTACH TO DB2

Explicit security checking:


CONNECT TO sample USER test1 USING <password>
Database Connection Information Database server SQL authorization ID Local database alias = DB2/NT 9.1.0 = TEST1 = SAMPLE

Copyright IBM Corporation 2008

Figure 2-4. DB2 authentication

CE5001.0

Notes:

2-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Authentication within the DBM configuration


GET DBM CFG
Server Connection Authentication (SRVCON_AUTH) = KERBEROS Database manager authentication (AUTHENTICATION) = SERVER_ENCRYPT

Copyright IBM Corporation 2008

Figure 2-5. Authentication within the DBM configuration

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-7

Student Notebook

DB2 authentication types


SERVER CLIENT *KERBEROS DATA_ENCRYPT GSSPLUGIN *KRB_SERVER_ENCRYPT DATA_ENCRYPT_CMP GSS_SERVER_ENCRYPT SERVER_ENCRYPT

Copyright IBM Corporation 2008

Figure 2-6. DB2 authentication types

CE5001.0

Notes:

2-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

DB2 Authorities
Function
MIGRATE DATABASE UPDATE DBM CFG GRANT/REVOKE DBADM UPDATE db/node/dcs directories FORCE USERS OFF SYSTEM CREATE/DROP DATABASE CREATE/DROP/ALTER TABLE SPACE RESTORE TO NEW DATABASE UPDATE DB CFG BACKUP DATABASE or TABLE SPACE RESTORE TO EXISTING DATABASE PERFORM ROLLFORWARD RECOVERY START/STOP DATABASE INSTANCE RESTORE TABLE SPACE RUN TRACE TAKE DBM or DB SNAPSHOTS QUERY TABLE SPACE STATE UPDATE LOG HISTORY FILES QUIESCE TABLE SPACE REORG TABLE RUN RUNSTATS UTILITY READ LOG FILES CREATE/ACTIVATE/DROP EVENT MONITORS

SYSADM
YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES

SYSCTRL

SYSMAINT

DBADM

LOAD

YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES

YES YES YES YES YES YES YES YES YES YES YES YES YES

YES YES YES YES YES YES YES

YES YES YES

Copyright IBM Corporation 2008

Figure 2-7. DB2 Authorities

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-9

Student Notebook

DB2 Privileges
SYSADM SYSCTRL SYSMAINT SECADM DBADM SYSMON

Authorities Privileges

........................................
Schema Owner CONTROL (Tables) CONTROL (Views) CONTROL (Packages) EXECUTE (Routines)

LOAD

BINDADD CONNECT CREATETAB CREATE_EXTERNAL_ROUTINE CREATE_NOT_FENCED_ROUTINE IMPLICIT_SCHEMA LOAD QUIESCE_CONNECT (Database)

Table space Owner

ALL ALTER CREATEIN DELETE ALTERIN INDEX DROPIN INSERT REFERENCES SELECT USE UPDATE
Copyright IBM Corporation 2008

CONTROL (Indexes) ALL DELETE INSERT SELECT UPDATE BIND EXECUTE

Figure 2-8. DB2 Privileges

CE5001.0

Notes:

2-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Explicit authorization

Database privileges Package privileges * GRANT/ Table/view REVOKE privileges CONTROL Schema privileges USE

ON DATABASE ON PACKAGE package_name userid ON TABLE USER/ TO/ table/view_name FROM GROUP groupid PUBLIC ON INDEX index_name ON SCHEMA schema_name OF TABLESPACE tablespacename

* must be SYSADM, DBADM, or have CONTROL on object


Copyright IBM Corporation 2008

Figure 2-9. Explicit authorization

CE5001.0

Notes:
Privileges and DBADM authority may be explicitly GRANTed to users or PUBLIC by someone who holds a higher authority or privilege (SYSADM, DBADM, or CONTROL). When granting database privileges, the name of the database is not specified, because you must be CONNECTed (using) a specific database in order to execute SQL statements. Therefore, DB2 knows to which database you are connected.

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-11

Student Notebook

LBAC (Label Based Access Control) query


SELECT * FROM EMP WHERE SALARY >= 50000 User Level = 100
Users with user level 100 can view the rows with ID <= 100 and salary >= 50000 (indicated in green) With no LBAC user level imposed, users can view rows that meet the salary >= 50000 qualifier (shown in red)

No LBAC LBAC

ID 255 100 50 50 60 250 102 100 75 253 90 200 105

SALARY 60000 50000 70000 45000 30000 56000 82000 54000 33000 46000 83000 78000 45000

Copyright IBM Corporation 2008

Figure 2-10. LBAC (Label Based Access Control) query

CE5001.0

Notes:

2-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Example implementation of LBAC Steps overview:


1. Define the security policies and labels
a. Define the security label component b. Define the security policy c. Define the security labels

2. Create the protected SALES table by including a column that holds the security label and attaching the security policy to the table. 3. Grant the appropriate security labels to users.

Requires SECADM authority to execute commands for


creating security policies and labels.
Copyright IBM Corporation 2008

Figure 2-11. Example implementation of LBAC

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Part 2. Security

2-13

Student Notebook

Unit summary
Having completed this unit, you should be able to: Identify the structure of the entire DB2 environment, which includes client, servers, gateways, and hosts. Use the db2 catalog command on the gateway and client. Use the basics of the SYSADM, SYSCTRL, SYSMAINT, and SYSMON authorities, which are set in the DBM CFG file. List the basics of the DBADM, LOAD, and SECADM authorities, which are set using the GRANT command and revoked using the REVOKE command. Identify what command each authority is allowed to run. Identify the different types of privileges and what they allow a user to do. Identify how a privilege is obtained/revoked explicitly, implicitly, or (for packages only) indirectly. Identify the basics of Label-Based Access Control, and how to define different types of policies based on this new security concept.
Copyright IBM Corporation 2008

Figure 2-12. Unit summary

CE5001.0

Notes:

2-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 3. Accessing DB2 data


What this unit is about
This unit describes: Creating a DB2 database on your own Cataloging it for use by other users Examining and manipulating the objects within that database

What you should be able to do


After completing this unit, you should understand how to: Create a DB2 database on your own Catalog it for use by other users Examine and manipulate the objects within that database.

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-1

Student Notebook

Unit objectives
After completing this unit, you should understand how to:

Create a DB2 database on your own Catalog it for use by other users Examine and manipulate the objects within that database

Copyright IBM Corporation 2008

Figure 3-1. Unit objectives

CE5001.0

Notes:

3-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Ability to identify DB2 objects Knowledge of basic characteristics and properties of DB2
objects

Given a DDL SQL statement, knowledge to identify results


(ability to create objects)

Copyright IBM Corporation 2008

Figure 3-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-3

Student Notebook

What makes up a DB2 database? A DB2 database is made up of a collection of objects The database contains many of the following physical and
logical objects:
Tables, views, indexes, schemas Locks, triggers, stored procedures, packages Buffer pools, log files, table spaces

Copyright IBM Corporation 2008

Figure 3-3. What makes up a DB2 database?

CE5001.0

Notes:

3-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

DB2 object hierarchy


Instance 1
SYSCATSPACE

dbm configuration file


SYSCATSPACE

Catalog Log DB configuration file


TSSMS1 TSDMSLRG1

Catalog Log Database 2 View1

Database 1

View1 View2

DB configuration file
USERSPACE1

Table1 Index1
TSDMSLRG3

Table2

Table3
TSDMSREG2

Table2

Index1 BLOBs Index2

Copyright IBM Corporation 2008

Figure 3-4. DB2 object hierarchy

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-5

Student Notebook

Default table space containers with Automatic Storage


CREATE DATABASE DSS ON /dbauto DBPATH ON /database

dbauto
inst20 NODEnnnn

DB2INSTANCE=inst20

DSS T0000000
C0000000.CAT

SYSCATSPACE (Catalog Partition Only) TEMPSPACE1


C0000000.TMP

T0000001 T0000002
C0000000.LRG
Copyright IBM Corporation 2008

USERSPACE1

Figure 3-5. Default table space containers with Automatic Storage

CE5001.0

Notes:

3-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Creating your first database

Copyright IBM Corporation 2008

Figure 3-6. Creating your first database

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-7

Student Notebook

Control Center

Copyright IBM Corporation 2008

Figure 3-7. Control Center

CE5001.0

Notes:

3-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Create Database Wizard

Copyright IBM Corporation 2008

Figure 3-8. Create Database Wizard

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-9

Student Notebook

CREATE DATABASE syntax


CREATE DATABASE DB database-name AT DBPARTIONNUM |Create Database Options|

Create Database options: AUTOMATIC STORAGE YES NO ON , path drive DBPATH ON TERRITORY territory DFT_EXTENT_SZ dft-extentsize path drive

ALIAS

db-alias

USING CODESET

codeset

COLLATE USING

SYSTEM IDENTITY

PAGESIZE

4096 n K

RESTRICTIVE USER TABLESPACE |tblspace-defn| WITH tblspace-defn: | MANAGED BY SYSTEM USING ( DATABASE USING ( EXTENTSIZE OVERHEAD num-pages number-of-milliseconds "comment-string"

CATALOG TABLESPACE |tblspace-defn| TEMPORARY TABLESPACE |tblspace-defn|

|autoconfigure-settings| , ) 'container-string' , FILE 'container-string' DEVICE PREFETCHSIZE TRANSFERRATE num-pages number-of-milliseconds

num-pages

Copyright IBM Corporation 2008

Figure 3-9. CREATE DATABASE syntax

CE5001.0

Notes:

3-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

CREATE DATABASE examples


CREATE DATABASE sales1 ON /dbsales1 Database Path: /dbsales1 Automatic Storage Path: /dbsales1 CREATE DATABASE sales2 AUTOMATIC STORAGE no ON /dbsales2 Database Path: /dbsales2 Automatic Storage not enabled CREATE DATABASE sales3 ON /dbauto3 DBPATH ON /dbsales3 Database Path: /dbsales3 Automatic Storage Path: /dbauto3 CREATE DATABASE sales4 AUTOMATIC STORAGE yes ON /dbauto41,/dbauto42,/dbauto43 DBPATH ON /dbsales4 Database Path: /dbsales4 Automatic Storage Paths: /dbauto41, /dbauto42 and /dbauto43
Copyright IBM Corporation 2008

Figure 3-10. CREATE DATABASE examples

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-11

Student Notebook

Database Configuration

Copyright IBM Corporation 2008

Figure 3-11. Database Configuration

CE5001.0

Notes:

3-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

CREATE TABLESPACE syntax (1 of 2)


REGULAR CREATE LARGE SYSTEM USER DATABASE PARTITION GROUP IN db-partition-group-name AUTOMATIC STORAGE | size-attributes | SYSTEM | system-containers | DATABASE |database-containers | EXTENTSIZE TEMPORARY PAGESIZE 4096 PAGESIZE integer K TABLESPACE tablespace-name

MANAGED BY

num-pages integer K M G

PREFETCHSIZE

AUTOMATIC num-pages integer

BUFFERPOOL K M G

bufferpool-name

OVERHEAD

7.5 number-of-milliseconds 0.06 number-of-milliseconds

FILE SYSTEM CACHING NO FILE SYSTEM CACHING DROPPED TABLE RECOVERY ON OFF

TRANSFERRATE

Copyright IBM Corporation 2008

Figure 3-12. CREATE TABLESPACE syntax (1 of 2)

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-13

Student Notebook

CREATE TABLESPACE syntax (2 of 2)


size-attributes:
AUTORESIZE YES NO INITIALSIZE int K M G INCREASESIZE int perc K M G MAXSIZE int K M G NONE

system-containers:

,
USING ( 'container-string' ) | on-db-partitions-clause |

database-containers:
USING | container-clause | | on-db-partitions-clause |

container-clause:

,
( FILE DEVICE 'container-string' num-pages integer K M G )

on-db-partitions-clause:

,
ON DBPARTITIONNUM DBPARTITIONNUMS
Copyright IBM Corporation 2008

db-partition-number1 TO db-partition-number2

Figure 3-13. CREATE TABLESPACE syntax (2 of 2)

CE5001.0

Notes:

3-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Cataloging your DB2 database


CATALOG DATABASE DB database-name AS -- alias

path drive AT NODE -- nodename ON

AUTHENTICATION

SERVER CLIENT SERVER_ENCRYPT KERBEROS TARGET PRINCIPAL--principalname DATA_ENCRYPT GSSPLUGIN

WITH -- "comment-string"

Copyright IBM Corporation 2008

Figure 3-14. Cataloging your DB2 database

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-15

Student Notebook

Configuration Assistant (1 of 2)

Copyright IBM Corporation 2008

Figure 3-15. Configuration Assistant (1 of 2)

CE5001.0

Notes:

3-16 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Configuration Assistant (2 of 2)

Copyright IBM Corporation 2008

Figure 3-16. Configuration Assistant (2 of 2)

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-17

Student Notebook

Manipulating DB2 objects with the Control Center


Menu bar Control bar

Contents pane

Objects pane

Contents pane Tool bar

Details

Copyright IBM Corporation 2008

Figure 3-17. Manipulating DB2 objects with the Control Center

CE5001.0

Notes:

3-18 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit summary
Having completed this unit, you should understand how to:

Create a DB2 database on your own Catalog it for use by other users Examine and manipulate the objects within that database

Copyright IBM Corporation 2008

Figure 3-18. Unit summary

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 3. Accessing DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

3-19

Student Notebook

3-20 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 4. Working with DB2 data


What this unit is about
This unit describes: Understanding the fundamentals of SQL, with a focus on SQL language elements Using DML to select, insert, update, or delete data Using COMMIT and ROLLBACK statements to manage transactions, and know what constitutes a transaction boundary Creating and call SQL procedures or invoke user-defined functions from the command line

What you should be able to do


After completing this unit, you should be able to: Identify the fundamentals of SQL, with a focus on SQL language elements Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage transactions, and know what constitutes a transaction boundary Create and call SQL procedures or invoke user-defined functions from the command line

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

Identify the fundamentals of SQL, with a focus on SQL


language elements

Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage
transactions, and know what constitutes a transaction boundary

Create and call SQL procedures or invoke user-defined


functions from the command line

Copyright IBM Corporation 2008

Figure 4-1. Unit objectives

CE5001.0

Notes:

4-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Given a DML SQL statement, knowledge to identify results Ability to use SQL to SELECT data from tables Ability to use SQL to SORT or GROUP data Ability to use SQL to UPDATE, DELETE, or INSERT data Knowledge of transactions (that is, commit/rollback and
transaction boundaries)

Ability to call a procedure or invoke a user-defined function

Copyright IBM Corporation 2008

Figure 4-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-3

Student Notebook

The parts of speech of SQL SQL is a language that is used to define and manipulate
database objects.

There are several types of SQL:


DDL: Data Definition Language DML: Data Manipulation Language DCL: Data Control Language Use SQL to:
Define a database table (DDL) Insert data into the table (DML) Change the data in the table (DML) Retrieve data from the table (DML) Change security on database objects (DCL)

In this unit, we will concentrate on DML.


Copyright IBM Corporation 2008

Figure 4-3. The parts of speech of SQL

CE5001.0

Notes:

4-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Types of SQL statements


Most SQL statements contain one or more of the following language
elements:
Single-byte characters can be a letter, a digit (0-9), or a special character. A token is a sequence of one or more characters. An SQL identifier is a token that is used to form a name. The data type of a value determines how DB2 interprets that value. A constant specifies a value. A special register is a storage area that is defined for an application process by the database manager and is used to store information that can be referenced in SQL statements. A routine can be a function, a method, or a procedure.
A function is a relationship between one or more input data values and one or more result values. A method is also a relationship between a set of input data values and a set of result values. However, database methods are defined, either implicitly or explicitly, as part of the definition of a user-defined structured type. A procedure is an application program that can be started by executing a CALL statement.

An expression specifies a value. A predicate specifies a condition that is true, false, or unknown about a given row or group. Copyright IBM Corporation 2008
Figure 4-4. Types of SQL statements CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-5

Student Notebook

Data Manipulation Language (DML) SELECT The SELECT statement is used to retrieve table or view data.
In its simplest form, the SELECT statement can be used to retrieve all the data in a table.
"SELECT * FROM staff"; SELECT * FROM staff FETCH FIRST 10 ROWS ONLY; SELECT name, salary FROM staff; SELECT DISTINCT dept, job FROM staff; SELECT name, salary + comm AS pay FROM staff;

Copyright IBM Corporation 2008

Figure 4-5. Data Manipulation Language (DML) SELECT

CE5001.0

Notes:

4-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Data Manipulation Language (DML) WHERE Use the WHERE clause to select specific rows from a table or
view by specifying one or more selection criteria, or search conditions.
"SELECT name, salary FROM staff WHERE salary > 20000"; "SELECT name, job, salary FROM staff WHERE job <> 'Mgr AND name LIKE 'S%' AND salary > 20000";

Subquery
"SELECT lastname FROM employee WHERE lastname IN (SELECT sales_person FROM sales WHERE sales_date < '01/01/1996')"; "SELECT e.salary FROM employee e WHERE e.salary < (SELECT AVG(s.salary) FROM staff s)";
Copyright IBM Corporation 2008

Figure 4-6. Data Manipulation Language (DML) WHERE

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-7

Student Notebook

Data Manipulation Language (DML) ORDER BY Use the ORDER BY clause to sort the result set by values in
one or more columns.
"SELECT name, salary FROM staff WHERE salary > 20000 ORDER BY salary";

Copyright IBM Corporation 2008

Figure 4-7. Data Manipulation Language (DML) ORDER BY

CE5001.0

Notes:

4-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Data Manipulation Language (DML) Joins A join is a query that combines data from two or more tables.
SELECT deptnumb, deptname, manager, id, name, dept, job FROM org, staff; SELECT deptnumb, deptname, id AS manager_id, name AS manager FROM org, staff WHERE manager = id ORDER BY deptnumb; Inner joins return only rows from the cross product that meet the join condition. Outer joins return rows that are generated by an inner join operation, plus rows that would not be returned by the inner join operation.
Left outer join Right outer join Full outer join
Copyright IBM Corporation 2008

Figure 4-8. Data Manipulation Language (DML) Joins

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-9

Student Notebook

Data Manipulation Language (DML) Set operators Use set operators to combine two or more queries into a single
query
The UNION set operator generates a result table by combining two or more other result tables. The EXCEPT set operator generates a result table by including all rows that are returned by the first query, but not by the second or any subsequent queries. The INTERSECT set operator generates a result table by including only rows that are returned by all the queries. "SELECT sales_person FROM sales WHERE region = 'Ontario-South' UNION SELECT sales_person FROM sales WHERE sales > 3";

Copyright IBM Corporation 2008

Figure 4-9. Data Manipulation Language (DML) Set operators

CE5001.0

Notes:

4-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Data Manipulation Language (DML) GROUP BY Use the GROUP BY clause to organize rows in a result set.
SELECT sales_date, MAX(sales) AS max_sales FROM sales GROUP BY sales_date;

Grouping sets can be used to analyze data at different levels of


aggregation in a single pass (not available in DB2 z/OS).
SELECT YEAR(sales_date) AS year, region, SUM(sales) AS tot_sales FROM sales GROUP BY GROUPING SETS (YEAR(sales_date), region, () );

The HAVING clause is often used with a GROUP BY clause to


retrieve results for groups that satisfy only a specific condition.
SELECT sales_person, SUM(sales) AS total_sales FROM sales GROUP BY sales_person HAVING SUM(sales) >IBM25; 2008 Copyright Corporation
Figure 4-10. Data Manipulation Language (DML) GROUP BY CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-11

Student Notebook

Data Manipulation Language (DML) INSERT The INSERT statement is used to add new rows to a table or a
view.
INSERT INTO staff VALUES (1212,'Cerny',20,'Sales',3,90000.00,30000.00); INSERT INTO staff VALUES (1213,'Wolfrum',20,'Sales',2,90000.00,10000.00); Equivalent (not available on DB2 z/OS): INSERT INTO staff (id, name, dept, job, years, salary, comm) VALUES (1212,'Cerny',20,'Sales',3,90000.00,30000.00), (1213,'Wolfrum',20,'Sales',2,90000.00,10000.00);

A fullselect is a statement that generates a result table.


CREATE TABLE pers LIKE staff; INSERT INTO pers SELECT id, name, dept, job, years, salary, comm FROM staff WHERE dept = 38;
Copyright IBM Corporation 2008

Figure 4-11. Data Manipulation Language (DML) INSERT

CE5001.0

Notes:

4-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Data Manipulation Language (DML) UPDATE The UPDATE statement is used to change the data in a table
or a view.
UPDATE staff SET dept = 51, salary = 70000 WHERE id = 750;

Equivalent (not available on DB2 z/OS):


UPDATE staff SET (dept, salary) = (51, 70000) WHERE id = 750;

If you don't specify a WHERE clause, DB2 updates each row


in the table or view!

Copyright IBM Corporation 2008

Figure 4-12. Data Manipulation Language (DML) UPDATE

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-13

Student Notebook

Data Manipulation Language (DML) DELETE The DELETE statement is used to delete entire rows of data
from a table.
DELETE FROM staff WHERE id IN (1212, 1213);

If you don't specify a WHERE clause, DB2 deletes all the rows
in the table or view!

Copyright IBM Corporation 2008

Figure 4-13. Data Manipulation Language (DML) DELETE

CE5001.0

Notes:

4-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Data Manipulation Language (DML) MERGE


Use the MERGE statement to combine conditional update, insert, or delete
operations
MERGE INTO employee AS e USING (SELECT empno, firstnme, midinit, lastname, workdept, phoneno, hiredate, job, edlevel, sex, birthdate, salary FROM my_emp) AS m ON e.empno = m.empno WHEN MATCHED THEN UPDATE SET (salary) = (m.salary) WHEN NOT MATCHED THEN INSERT (empno, firstnme, midinit, lastname, workdept, phoneno, hiredate, job, edlevel, sex, birthdate, salary) VALUES (m.empno, m.firstnme, m.midinit, m.lastname, m.workdept, m.phoneno, m.hiredate, m.job, m.edlevel, m.sex, m.birthdate, m.salary);

Correlation names have been assigned to both the source and the target
table to avoid ambiguous table references in the search condition.
Copyright IBM Corporation 2008

Figure 4-14. Data Manipulation Language (DML) MERGE

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-15

Student Notebook

Data Manipulation Language (DML) Reference Use the data-change-table-reference clause to retrieve
intermediate result sets in the same unit of work
SELECT salary FROM OLD TABLE ( UPDATE employee SET salary = salary * 1.07 WHERE empno = '000220'); SALARY ----------29840.00 1 record(s) selected.

Copyright IBM Corporation 2008

Figure 4-15. Data Manipulation Language (DML) Reference

CE5001.0

Notes:

4-16 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Units of work and savepoints A unit of work (UOW), also known as a transaction:
Is a recoverable sequence of operations within an application process. Starts implicitly when the first SQL statement within an application process is issued against the database.
All subsequent reads and writes by the same application process are considered part of the same UOW.

The application ends the UOW by issuing either a COMMIT or


a ROLLBACK statement

A savepoint lets you selectively roll back a subset of actions


that make up a UOW without losing the entire transaction.
You can nest savepoints and have several savepoint levels active at the same time; this allows your application to roll back to a specific savepoint, as necessary.
Copyright IBM Corporation 2008

Figure 4-16. Units of work and savepoints

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-17

Student Notebook

Creating and calling an SQL procedure An SQL procedure is:


A stored procedure whose body is written in SQL. The body contains the logic of the SQL procedure. Created when you successfully invoke a CREATE PROCEDURE (SQL) statement
CREATE PROCEDURE sales_status (IN quota INTEGER, OUT sql_state CHAR(5)) DYNAMIC RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE SQLSTATE CHAR(5); DECLARE rs CURSOR WITH RETURN FOR SELECT sales_person, SUM(sales) AS total_sales FROM sales GROUP BY sales_person HAVING SUM(sales) > quota; OPEN rs; SET sql_state = SQLSTATE; END @
Copyright IBM Corporation 2008

Figure 4-17. Creating and calling an SQL procedure

CE5001.0

Notes:

4-18 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Creating and using SQL user-defined functions Create user-defined functions to extend the set of built-in DB2
functions.
Suppose that you needed a function that returns the area of a circle when the radius of that circle is specified as an argument to the function.
CREATE FUNCTION ca (r DOUBLE) RETURNS DOUBLE LANGUAGE SQL CONTAINS SQL NO EXTERNAL ACTION DETERMINISTIC RETURN 3.14159 * (r * r);

Example output:
db2 SELECT ca(96.8) AS area FROM sysibm.sysdummy1 AREA -----------------------+2.94374522816000E+004 1 record(s) selected.
Copyright IBM Corporation 2008

Figure 4-18. Creating and using SQL user-defined functions

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-19

Student Notebook

User-defined table function You can also create a user-defined table function, which takes
zero or more input arguments and returns data as a table.
A table function can only be used in the FROM clause of an SQL statement.

A function that returns the names and employee numbers of all


employees that hold a specific job:
CREATE FUNCTION jobemployees (job VARCHAR(8)) RETURNS TABLE ( empno CHAR(6), firstname VARCHAR(12), lastname VARCHAR(15)) LANGUAGE SQL READS SQL DATA NO EXTERNAL ACTION DETERMINISTIC RETURN SELECT empno, firstnme, lastname FROM employee WHERE employee.job = jobemployees.job;
Copyright IBM Corporation 2008

Figure 4-19. User-defined table function

CE5001.0

Notes:

4-20 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit summary
Having completed this unit, you should be able to:

Identify the fundamentals of SQL, with a focus on SQL


language elements

Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage
transactions, and know what constitutes a transaction boundary

Create and call SQL procedures or invoke user-defined


functions from the command line

Copyright IBM Corporation 2008

Figure 4-20. Unit summary

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 4. Working with DB2 data


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

4-21

Student Notebook

4-22 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 5. Working with DB2 objects


What this unit is about
This unit describes: Understanding data types and advanced data types Creating tables, views, and indexes in a DB2 database Understanding the features and use of unique constraints, referential integrity constraints, and table check constraints Using views to restrict access to data Understanding the features of indexes

What you should be able to do


After completing this unit, you should be able to: List data types and advanced data types Create tables, views, and indexes in a DB2 database Identify the features and use of unique constraints, referential integrity constraints, and table check constraints Use views to restrict access to data List the features of indexes

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

List data types and advanced data types Create tables, views, and indexes in a DB2 database Identify the features and use of unique constraints, referential
integrity constraints, and table check constraints

Use views to restrict access to data List the features of indexes

Copyright IBM Corporation 2008

Figure 5-1. Unit objectives

CE5001.0

Notes:

5-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Ability to demonstrate usage of DB2 data types Given a situation, ability to create table Knowledge to identify when referential integrity should be used Knowledge to identify methods of data constraint Knowledge to identify characteristics of a table, view or index Knowledge to identify when triggers should be used Knowledge of schemata Knowledge of data type options for storing XML data Knowledge of non-relational data concepts (extenders)
Copyright IBM Corporation 2008

Figure 5-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-3

Student Notebook

Data types DB2 comes with basic data types such as INTEGER, CHAR,
and DATE:
It also includes facilities to create user-defined data types (UDTs)

There are four categories of built-in data types:


Numeric, string, datetime, and XML

The user-defined data types are categorized as:


Distinct, structured, and reference

Copyright IBM Corporation 2008

Figure 5-3. Data types

CE5001.0

Notes:

5-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Numeric data types Three categories of numeric data:


Integer: Used to store integer numbers.
SMALLINT Store integers from -32,768 to 32,767 in 2 bytes. INTEGER Store integers from -2,147,483,648 to 2,147,483,647 in 4 bytes. BIGINT Store integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 in 8 bytes.

Decimal: Store numbers with fractional parts


DECIMAL

Floating point: Store approximations of numbers


REAL Defined with a length between 1 and 24 digits and requires 4 bytes of storage. DOUBLE Defined with a length of between 25 and 53 digits and requires 8 bytes of storage.

Copyright IBM Corporation 2008

Figure 5-4. Numeric data types

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-5

Student Notebook

String data types


DB2 provides several data types for storing character data or strings Single-byte character strings:
CHAR or CHARACTER Store fixed-length character strings up to 254 bytes. VARCHAR Store variable-length character strings.
The maximum length of a VARCHAR column is 32,672 bytes

Double-byte character strings:


GRAPHIC Store fixed-length double-byte character strings.
The maximum length of a GRAPHIC column is 127 characters

VARGRAPHIC Store variable-length double-byte character strings.


The maximum length of a VARGRAPHIC column is 16,336 characters

Long data types can be defined up to 2 GB in length


LONG VARCHAR CLOB (character large object) LONG VARGRAPHIC DBCLOB (double-byte character large object) BLOB (binary large object)
Copyright IBM Corporation 2008

Figure 5-5. String data types

CE5001.0

Notes:

5-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Datetime data types DB2 provides three data types to store dates and times:
DATE TIME
Several formats available: ISO, USA, EUR, and JIS.

TIMESTAMP
Single format: yyyy-mm-dd-hh.mm.ss.nnnnnn.

Applications can manipulate them as strings. Enclose the value in quotation marks when updating these data types.

Copyright IBM Corporation 2008

Figure 5-6. Datetime data types

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-7

Student Notebook

XML data type Used to store well-formed XML documents:


Stored in an internal representation different from string data types. To store XML data in an XML data type column, transform the data using the XMLPARSE function. An XML data type value can be transformed into a serialized string value representation of the XML document using the XMLSERIALIZE function.

DB2 provides many other built-in functions to manipulate XML


data types.

Copyright IBM Corporation 2008

Figure 5-7. XML data type

CE5001.0

Notes:

5-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

User-defined data types There are three user-defined data types:


User-defined distinct:
Define a new data type based on a built-in type:
Has the same features of the built-in type Can use it to ensure that only values of the same type are compared

User-defined structured:
Create a type that consists of several columns of built-in types
Use this structured type when creating a table

User-defined reference:
When using structured types, you can define references to rows in another table using reference types:
These references appear similar to referential constraints; however, they do not enforce relationships between the tables References in tables let you specify queries in a different way

Copyright IBM Corporation 2008

Figure 5-8. User-defined data types

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-9

Student Notebook

Create Trigger statement

CREATE TRIGGER reorder AFTER UPDATE OF qty ON stock REFERENCING NEW AS n FOR EACH ROW MODE DB2SQL WHEN (n.qty <=5) INSERT INTO reorder VALUES (n.itemno, current timestamp);

Copyright IBM Corporation 2008

Figure 5-9. Create Trigger statement

CE5001.0

Notes:

5-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Create Schema

CREATE SCHEMA payroll AUTHORIZATION db2admin; COMMENT ON SCHEMA payroll IS 'schema for payroll application';
Copyright IBM Corporation 2008

Figure 5-10. Create Schema

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-11

Student Notebook

Set current schema

CONNECT TO musicdb USER Keith; "SELECT * FROM employee"; Will select from KEITH.EMPLOYEE

SET CURRENT SCHEMA = 'PAYROLL'; SELECT * FROM employee; Will select from PAYROLL.EMPLOYEE

Copyright IBM Corporation 2008

Figure 5-11. Set current schema

CE5001.0

Notes:

5-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

DB2 Extenders DB2 Extenders provide support for complex, nontraditional


data types.
Packaged separately from the DB2 server code and installed on the server and into each database that uses the data type.

DB2 Extenders are implemented using the features of


user-defined types and user-defined functions (UDFs).
Each extender comes with one or more UDTs, UDFs for operating on the UDT, specific application programming interfaces (APIs), and perhaps other tools.

For example, the DB2 Image Extender includes:


The DB2IMAGE UDT UDFs to insert and retrieve from a DB2IMAGE column APIs to search based on characteristics of images

Before using these data types, install the extender support into
the database.
Copyright IBM Corporation 2008

Figure 5-12. DB2 Extenders

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-13

Student Notebook

Tables CREATE All data is stored in tables in the database. A table consists of one or more columns of various data types. The data is stored in rows or records.
CREATE TABLE artists (artno smallint NOT NULL, name VARCHAR(50) WITH DEFAULT 'abc', classification CHAR(1) NOT NULL, bio CLOB(100K) LOGGED, picture BLOB( 10M) NOT LOGGED COMPACT) IN dms01;

Copyright IBM Corporation 2008

Figure 5-13. Tables CREATE

CE5001.0

Notes:

5-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Tables ALTER Use the ALTER TABLE SQL statement to change


characteristics of a table.
For instance, you can add or drop:
A column A primary key One or more unique or referential constraints One or more check constraints

The following statement adds a column called BOOKTYPE to


the BOOKS table:
ALTER TABLE books ADD booktype CHAR(1);

Copyright IBM Corporation 2008

Figure 5-14. Tables ALTER

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-15

Student Notebook

Tables DROP The DROP TABLE statement:


Removes a table from the database
Deleting the data and the table definition Indexes or constraints defined on the table are dropped as well

The following DROP TABLE statement deletes the BOOKS


table from the database:
DROP TABLE books;

Copyright IBM Corporation 2008

Figure 5-15. Tables DROP

CE5001.0

Notes:

5-16 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Tables NOT NULL, DEFAULT, and GENERATED NOT NULL:


If you do not want to allow null values, specify the NOT NULL clause for the column.

DEFAULT:
Specify a default value using the WITH DEFAULT clause and a default value.
CREATE TABLE books ( bookid INTEGER NOT NULL, bookname VARCHAR(100) WITH DEFAULT 'TBD', isbn CHAR(10) );

GENERATED:
Use the GENERATED ALWAYS option to have DB2 calculate the value of a column automatically.
CREATE TABLE books ( bookid INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), bookname VARCHAR(100) WITH DEFAULT 'TBD', isbn CHAR(10) );
Copyright IBM Corporation 2008

Figure 5-16. Tables NOT NULL, DEFAULT, and GENERATED

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-17

Student Notebook

Constraints Unique constraints Unique constraints


Used to ensure that values in a column are unique Can be defined over one or more columns
Each column included in the unique constraint must be defined as NOT NULL

Can be defined either as the PRIMARY KEY or UNIQUE constraint


CREATE TABLE books ( bookid INTEGER NOT NULL PRIMARY KEY, bookname VARCHAR(100), isbn CHAR(10) NOT NULL CONSTRAINT booksisbn UNIQUE );

The CONSTRAINT keyword lets you specify a name for the


constraint.
Copyright IBM Corporation 2008

Figure 5-17. Constraints Unique constraints

CE5001.0

Notes:

5-18 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Constraints Referential integrity constraints


Referential integrity constraints are used to define relationships between
tables and ensure that these relationships remain valid.
A PRIMARY KEY value must be UNIQUE and cannot be NULL By definition, a FOREIGN KEY matches a PRIMARY KEY of a UNIQUE KEY or can be null DB2 enforcement of referential constraints occurs during insert/update/load of FOREIGN KEY DB2 ensures that a PRIMARY KEY/UNIQUE KEY is updated only if no FOREIGN KEY matches the original value of the primary/unique key CREATE TABLE authors ( authorid INTEGER NOT NULL PRIMARY KEY, lname VARCHAR(100), fname VARCHAR(100)); CREATE TABLE books ( bookid INTEGER NOT NULL PRIMARY KEY, bookname VARCHAR(100), isbn CHAR(10), authorid INTEGER REFERENCES authors);
Copyright IBM Corporation 2008

Figure 5-18. Constraints Referential integrity constraints

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-19

Student Notebook

Constraints Table check constraints Table check constraints are used to verify that column data
does not violate rules defined for the column and to restrict the values in a certain column of a table.
DB2 ensures that the constraint is not violated during inserts and updates.

Example:
ALTER TABLE books ADD booktype CHAR(1) CHECK (booktype IN ('F','N') ); You can define check constraints when you create the table or add them later using the ALTER TABLE SQL statement. You can modify check constraints by dropping and then recreating them using the ALTER TABLE SQL statement.
Copyright IBM Corporation 2008

Figure 5-19. Constraints Table check constraints

CE5001.0

Notes:

5-20 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Views
Views allow different users or applications to look at the same data in
different ways. To the user, a view just looks like a table. Except for the view definition, a view does not take up space in the database; the data presented in a view is derived from another table. You can create a view on an existing table (or tables), on another view, or some combination of the two. A view defined on another view is called a nested view. To define column names in the view that are different from those in the base table, you can specify them in the CREATE VIEW statement. CREATE VIEW mybookview (title,type) AS SELECT bookname,booktype FROM books; If you do not want to allow a user to insert rows that are outside the scope of the view, you can define the view with the check option. The following statement defines a view using WITH CHECK OPTION:
"CREATE VIEW nonfictionbooks AS SELECT * FROM books WHERE booktype = 'N' WITH CHECK OPTION";
Copyright IBM Corporation 2008

Figure 5-20. Views

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-21

Student Notebook

Indexes An index is an ordered list of the key values of a column or


columns of a table.
There are two reasons why you might create an index:
To ensure uniqueness of values in a column or columns. To improve performance of queries against the table.

Indexes can be defined as unique or nonunique. Indexes are created using the CREATE INDEX SQL statement.
Indexes are also created implicitly in support of a PRIMARY KEY or UNIQUE constraint.

Indexes are created as ascending, descending, or bidirectional.


Bidirectional is recommended

Copyright IBM Corporation 2008

Figure 5-21. Indexes

CE5001.0

Notes:

5-22 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Creating indexes
CREATE UNIQUE INDEX dba1.empno ON dba1.employee (empno ASC) PCTFREE 10 MINPCTUSED 10 ALLOW REVERSE SCANS PAGE SPLIT SYMMETRIC COLLECT SAMPLED DETAILED STATISTICS; CRETE UNIQUE INDEX itemno ON albums (itemno); CREATE INDEX item ON stock (itemno) CLUSTER; CREATE UNIQUE INDEX empidx ON employee (empno) INCLUDE (lastname, firstname);
Copyright IBM Corporation 2008

Figure 5-22. Creating indexes

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-23

Student Notebook

Clustering indexes You can create one index on each table as the clustering
index:
A clustering index is useful when the table data is often referenced in a particular order. The clustering index defines the order in which data is stored in the database.

To create an index as the clustering index, specify the


CLUSTER clause on the CREATE INDEX statement:
CREATE INDEX iauthbkname ON books (authorid, bookname) CLUSTER;

Copyright IBM Corporation 2008

Figure 5-23. Clustering indexes

CE5001.0

Notes:

5-24 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Using included columns in indexes When creating an index, you have the option to include extra
column data that is stored with the key, but is not actually part of the key itself and is not sorted.
The main reason for including additional columns in an index is to improve the performance of certain queries: with this data already available in the index page, DB2 does not need to access the data page to fetch it.

Included columns can only be defined for unique indexes. Cons:


Requires more physical space in the database because the table data would essentially be duplicated in the index. All the copies of the data would need to be updated whenever the data value is updated.

Copyright IBM Corporation 2008

Figure 5-24. Using included columns in indexes

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 5. Working with DB2 objects


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

5-25

Student Notebook

Unit summary
Having completed this unit, you should be able to:

List data types and advanced data types Create tables, views, and indexes in a DB2 database Identify the features and use of unique constraints, referential
integrity constraints, and table check constraints

Use views to restrict access to data List the features of indexes

Copyright IBM Corporation 2008

Figure 5-25. Unit summary

CE5001.0

Notes:

5-26 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 6. Data concurrency


What this unit is about
This unit describes: Identifying factors that influence locking Listing objects on which locks can be obtained Identifying characteristics of DB2 locks Identifying the isolation level that should be used for a given situation

What you should be able to do


After completing this unit, you should be able to: Identify factors that influence locking List objects on which locks can be obtained Identify characteristics of DB2 locks Identify the isolation level that should be used for a given situation

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

Identify factors that influence locking List objects on which locks can be obtained Identify characteristics of DB2 locks Identify the isolation level that should be used for a given
situation

Copyright IBM Corporation 2008

Figure 6-1. Unit objectives

CE5001.0

Notes:

6-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Knowledge to identify factors that influence locking Ability to list objects on which locks can be obtained Knowledge to identify characteristics of DB2 locks Given a situation, knowledge to identify the isolation levels that
should be used

Copyright IBM Corporation 2008

Figure 6-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-3

Student Notebook

Simple workload consisting of three transactions


CONNECT TO my_db CREATE TABLE department (dept_id INTEGER NOT NULL, dept_name VARCHAR(20)) INSERT INTO department VALUES(100, 'PAYROLL') INSERT INTO department VALUES(200, 'ACCOUNTING') COMMIT; INSERT INTO department VALUES(300, 'SALES') ROLLBACK; INSERT INTO department VALUES(500, 'MARKETING') COMMIT;
Copyright IBM Corporation 2008

Figure 6-3. Simple workload consisting of three transactions

CE5001.0

Notes:

6-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Comparing successful and unsuccessful transactions

Copyright IBM Corporation 2008

Figure 6-4. Comparing successful and unsuccessful transactions

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-5

Student Notebook

Lock mode compatibility


MODE OF LOCK B MODE OF LOCK A IN S IS IX SIX U X IN YES YES YES YES YES YES YES IS YES YES YES YES YES YES NO S YES YES YES NO NO YES NO IX YES YES NO YES NO NO NO SIX U X Z YES YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO LOCK A MODE S U YES YES YES YES NO NO NO NO NO Z NO NO NO NO NO NO NO NO MODE OF LOCK B S YES YES NO NO YES NO U YES NO NO NO YES NO X NO NO NO NO NO NO W NO NO NO NO NO YES NS YES YES NO NO YES YES NW NO NO NO YES YES NO

Table Locks

Row Locks

X W NS NW

Copyright IBM Corporation 2008

Figure 6-5. Lock mode compatibility

CE5001.0

Notes:

6-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Concurrency and isolation levels Repeatable read (RR) Read stability (RS) Cursor stability (CS) Uncommitted read (UR or dirty read)

Copyright IBM Corporation 2008

Figure 6-6. Concurrency and isolation levels

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-7

Student Notebook

Repeatable read isolation level

Copyright IBM Corporation 2008

Figure 6-7. Repeatable read isolation level

CE5001.0

Notes:

6-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Read stability isolation level

Copyright IBM Corporation 2008

Figure 6-8. Read stability isolation level

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-9

Student Notebook

Cursor stability isolation level

Copyright IBM Corporation 2008

Figure 6-9. Cursor stability isolation level

CE5001.0

Notes:

6-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Uncommitted read isolation level

Copyright IBM Corporation 2008

Figure 6-10. Uncommitted read isolation level

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-11

Student Notebook

Lock attributes All locks have the following basic attributes:


Object: The object attribute identifies the data resource that is being locked. Size: The size attribute specifies the physical size of the portion of the data resource that is being locked. Duration: The duration attribute specifies the length of time for which a lock is held. Mode: The mode attribute specifies the type of access allowed for the lock owner as well as the type of access permitted for concurrent users of the locked data resource. This attribute is commonly referred to as the lock state.

Copyright IBM Corporation 2008

Figure 6-11. Lock attributes

CE5001.0

Notes:

6-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Table lock modes


IN IS IX SIX S U X Z Intent None Intention Share Intention eXclusive Share with Intention eXclusive Share Update eXclusive superexclusive

Row Locking also used (See next page)

Strict Table Locking

Copyright IBM Corporation 2008

Figure 6-12. Table lock modes

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-13

Student Notebook

Row lock modes


Row Lock S U X W NS NW Share Update eXclusive Weak exclusive Next key Share Next key Weak exclusive Minimum* Supporting Table Lock IS IX IX IX IS IX

An application does not acquire if it is using Table Locks of

Row locks S, U, X, or Z

Copyright IBM Corporation 2008

Figure 6-13. Row lock modes

CE5001.0

Notes:

6-14 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Lock conversion Changing of locks when application accesses data object (row,
table, or block) on which it already holds a lock and requires a more restrictive lock
More restrictive lock may or may not be granted

Example 1:
Application selects row from a table using FOR UPDATE clause FOR UPDATE causes U-lock on row Application then updates row using WHERE CURRENT of cursor Causes lock conversion of U-lock to X-lock

Example 2:
Application performs RS select which identifies a row Row is read using index, IS lock on table, S lock on row Application then performs update against row Table lock conversion from IS to IX, row lock from S to X
Copyright IBM Corporation 2008

Figure 6-14. Lock conversion

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-15

Student Notebook

Lock escalation

locklist
X X

maxlocks
X IX

m a x l o c k s

e x c e e d e d
X

ESCALATING APPLICATION
X X IX

..........
............

OTHER APPLICATIONS

.........

locklist

l o c f k u l l i l s t

............

Copyright IBM Corporation 2008

..........

Figure 6-15. Lock escalation

CE5001.0

Notes:

6-16 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Lock wait and timeout


12 locktimeout 30 9 6 x x x x x x x x x x x x 3

-1

I just want that one!

LOCK x HOG
x x

WAIT!

If the application hogging the locks doesnt COMMIT or ROLLBACK other applications wait until lock is available or timeout exceeded

Copyright IBM Corporation 2008

Figure 6-16. Lock wait and timeout

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-17

Student Notebook

Deadlock causes and detection


UNIT OF WORK DELETE SOME CEREAL AND MILK

RAISIN BRAN
APPLICATION A

MI

LK
APPLICATION B

dlchktime

10000

milliseconds

Copyright IBM Corporation 2008

Figure 6-17. Deadlock causes and detection

CE5001.0

Notes:

6-18 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit summary
Having completed this unit, you should be able to:

Identify factors that influence locking List objects on which locks can be obtained Identify characteristics of DB2 locks Identify the isolation level that should be used for a given
situation

Copyright IBM Corporation 2008

Figure 6-18. Unit summary

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 6. Data concurrency


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

6-19

Student Notebook

6-20 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 7. Introducing XQuery


What this unit is about
This unit describes: Understanding fundamental XQuery concepts Writing simple XQueries using several common expressions

What you should be able to do


After completing this unit, you should be able to: List fundamental XQuery concepts Write simple XQueries using several common expressions

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

List fundamental XQuery concepts Write simple XQueries using several common expressions

Copyright IBM Corporation 2008

Figure 7-1. Unit objectives

CE5001.0

Notes:

7-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Certification Exam (730) objectives Knowledge of XML data implications (non-shredding) Given an XQuery statement, knowledge to identify results

Copyright IBM Corporation 2008

Figure 7-2. Certification Exam (730) objectives

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-3

Student Notebook

Sample XML document

Copyright IBM Corporation 2008

Figure 7-3. Sample XML document

CE5001.0

Notes:

7-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

XQuery navigation Navigate to the city element


/Client/Address/city

More path expressions:


//* //email /Client/email[1]/text() /Client/Address/* /Client/data(@id) /Client/Address[state="CA"]/../email

Copyright IBM Corporation 2008

Figure 7-4. XQuery navigation

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-5

Student Notebook

Sample data for the clients table


INSERT INTO clients VALUES (3227, 'Ella Kimpton', 'Gold', '<Client> <Address> <street>5401 Julio Ave.</street> <city>San Jose</city> <state>CA</state> <zip>95116</zip> </Address> <phone> <work>4084630000</work> <home>4081111111</home> <cell>4082222222</cell> </phone> <fax>4087776666</fax> <email>love2shop@yahoo.com</email> </Client>' );

Copyright IBM Corporation 2008

Figure 7-5. Sample data for the clients table

CE5001.0

Notes:

7-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Simple XML data retrieval


Simple XQuery to return customer contact data
XQUERY db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')

Output from the query:


<?xml version="1.0" encoding="windows-1252" ?> <Client> <Address> <street> 5401 Julio Ave. </street> <city> San Jose </city> <state> CA </state> <zip> 95116 </zip> </Address> <phone> <work> 4084630000 </work> <home> 4081111111 </home> <cell> 4082222222 </cell> </phone> <fax> 4087776666 </fax> <email> love2shop@yahoo.com </email> </Client> Copyright IBM Corporation 2008

Figure 7-6. Simple XML data retrieval

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-7

Student Notebook

Specifying a single filtering predicate FLWOR expression with a new WHERE clause
XQUERY FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO') /Client/Address WHERE $y/zip="95116" RETURN $y

Output from the query:


<Address> <street>5401 Julio Ave.</street> <city>San Jose</city> <state>CA</state> <zip>95116</zip> </Address>
Copyright IBM Corporation 2008

Figure 7-7. Specifying a single filtering predicate

CE5001.0

Notes:

7-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Converting XML to HTML


Querying DB2 XML data and returning results as HTML:
XQUERY <ul> { FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO') /Client/Address ORDER BY $y/zip RETURN <li>{$y}</li> } </ul>

HTML output of the query:

<ul> <li> <Address> <street>9407 Los Gatos Blvd.</street> <city>Los Gatos</city> <state>CA</state> <zip>95032</zip> </Address> </li> <li> <Address> <street>4209 El Camino Real</street> <city>Mountain View</city> <state>CA</state> <zip>95033</zip> </Address> </li> . . . </ul>
Copyright IBM Corporation 2008

Figure 7-8. Converting XML to HTML

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-9

Student Notebook

Conditional logic
XQuery with a three-part conditional expression
FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client RETURN ( IF ($y/email) THEN $y/email[1] ELSE IF ($y/phone/home) THEN <homePhone>{$y/phone/home/text()}</homePhone> ELSE $y/Address)

Query output:
<email> love2shop@yahoo.com </email> <Address> <street> 1204 Meridian Ave. </street> <apt> 4A </apt> <city> San Jose </city> <state> CA </state> <zip> 95124 </zip> </Address> <homePhone> 4083332222 </homePhone> . . .
Copyright IBM Corporation 2008

Figure 7-9. Conditional logic

CE5001.0

Notes:

7-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Hybrid queries
Embedding SQL within an XQuery
XQUERY FOR $y IN db2-fn:sqlquery ('SELECT contactinfo FROM clients WHERE status=''Gold'' ')/Client WHERE $y/Address/city="San Jose" RETURN ( IF ($y/email) THEN <emailList>{$y/email}</emailList> ELSE $y/Address )

Query output

<emailList> <email> love2shop@yahoo.com </email> <Address> <street> 1204 Meridian Ave. </street> <apt> 4A </apt> <city> San Jose </city> <state> CA </state> <zip> 95124 </zip> </Address>
Copyright IBM Corporation 2008

Figure 7-10. Hybrid queries

CE5001.0

Notes:

Copyright IBM Corp. 2008

Part 7. Introducing XQuery


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

7-11

Student Notebook

Unit summary
Having completed this unit, you should be able to:

List fundamental XQuery concepts Write simple XQueries using several common expressions

Copyright IBM Corporation 2008

Figure 7-11. Unit summary

CE5001.0

Notes:

7-12 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Unit 8. Summary
What this unit is about
This unit describes: A summary of the units covered

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Unit 8. Summary

8-1

Student Notebook

Review of units covered Units covered:


DB2 9 Fundamentals exam 730 prep, Part 1: DB2 planning DB2 9 Fundamentals exam 730 prep, Part 2: Security DB2 9 Fundamentals exam 730 prep, Part 3: Accessing DB2 data DB2 9 Fundamentals exam 730 prep, Part 4: Working with DB2 data DB2 9 Fundamentals exam 730 prep, Part 5: Working with DB2 objects DB2 9 Fundamentals exam 730 prep, Part 6: Data concurrency DB2 9 Fundamentals exam 730 prep, Part 7: Introducing XQuery

Copyright IBM Corporation 2008

Figure 8-1. Review of units covered

CE5001.0

Notes:

8-2

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 1: DB2 planning


DB2 Editions DB2 components Roadmap to the GUI tools CLP Command Line Processor and CLP syntax Online reference Using the CLP Control Center Command Editor Journal Replication Center Configuration Assistant Visual Explain Developer Workbench Memory Visualizer Storage Management IBM Data Warehouse Edition
Copyright IBM Corporation 2008

Figure 8-2. Part 1: DB2 planning

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Unit 8. Summary

8-3

Student Notebook

Part 2: Security Basic client-server-host configuration DB2 authentication Authentication within the DBM configuration DB2 authentication types DB2 Authorities DB2 Privileges LBAC (Label Based Access Control) query Example implementation of LBAC

Copyright IBM Corporation 2008

Figure 8-3. Part 2: Security

CE5001.0

Notes:

8-4

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 3: Accessing DB2 data


What makes up a DB2 database? DB2 object hierarchy Default table space containers with Automatic Storage Creating your first database Control Center Database Configuration CREATE TABLESPACE syntax CREATE DATABASE syntax CREATE DATABASE examples Cataloging your DB2 database Configuration Assistant Manipulating DB2 objects with the Control Center Create Database Wizard Creating and accessing basic DB2 objects
Copyright IBM Corporation 2008

Figure 8-4. Part 3: Accessing DB2 data

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Unit 8. Summary

8-5

Student Notebook

Part 4: Working with DB2 data



The parts of speech of SQL Types of SQL statements Data Manipulation Language (DML) SELECT Data Manipulation Language (DML) WHERE Data Manipulation Language (DML) ORDER BY Data Manipulation Language (DML) Joins Data Manipulation Language (DML) Set operators Data Manipulation Language (DML) GROUP BY Data Manipulation Language (DML) INSERT Data Manipulation Language (DML) UPDATE Data Manipulation Language (DML) DELETE Data Manipulation Language (DML) MERGE Data Manipulation Language (DML) Reference Units of work and savepoints Creating and calling an SQL procedure Creating and using SQL user-defined functions User-defined table function
Copyright IBM Corporation 2008

Figure 8-5. Part 4: Working with DB2 data

CE5001.0

Notes:

8-6

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 5: Working with DB2 objects


Data types Numeric data types String data types Datetime data types XML data type User-defined data types DB2 Extenders Tables CREATE, ALTER, DROP, NOT NULL, DEFAULT, and GENERATED Constraints Unique constraints, Referential integrity constraints, Table check constraints Views Creating indexes Clustering indexes Using included columns in indexes
Copyright IBM Corporation 2008

Figure 8-6. Part 5: Working with DB2 objects

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Unit 8. Summary

8-7

Student Notebook

Part 6: Data concurrency


Simple workload consisting of three transactions Comparing successful and unsuccessful transactions Concurrency and isolation levels Repeatable read, Read stability, Cursor stability, Uncommitted read isolation levels Lock attributes Table lock modes Row lock modes Lock mode compatibility Lock conversion Lock escalation Lock wait and timeout Deadlock causes and detection
Copyright IBM Corporation 2008

Figure 8-7. Part 6: Data concurrency

CE5001.0

Notes:

8-8

AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V4.1
Student Notebook

Uempty

Part 7: Introducing XQuery Sample XML document XQuery navigation Sample data for the clients table Simple XML data retrieval Specifying a single filtering predicate Converting XML to HTML Conditional logic Hybrid queries

Copyright IBM Corporation 2008

Figure 8-8. Part 7: Introducing XQuery

CE5001.0

Notes:

Copyright IBM Corp. 2008


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Unit 8. Summary

8-9

Student Notebook

8-10 AI Cert Prep for Exam 730


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Copyright IBM Corp. 2008

V5.1

backpg

Back page

You might also like