Professional Documents
Culture Documents
cover
Front cover
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.
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
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
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
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
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.
Trademarks
vii
Student Notebook
viii
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.
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)
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
Agenda
xi
Student Notebook
xii
V4.1
Student Notebook
Uempty
I need to call Larry Jones in the company directory... now, where is his phone number?
CE5001.0
Notes:
-xiii
Student Notebook
Data in a DBMS
DB2
Integrity and Recovery
Tables - R - Us
Continuous Operation
DBA
Optimizer
Logging
Lock Manager
Security
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
"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
CE5001.0
Notes:
-xv
Student Notebook
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Q Flexible
Notes:
TRUCTURED
UERY
ANGUAGE
CE5001.0
-xvii
Student Notebook
Table name
Describe the columns (give each column 3 attributes) COLUMN NAME DATATYPE / LENGTH NULL CHARACTERISTIC
DBA DBA
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CREATE TABLE COMPANY.DIRECTORY ( ID LASTNAME FIRSTNAME ADDRESS ~ INTEGER CHAR(15) CHAR(15) VARCHAR(45) ~ NOT NULL WITH DEFAULT, ~ NOT NULL , , ,
NOT NULL
CE5001.0
Notes:
-xix
Student Notebook
Application Manager
Logging Optimizer
Continuous Operation
Lock Manager
Security
Application Application
Application
Application
Application Application
Application Programs
Data Tables
CE5001.0
Notes:
-xx
V4.1
Student Notebook
Uempty
DB2
Integrity & Recovery
Lock
Continuous Operation
Lock Manager
Lock Manager
Security
Data
CE5001.0
Notes:
-xxi
Student Notebook
DB2
Integrity & Recovery
Continuous Operation
Logging Optimizer
Write
Lock Manager Security
Data Write/Commit
Logging
LOG
Copyright IBM Corporation 2008
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
1-1
Student Notebook
Unit objectives
After completing this unit, you should understand:
List the tools that are included with DB2. Use the Control Center to manage systems, DB2 instances,
databases, database objects, and more.
Use all of the standalone tools in the Control Center and the
Configuration Assistant.
CE5001.0
Notes:
1-2
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)
CE5001.0
Notes:
1-3
Student Notebook
DB2 Editions
Massively Parallel Processor (MPP)
Cluster
DB2
CE5001.0
Notes:
1-4
V4.1
Student Notebook
Uempty
DB2 components
DRDA Application Requester
CE5001.0
Notes:
1-5
Student Notebook
CE5001.0
Notes:
1-6
V4.1
Student Notebook
Uempty
CLP syntax
CE5001.0
Notes:
1-7
Student Notebook
CE5001.0
Notes:
1-8
V4.1
Student Notebook
Uempty
Information
General Administration Tools Control Center (db2cc) Journal (db2journal) Replication Center (dbrc) Task Center (db2tc) License Center
Monitoring Tools
Setup Tools
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
CE5001.0
Notes:
1-9
Student Notebook
Control Center
Contents Pane
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Command Editor
SQL Assist
CE5001.0
Notes:
1-11
Student Notebook
Journal
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Configuration Assistant
CE5001.0
Notes:
1-13
Student Notebook
Visual Explain
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Developer Workbench
CE5001.0
Notes:
1-15
Student Notebook
Memory Visualizer
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Storage Management
CE5001.0
Notes:
1-17
Student Notebook
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Unit summary
Having completed this unit, you should understand:
List the tools that are included with DB2. Use the Control Center to manage systems, DB2 instances,
databases, database objects, and more.
Use all of the standalone tools in the Control Center and the
Configuration Assistant.
CE5001.0
Notes:
1-19
Student Notebook
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
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
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
CE5001.0
Notes:
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)
CE5001.0
Notes:
2-4
V4.1
Student Notebook
Uempty
Linux
Windows
AIX
CE5001.0
Notes:
Part 2. Security
2-5
Student Notebook
CE5001.0
Notes:
2-6
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
Part 2. Security
2-7
Student Notebook
CE5001.0
Notes:
2-8
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
CE5001.0
Notes:
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
ALL ALTER CREATEIN DELETE ALTERIN INDEX DROPIN INSERT REFERENCES SELECT USE UPDATE
Copyright IBM Corporation 2008
CE5001.0
Notes:
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
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.
Part 2. Security
2-11
Student Notebook
No LBAC LBAC
SALARY 60000 50000 70000 45000 30000 56000 82000 54000 33000 46000 83000 78000 45000
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
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.
CE5001.0
Notes:
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
3-2
V4.1
Student Notebook
Uempty
Certification Exam (730) objectives Ability to identify DB2 objects Knowledge of basic characteristics and properties of DB2
objects
CE5001.0
Notes:
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
CE5001.0
Notes:
3-4
V4.1
Student Notebook
Uempty
Database 1
View1 View2
DB configuration file
USERSPACE1
Table1 Index1
TSDMSLRG3
Table2
Table3
TSDMSREG2
Table2
CE5001.0
Notes:
3-5
Student Notebook
dbauto
inst20 NODEnnnn
DB2INSTANCE=inst20
DSS T0000000
C0000000.CAT
T0000001 T0000002
C0000000.LRG
Copyright IBM Corporation 2008
USERSPACE1
CE5001.0
Notes:
3-6
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
3-7
Student Notebook
Control Center
CE5001.0
Notes:
3-8
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
3-9
Student Notebook
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"
num-pages
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
3-11
Student Notebook
Database Configuration
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
MANAGED BY
num-pages integer K M G
PREFETCHSIZE
BUFFERPOOL K M G
bufferpool-name
OVERHEAD
FILE SYSTEM CACHING NO FILE SYSTEM CACHING DROPPED TABLE RECOVERY ON OFF
TRANSFERRATE
CE5001.0
Notes:
3-13
Student Notebook
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
AUTHENTICATION
WITH -- "comment-string"
CE5001.0
Notes:
3-15
Student Notebook
Configuration Assistant (1 of 2)
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Configuration Assistant (2 of 2)
CE5001.0
Notes:
3-17
Student Notebook
Contents pane
Objects pane
Details
CE5001.0
Notes:
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
CE5001.0
Notes:
3-19
Student Notebook
V4.1
Student Notebook
Uempty
4-1
Student Notebook
Unit objectives
After completing this unit, you should be able to:
Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage
transactions, and know what constitutes a transaction boundary
CE5001.0
Notes:
4-2
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)
CE5001.0
Notes:
4-3
Student Notebook
The parts of speech of SQL SQL is a language that is used to define and manipulate
database objects.
CE5001.0
Notes:
4-4
V4.1
Student Notebook
Uempty
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:
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;
CE5001.0
Notes:
4-6
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
CE5001.0
Notes:
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";
CE5001.0
Notes:
4-8
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
CE5001.0
Notes:
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";
CE5001.0
Notes:
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;
Notes:
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);
CE5001.0
Notes:
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;
CE5001.0
Notes:
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!
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
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.
CE5001.0
Notes:
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.
CE5001.0
Notes:
4-17
Student Notebook
CE5001.0
Notes:
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
CE5001.0
Notes:
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.
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Unit summary
Having completed this unit, you should be able to:
Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage
transactions, and know what constitutes a transaction boundary
CE5001.0
Notes:
4-21
Student Notebook
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
5-2
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
CE5001.0
Notes:
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)
CE5001.0
Notes:
5-4
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
5-5
Student Notebook
CE5001.0
Notes:
5-6
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.
CE5001.0
Notes:
5-7
Student Notebook
CE5001.0
Notes:
5-8
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
5-9
Student Notebook
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);
CE5001.0
Notes:
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
CE5001.0
Notes:
5-11
Student Notebook
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Before using these data types, install the extender support into
the database.
Copyright IBM Corporation 2008
CE5001.0
Notes:
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;
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
5-15
Student Notebook
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
5-17
Student Notebook
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
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
CE5001.0
Notes:
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
CE5001.0
Notes:
5-21
Student Notebook
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.
CE5001.0
Notes:
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
CE5001.0
Notes:
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.
CE5001.0
Notes:
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.
CE5001.0
Notes:
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
6-2
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
CE5001.0
Notes:
6-3
Student Notebook
CE5001.0
Notes:
6-4
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
6-5
Student Notebook
Table Locks
Row Locks
X W NS NW
CE5001.0
Notes:
6-6
V4.1
Student Notebook
Uempty
Concurrency and isolation levels Repeatable read (RR) Read stability (RS) Cursor stability (CS) Uncommitted read (UR or dirty read)
CE5001.0
Notes:
6-7
Student Notebook
CE5001.0
Notes:
6-8
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
6-9
Student Notebook
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
6-11
Student Notebook
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
6-13
Student Notebook
Row locks S, U, X, or Z
CE5001.0
Notes:
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
CE5001.0
Notes:
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
............
..........
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
-1
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
CE5001.0
Notes:
6-17
Student Notebook
RAISIN BRAN
APPLICATION A
MI
LK
APPLICATION B
dlchktime
10000
milliseconds
CE5001.0
Notes:
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
CE5001.0
Notes:
6-19
Student Notebook
V4.1
Student Notebook
Uempty
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
CE5001.0
Notes:
7-2
V4.1
Student Notebook
Uempty
Certification Exam (730) objectives Knowledge of XML data implications (non-shredding) Given an XQuery statement, knowledge to identify results
CE5001.0
Notes:
7-3
Student Notebook
CE5001.0
Notes:
7-4
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
7-5
Student Notebook
CE5001.0
Notes:
7-6
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
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
CE5001.0
Notes:
7-8
V4.1
Student Notebook
Uempty
<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
CE5001.0
Notes:
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
CE5001.0
Notes:
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
CE5001.0
Notes:
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
CE5001.0
Notes:
V4.1
Student Notebook
Uempty
Unit 8. Summary
What this unit is about
This unit describes: A summary of the units covered
Unit 8. Summary
8-1
Student Notebook
CE5001.0
Notes:
8-2
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
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
CE5001.0
Notes:
8-4
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
Unit 8. Summary
8-5
Student Notebook
CE5001.0
Notes:
8-6
V4.1
Student Notebook
Uempty
CE5001.0
Notes:
Unit 8. Summary
8-7
Student Notebook
CE5001.0
Notes:
8-8
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
CE5001.0
Notes:
Unit 8. Summary
8-9
Student Notebook
V5.1
backpg
Back page