Anda di halaman 1dari 45

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

Supported on mainframes, minicomputers and PCs Current SQL standard is SQL99


11/04/2005 3
2005 IBM Corporation

SQL - Components
Data Definition Language (DDL)
Used to create, modify or drop database objects

Data Manipulation Language (DML)


Used to select, insert, update or delete database data (records)

Data Control Language (DCL)


Used to provide data object access control

11/04/2005

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 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

2005 IBM Corporation

Data Types - Categories


Two major categories of data types in DB2 Built-in data types User-defined data types
User-defined distinct types User-defined structured types User-defined reference type

11/04/2005

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 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

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 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

2005 IBM Corporation

Table Spaces Contd.


By default, tables are created in USERSPACE1 table space Any number of table spaces can be created in a database, and any number of tables in a table space

11/04/2005

16

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 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

2005 IBM Corporation

Indexes Contd.

Indexes are used for two primary reasons


Ensure uniqueness of data values Improve SQL query performance

Indexes can also be created on computed columns


Optimizer can save computation time

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

2005 IBM Corporation

Indexes Features new in V8


Type-2 indexes eliminates next-key locking
Transactions in prior releases could lock an adjacent record during processing, resulting in unnecessary delays in completing transactions

Indexes can be created on temporary tables.


Prior to this, any query against a temporary table object would result in table scan

11/04/2005

23

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 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

Packages cannot be directly referenced in DML statements


11/04/2005 25
2005 IBM Corporation

Packages Features new in V8


Packages can now exist in different version numbers
Multiple versions of the same package can coexist Advantage: Changes to an existing application and not invalidate the existing package that users are running

11/04/2005

26

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 27
2005 IBM Corporation

Buffer Pools

Used to cache data pages in memory


Physical I/O can be minimized

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

2005 IBM Corporation

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

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 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

2005 IBM Corporation

Log Files New features in V8


Log files can be 256GB in size
Log running transactions can finish without filling up one log file Even then, DB2 allows chaining of log files so that theoretically a user should never run out of log space

11/04/2005

36

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 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

2005 IBM Corporation

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

2005 IBM Corporation

Creating a database - Syntax


CREATE DATABASE database-name [AT DBPARTITIONNUM | [ON drive] [ALIAS databasealias] [USING CODESET codeset TERRITORY territory] [COLLATE USING {SYSTEM | IDENTITY | IDENTITY_16BIT | COMPATIBILITY | NLSCHAR}] [NUMSEGS numsegs] [DFT_EXTENT_SZ dft_extentsize] [CATALOG TABLESPACE tblspace-defn] [USER TABLESPACE tblspace-defn] [TEMPORARY TABLESPACE tblspace-defn] [WITH "comment-string"]] [AUTOCONFIGURE [USING config-keyword value [{,config-keyword value}...]] [APPLY {DB ONLY | DB AND DBM | NONE}]]
11/04/2005 40
2005 IBM Corporation

Creating a database - Syntax


tblspace-defn: MANAGED BY { SYSTEM USING ('string' [ {,'string'} ... ] ) | DATABASE USING ({FILE | DEVICE} 'string' number-ofpages [ {,{FILE | DEVICE} 'string' number-of-pages} ... ] ) } [EXTENTSIZE number-of-pages] [PREFETCHSIZE numberof-pages] [OVERHEAD number-of-milliseconds] [TRANSFERRATE number-of-milliseconds] [NO FILE SYSTEM CACHING | FILE SYSTEM CACHING]

11/04/2005

41

2005 IBM Corporation

Creating a database - Example


create database vtutest on d: catalog tablespace managed by system using ('D:\vtutest1\vtucat') user tablespace managed by system using ('D:\vtutest2\vtuusr') temporary tablespace managed by system using ('D:\vtutest3\vtutemp')
11/04/2005 42
2005 IBM Corporation

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

2005 IBM Corporation

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

Anda mungkin juga menyukai