Top Banner
 © 2005 IBM Corporation Basic SQL – Par t I Understanding Database Objects B A Nagabhushan IBM India Software Labs In-demand skills for an on demand world
45

SQL Basics 1 Presentation

Apr 06, 2018

Download

Documents

1981todurkar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 1/45

 © 2005 IBM Corporation

Basic SQL – Part IUnderstanding Database Objects

B A Nagabhushan

IBM India Software Labs

In-demand skills for an on demand world

Page 2: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 2/45

11/04/2005 2  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 3: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 3/45

11/04/2005 3  © 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 ANSIstandards

• Supported on mainframes, minicomputers and

PCs

• Current SQL standard is SQL99

Page 4: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 4/45

11/04/2005 4  © 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

Page 5: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 5/45

11/04/2005 5  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 6: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 6/45

11/04/2005 6  © 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

Page 7: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 7/45

11/04/2005 7  © 2005 IBM Corporation

Data Types - Categories• Two major categories of data types in DB2

Built-in data typesUser-defined data types

• User-defined distinct types

• User-defined structured types

• User-defined reference type

Page 8: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 8/45

11/04/2005 8  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 9: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 9/45

11/04/2005 9  © 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) tablesTemporary (declared) tables

Temporary (derived) tables

Page 10: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 10/45

11/04/2005 10  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 11: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 11/45

11/04/2005 11  © 2005 IBM Corporation

Schemas• Used in DB2 to logically group other database

objects

• Most database objects are named using a two-part naming convention – SCHEMA_NAME.OBJECT_NAME

• Example: BBHUSHAN.TABLE1 whereBBHUSHAN is the schema name and TABLE1

is the object name• Implicit schema when no schema is specified

Authorization ID

Page 12: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 12/45

11/04/2005 12  © 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 dynamicSQL statements

Can be modified by SET CURRENT SCHEMA

Static SQL statements are qualified by the

authorization ID of the person binding the

application

Page 13: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 13/45

11/04/2005 13  © 2005 IBM Corporation

Schemas – Contd.• Another SCHEMA qualifier is the

SESSION keywordUsed for temporary tables created and used during a

connection

Only way to reference a declared temporary table inthe SQL is through the SESSION qualifier

If not used, DB2 tries to find the table using the

current schema

Page 14: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 14/45

11/04/2005 14  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 15: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 15/45

11/04/2005 15  © 2005 IBM Corporation

Table Spaces• Logical layer between the database and tables

stored in the database

• Table spaces are created within a database, andtables are created within table spaces

• Two kinds of table spaces:

System Managed Space (SMS)Database Managed Space (DMS)

Page 16: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 16/45

11/04/2005 16  © 2005 IBM Corporation

Table Spaces – Contd.• By default, tables are created in USERSPACE1

table space

• Any number of table spaces can be created in adatabase, and any number of tables in a tablespace

Page 17: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 17/45

11/04/2005 17  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 18: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 18/45

11/04/2005 18  © 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 definitionis stored in the database

• Used to limit access to sensitive data

• Views can be deletable, updatable, or read-only

Page 19: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 19/45

11/04/2005 19  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 20: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 20/45

11/04/2005 20  © 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 singletable

Page 21: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 21/45

11/04/2005 21  © 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

columnsOptimizer can save computation time

• Indexes are maintained automatically by DB2 asdata is inserted, updated, and deleted

Caution: Maintenance overhead can negatively impactperformance of INSERT, UPDATE and DELETE

Page 22: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 22/45

11/04/2005 22  © 2005 IBM Corporation

Indexes – Contd.• Indexes can be defined in ascending ordescending order

• Indexes can be defined as unique or non-unique

• Indexes can be defined on a single column ormultiple columns

• Indexes can be defined to support forward orreverse scans

• Visual Explain shows if a particular index isused in an SQL statement

Page 23: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 23/45

11/04/2005 23  © 2005 IBM Corporation

Indexes – Features new in V8• Type-2 indexes eliminates “next-key” locking

Transactions in prior releases could lock an adjacentrecord during processing, resulting in unnecessarydelays 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

Page 24: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 24/45

11/04/2005 24  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 25: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 25/45

11/04/2005 25  © 2005 IBM Corporation

Packages• Contain executable forms of SQL statements.

• Corresponds to a program source moduleOnly the corresponding program source module can

invoke the contents of a package

• Packages contain access plans selected by DB2during the BIND or PREP process.

“Static Binding” as this is performed prior toexecution of SQL statement

• Packages cannot be directly referenced in DML

statements

Page 26: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 26/45

11/04/2005 26  © 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 andnot invalidate the existing package that users arerunning

Page 27: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 27/45

11/04/2005 27  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 28: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 28/45

11/04/2005 28  © 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 aparticular 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 showsthe buffer pools in a database

Page 29: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 29/45

11/04/2005 29  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 30: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 30/45

11/04/2005 30  © 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 executablestatement in a program and ends with either anexplicit or implicit COMMIT or ROLLBACK

Implicit COMMIT or ROLLBACK can occur whena DB2 application terminates

Page 31: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 31/45

11/04/2005 31  © 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 subsetof SQL statements in a transaction

Page 32: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 32/45

11/04/2005 32  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 33: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 33/45

11/04/2005 33  © 2005 IBM Corporation

Locks• DB2 locking mechanism attempts to avoid

resource conflicts, but with full data integrity

• Transactions obtain locks as SQL statements areprocessed

• Locks are released when the resource is nolonger 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

Page 34: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 34/45

11/04/2005 34  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions• Locks

• Log files

• Creating a database

Page 35: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 35/45

11/04/2005 35  © 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

Page 36: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 36/45

11/04/2005 36  © 2005 IBM Corporation

Log Files – New features in V8• Log files can be 256GB in size

Log running transactions can finish without fillingup one log file

Even then, DB2 allows chaining of log files so thattheoretically a user should never run out of log

space

Page 37: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 37/45

11/04/2005 37  © 2005 IBM Corporation

Agenda• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions

• Locks

• Log files

• Creating a database

Page 38: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 38/45

11/04/2005 38  © 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 aDB2 CLP command

• Next logical step is to create table spaces

Page 39: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 39/45

11/04/2005 39  © 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 byDB2

USERSPACE1 – contains the user tables

Page 40: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 40/45

11/04/2005 40  © 2005 IBM Corporation

Creating a database - SyntaxCREATE DATABASE database-name

[AT DBPARTITIONNUM | [ON drive] [ALIAS database-

alias]

[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}]]

Page 41: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 41/45

11/04/2005 41  © 2005 IBM Corporation

Creating a database - Syntaxtblspace-defn:

 MANAGED BY { SYSTEM USING ('string' [ {,'string'}

... ] ) |

DATABASE USING ({FILE | DEVICE} 'string' number-of-

pages

[ {,{FILE | DEVICE} 'string' number-of-pages} ... ]

) }

[EXTENTSIZE number-of-pages] [PREFETCHSIZE number-

of-pages]

[OVERHEAD number-of-milliseconds] [TRANSFERRATE

number-of-milliseconds]

[NO FILE SYSTEM CACHING | FILE SYSTEM CACHING]

Page 42: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 42/45

11/04/2005 42  © 2005 IBM Corporation

Creating a database - Examplecreate 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')

Page 43: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 43/45

11/04/2005 43  © 2005 IBM Corporation

Summary• Data Types

• Tables

• Schemas• Table spaces

• Views

• Indexes• Packages

• Buffer Pools

• Transactions

• Locks

• Log files

• Creating a database

Page 44: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 44/45

11/04/2005 44  © 2005 IBM Corporation

Your turn…• Exercises posted in the forum

• Answers will be posted 3 days later

Page 45: SQL Basics 1 Presentation

8/3/2019 SQL Basics 1 Presentation

http://slidepdf.com/reader/full/sql-basics-1-presentation 45/45

11/04/2005 45  © 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 AdministrationCertification Guide - George Baklarz & Bill Wong