Basic SQL - Part I Understanding Database Objects
Basic SQL - Part I Understanding Database Objects
B A Nagabhushan IBM India Software Labs In-demand skills for an on demand world
2005 IBM Corporation
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 2
2005 IBM Corporation
SQL An overview
A query language used to query or modify information from a Relational Database System Standard Language for an RDBMS as per the American National Standards Institute (ANSI)
All RDBMS vendors must conform to the ANSI standards
SQL - Components
Data Definition Language (DDL)
Used to create, modify or drop database objects
11/04/2005
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 5
2005 IBM Corporation
Data Types
Each column in a DB2 table must be associated with a data type Data Type indicates the kind of data that is valid for the column
11/04/2005
11/04/2005
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 8
2005 IBM Corporation
Tables
A table is an un-ordered set of rows Rows consists of columns Each column is based on a datatype Tables are referenced in the FROM and INTO clauses of SQL statements Three types of tables
Permanent (base) tables Temporary (declared) tables Temporary (derived) tables
11/04/2005 9
2005 IBM Corporation
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 10
2005 IBM Corporation
Schemas
Used in DB2 to logically group other database objects Most database objects are named using a twopart naming convention SCHEMA_NAME. OBJECT_NAME Example: BBHUSHAN.TABLE1 where BBHUSHAN is the schema name and TABLE1 is the object name Implicit schema when no schema is specified Authorization ID
11/04/2005 11
2005 IBM Corporation
Schemas Contd.
Implicit schema when referenced in an SQL statement, once again, the authorization ID CURRENT SCHEMA register contains default qualifier
Used for unqualified objects referenced for dynamic SQL statements Can be modified by SET CURRENT SCHEMA Static SQL statements are qualified by the authorization ID of the person binding the application
11/04/2005 12
2005 IBM Corporation
Schemas Contd.
Another SCHEMA qualifier is the SESSION keyword
Used for temporary tables created and used during a connection Only way to reference a declared temporary table in the SQL is through the SESSION qualifier If not used, DB2 tries to find the table using the current schema
11/04/2005
13
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 14
2005 IBM Corporation
Table Spaces
Logical layer between the database and tables stored in the database Table spaces are created within a database, and tables are created within table spaces Two kinds of table spaces:
System Managed Space (SMS) Database Managed Space (DMS)
11/04/2005
15
11/04/2005
16
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 17
2005 IBM Corporation
Views
Virtual tables derived from one or more tables or views Can be used interchangeably when getting data Changes made to the data through a view are made to the underlying table too Views do not contain real data only definition is stored in the database Used to limit access to sensitive data Views can be deletable, updatable, or read-only
11/04/2005 18
2005 IBM Corporation
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 19
2005 IBM Corporation
Indexes
Ordered set of pointers to rows in a table Associated with individual objects Any permanent table Any declared temporary table Indexes cannot be defined on a view Multiple indexes can be defined on a single table
11/04/2005
20
Indexes Contd.
Indexes are maintained automatically by DB2 as data is inserted, updated, and deleted
Caution: Maintenance overhead can negatively impact performance of INSERT, UPDATE and DELETE
11/04/2005 21
2005 IBM Corporation
Indexes Contd.
Indexes can be defined in ascending or descending order Indexes can be defined as unique or non-unique
Indexes can be defined on a single column or multiple columns Indexes can be defined to support forward or reverse scans Visual Explain shows if a particular index is used in an SQL statement
11/04/2005 22
11/04/2005
23
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 24
2005 IBM Corporation
Packages
Contain executable forms of SQL statements. Corresponds to a program source module
Only the corresponding program source module can invoke the contents of a package
Packages contain access plans selected by DB2 during the BIND or PREP process.
Static Binding as this is performed prior to execution of SQL statement
11/04/2005
26
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 27
2005 IBM Corporation
Buffer Pools
Buffer pools can be assigned to cache only a particular table space Every database must have a buffer pool
IBMDEFAULTBP is the default buffer pool
Once a buffer pool is created, table spaces can be assigned to it SYSCAT.BUFFERPOOLS catalog view shows the buffer pools in a database
11/04/2005 28
2005 IBM Corporation
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 29
2005 IBM Corporation
Transactions
Sequence of SQL statements executing a single operation
Same as Unit of Work
A transaction either succeeds or fails A transaction starts with the first executable statement in a program and ends with either an explicit or implicit COMMIT or ROLLBACK
Implicit COMMIT or ROLLBACK can occur when a DB2 application terminates
11/04/2005
30
Transactions Contd.
DB2 allows SAVEPOINTs within a transaction
Allows selective rollback without undoing work prior to the savepoint Provide control over the work performed by a subset of SQL statements in a transaction
11/04/2005
31
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 32
2005 IBM Corporation
Locks
DB2 locking mechanism attempts to avoid resource conflicts, but with full data integrity Transactions obtain locks as SQL statements are processed Locks are released when the resource is no longer required or at the end of the transaction Locks are stored in memory on the database server (in locklist) Two types of locks table locks and row locks
11/04/2005 33
2005 IBM Corporation
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 34
2005 IBM Corporation
Log Files
As transactions are processed, they are tracked within log files DB2 uses write-ahead logging
Changes are first written to the log files, and later, applied to the physical database tables
Applications should commit their work at regular intervals to avoid running out of log space
11/04/2005
35
11/04/2005
36
Agenda
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 37
2005 IBM Corporation
Creating a database
A DB2 database must exist before any of the objects can be created in it Database must have a name No schema associated with the database
CREATE DATABASE CREATE DATABASE is not a SQL statement it is a DB2 CLP command Next logical step is to create table spaces
11/04/2005
38
Creating a database
When a database is created without any table space options, three SMS table spaces are created by default:
SYSCATSPACE contains system catalog tables TEMPSPACE1 contains temporary tables used by DB2 USERSPACE1 contains the user tables
11/04/2005
39
11/04/2005
41
Summary
Data Types Tables Schemas Table spaces Views Indexes Packages Buffer Pools Transactions Locks Log files Creating a database
11/04/2005 43
2005 IBM Corporation
Your turn
Exercises posted in the forum Answers will be posted 3 days later
11/04/2005
44
References
Get the most out of DB2 UDB documentation http://www.ibm.com/developerworks/db2/library/techarticle/ dm-0412chong/ DB2 Basics: Table spaces and Buffer pools http://www.ibm.com/developerworks/db2/library/techarticle/ 0212wieser/0212wieser.html New to DB2? http://www.ibm.com/developerworks/db2/newto/db2basics.h tml
DB2 Universal Database V8.1 for Linux, UNIX, and Windows - Database Administration Certification Guide - George Baklarz & Bill Wong
11/04/2005 45
2005 IBM Corporation