50
IBM DB2 9 Section -3) Working with Working with Databases and Database Objects Databases and Database Objects © 2008 IBM Corporation Amit Daharwal IT Specialist – IBM Academic Initiative

3) Working With Databases and Database Objects Ppt

Embed Size (px)

Citation preview

Page 1: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Section -3) Working with Working with

Databases and Database ObjectsDatabases and Database Objects

© 2008 IBM Corporation

Amit Daharwal

IT Specialist – IBM Academic Initiative

Page 2: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Section 3 Section 3 -- Working with Databases and Working with Databases and

Database Objects (17%)Database Objects (17%)

��Ability to identify and connect to DB2 servers Ability to identify and connect to DB2 servers

and databasesand databases

��Ability to identify DB2 objectsAbility to identify DB2 objects

2

��Ability to identify DB2 objectsAbility to identify DB2 objects

��Knowledge of basic characteristics and Knowledge of basic characteristics and

properties of DB2 objectsproperties of DB2 objects

��Given a DDL SQL statement, ability to identify Given a DDL SQL statement, ability to identify

results (ability to create DB2 objects)results (ability to create DB2 objects)

Page 3: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Servers, Instances, and Databases

�During the installation process, program files for a background

process known as the DB2 Database Manager are physically

copied to a specific location on the server.

3

�A DB2 instance is a logical database server environment.

There can be multiple DB2 instances on a server. Multiple

Databases can be created within a DB2 instance.

Page 4: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

More on instances

�The default instance for a particular system is defined by the

DB2INSTANCE environment variable. Initially for Windows

default is ‘DB2’ and for Linux/Unix it is ‘db2inst1’.

�DB2 9 Instance Management Commands:

Command Purpose

4

db2icrt [InstanceName] Creates a new instance

db2idrop [InstanceName] Deletes (drops) an existing instance

db2ilist Lists all instances that have been defined

db2start Starts the DB2 Database Manager background

processes for the current instance

db2stop Stops the DB2 Database Manager background

processes for the current instance

Page 5: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

DB2 Storage Model

5

Page 6: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

DB2 Storage Model

�A database is created within an instance.

�There may be multiple databases managed by a DB2

instance

�Logical storage model:

�Tables are made up of columns and rows

6

�Tables are placed into table spaces

�Table spaces can contain more than one table

�Physical storage model:

�Container is a physical storage device

�Can be a directory, a device, or a file

�Container is assigned to a table space

Page 7: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Tablespaces

�A tablespace is a logical layer between data and storage

devices.

�Consists of one or more containers.

�Tables, regular data, indexes, and long data reside in

tablespaces

�SQL: CREATE / DROP / ALTER TABLESPACE

7

�SQL: CREATE / DROP / ALTER TABLESPACE

�To display Table space info:

�LIST TABLESPACES [SHOW DETAIL]

�Details about containers can be obtained with following

command.

�LIST TABLESPACE CONTAINERS FOR tablespace_id

[SHOW DETAIL]

Page 8: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Types of Tablespaces

�System-Managed Space (SMS):

�OS’s file system manager allocates and manages the space.

�Database-Managed Space (DMS):

�Here, the database manager controls the storage space.

�Automatic Storage With DMS:

�Databases that are enabled for automatic storage have a set

8

�Databases that are enabled for automatic storage have a set

of one or more storage paths associated with them.

�A table space can be defined as "managed by automatic

storage" and its containers assigned and allocated by DB2

based on those storage paths.

�You cannot enable/disable automatic storage once it is

defined for database.

�Default type in DB2 9.

Page 9: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

9

Page 10: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database

� The database contains following objects:

�Tables, views, indexes, schemas, Locks, triggers, stored

procedures, packages, Buffer pools, log files, table spaces

� Tools, which can help you create DB2 9 database are First

Steps, Control Center [ Using GUI Wizard ] and Command

Line Processor (CLP).

10

Line Processor (CLP).

� Some database commands -

CREATE DATABASE

<dbname>

To create a database. (Database name

can not begin with "SYS", "DBM", or "IBM“)

CONNECT TO <dbname> To connect to a database

CONNECT RESET To terminate connection

DROP DATABASE <dbname> To drop database

Page 11: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

With database, DB2 automatically creates

� Three table spaces are created.

� SYSCATSPACE - This is where the DB2 system

catalog is kept that tracks all of the metadata

associated with DB2 objects.

� TEMPSPACE1 – A temporary work area where

11

� TEMPSPACE1 – A temporary work area where

DB2 can place intermediate results.

� USERSPACE1 – A place where all user objects

(tables, indexes) reside by default.

� Configuration information (DB CFG)

� Log files, History files etc.

Page 12: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Cataloging

�DB2 automatically catalogs databases when they are

created. It catalogs an entry for the DB in the local database

directory and another entry in the system database directory.

�Without catalog information, an application cannot connect to a

DB. DB2 has multiple directories that are used to access DBs.

�The database directory contains a list and pointer to where

12

�The database directory contains a list and pointer to where

all the known databases can be found. (list db directory)

�The node directory contains information relating to how and

where remote instances can be found. (list node directory)

�The Database Connection Services (DCS) Directory

contains information relating to how and where databases on

DRDA systems can be found. (list dcs directory)

Page 13: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

3 ways of cataloging a database at a client�Automated configuration using discovery. This method is normally used to configure small numbers of clients.

�Search discovery- Client searches for servers on the network

�Known discovery- one particular server is queried for information about the instances and their databases

�Automated configuration using access profiles. This

13

�Automated configuration using access profiles. This method is used to configure a large number of clients.

�Server profiles- created from DB2 servers, contains information about all the instances and databases of server

�Client profiles- used to duplicat the cataloged databases and/or the client settings from one client to another

�Manual configuration used to exploit some advanced options not available using automation

Page 14: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Get connected using Configuration Assistant

14

Page 15: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Search the Network (1)

15

Page 16: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Search the Network (2)

16

Page 17: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Search the Network (3)

17Click

Page 18: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Search the Network (4)

Enter User ID and

Password through

18

Click

Password through

which you want to

connect and click

Test Connection

Result tab will show

connection results.

Page 19: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant – Manually (1)

19

Enter IP address or

Machine Name Default port on

Windows : 50000

Click

Page 20: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Manually (2)

Choose protocol

(e.g. TCP/IP)

20

Click

Page 21: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant – Manually (3)

21

Enter Database name

Click

Enter : Alias of database

which don't exist on client

Page 22: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant – Manually (4)

22

Click

Page 23: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Manually (5)

23

Click

Page 24: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant – Manually (6)

24Click

Page 25: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Configuration Assistant - Manually (7)

Enter User ID and

Password through

25

Click

Password through

which you want to

connect and click

Test Connection

Result tab will show

connection results.

Page 26: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Client/Server Connectivity using CLP

�To illustrate connectivity ,we will use a scenario:

� INSTANCE is DB2

�DATABASE is SAMPLE

� IP ADDRESS is 9.186.128.141

�Settings on the server:

26

�Value of DB2COMM is tcpip

�Services file contains valid TCP/IP port number , in our case

default is 50000

�Windows client machine:

�Catalog the server

�Catalog the database

Page 27: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

CLP - Cataloging the Server and Database

�Cataloging a server:

�CATALOG TCPIP NODE db2server

REMOTE 9.186.128.141 SERVER 50000

�Cataloging a database:

�CATALOG DATABASE sample AS srv_samp

27

�CATALOG DATABASE sample AS srv_samp

AT NODE db2server

�UNCATALOG DATABASE sample

�Cataloging a DCS database (DRDA Server)

�CATALOG DCS DATABASE test_db AS dsn_db

WITH "DB2 z/OS database"

Page 28: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects - Tables

�A Table is an unordered set of rows. Rows consist of columns.

Each column is based on a data type. Types of tables can be:

�Base tables: User-defined tables designed to hold persistent

user data, created with the CREATE TABLE SQL statement.

�Temporary (Derived) tables: Tables populated with rows

retrieved from one or more base tables in response to a query.

28

retrieved from one or more base tables in response to a query.

�Temporary (Declared) tables: User-defined tables used to

hold non-persistent data temporarily, on behalf of a single

application. These tables are explicitly created by an

application when they are needed and implicitly destroyed

when the application terminates its last database connection.

Declared temporary table is created with the DECLARE

GLOBAL TEMPORARY TABLE statement.

Page 29: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects - Views

�Views can be seen as virtual tables derived from one

or more tables or views and

�Created to limit access to sensitive data while allowing

more general access to other data.

�Views do not contain real data.

�Can be deletable, updatable, insertable, and read-only.

29

�Can be deletable, updatable, insertable, and read-only.

�When changes are made to data through a view, the data

is changed in underlying table itself.

�Can be used interchangeably with tables when retrieving

data.

�CREATE VIEW dept_view AS

SELECT * FROM department

Page 30: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects – Aliases & Nicknames

�An alias is simply an alternate name for a table or

view.

�Like tables and views, an alias can be created,

dropped, and have comments associated with it.

�Aliases can also be created for nicknames that refer

30

�Aliases can also be created for nicknames that refer

to tables or views found on federated systems.

�Aliases are publicly referenced names, so no special

authority or privilege is required to use them.

�CREATE ALIAS empinfo FOR employees

Page 31: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects – Sequences

�A sequence is an object that is used to generate data values

automatically. Unlike an identity column, a sequence is not tied

to any specific column or any specific table.

�Values generated can be any exact numeric data type that

has a scale of zero (SMALLINT, BIGINT, INTEGER, or

DECIMAL).The default increment value is 1.

31

DECIMAL).The default increment value is 1.

�The PREVIOUS VALUE expression returns the most recently

generated value for the specified sequence, while the NEXT

VALUE expression returns the next sequence value.

�CREATE SEQUENCE emp_id START WITH 100

INCREMENT BY 10 CACHE 5

Page 32: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects – Triggers

�A trigger is used to define a set of actions that are to be executed whenever an insert, update, or delete operation is performed against a table or updatable view.

�Like constraints, triggers are often used to enforce data integrity and business rules.

�Unlike constraints, triggers can also be used to update other

32

�Unlike constraints, triggers can also be used to update other tables, automatically generate or transform values for inserted or updated rows, and invoke functions to perform tasks such as issuing errors or alerts.

�Ex. CREATE TRIGGER pay_raise BEFORE UPDATE ON employees FOR EACH ROW SET new.salary = salary * 1.1

Page 33: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects – Stored Procedures

�An SQL stored procedure is composed entirely of SQL

statements. It is created by executing one form of the CREATE

PROCEDURE SQL statement.

�When an SQL stored procedure is used to implement a

business rule, the logic can be incorporated into any

application simply by invoking the stored procedure.

33

application simply by invoking the stored procedure.

�An external stored procedure is a stored procedure that is

written using a high-level programming language such as C,

C++, Java, or COBOL.

�External stored procedures can be more powerful than SQL

stored procedures because they can take advantage of system

calls and administrative APIs along with SQL statements.

Page 34: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Calling a Stored Procedure

CREATE PROCEDURE get_sales (IN quota INTEGER, OUT retcode CHAR(5)) DYNAMIC RESULT SETS 1 LANGUAGE SQL

BEGIN

DECLARE sqlstate CHAR(5); DECLARE sales_results CURSOR WITH RETURN FOR SELECT sales_person, SUM(sales) AS total_sales

34

SELECT sales_person, SUM(sales) AS total_sales FROM sales GROUP BY sales_person

HAVING SUM(sales) > quota; DECLARE EXIT HANDLER FOR SQLEXCEPTION SET retcode = sqlstate; OPEN sales_results; SET retcode = sqlstate;

END

�CALL get_sales (25, ?)

Page 35: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Database Objects – UDFs

�User-defined functions (UDFs) are special objects that are

used to extend and enhance the support provided by the built-

in functions available with DB2 9.

�Unlike DB2's built-in functions, user-defined functions can take

advantage of system calls and DB2's administrative APIs.

�User-defined functions are created (or registered) by executing

35

�User-defined functions are created (or registered) by executing

the CREATE FUNCTION SQL statement.

�SQL Scalar, Table, or Row. constructed using only SQL

statements and can return a value, row or table.

�External Scalar/Table written using a high-level programming

language such as C, C++, or Java and returns a single value

or table.

Page 36: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Invoking User-Defined Functions

CREATE FUNCTION jobemployees (job VARCHAR(8))

RETURNS TABLE (empno CHAR(6),

firstname VARCHAR(12), lastname VARCHAR(15))

LANGUAGE SQL READS SQL DATA

NO EXTERNAL ACTION DETERMINISTIC

RETURN SELECT empno, firstnme, lastname

36

RETURN SELECT empno, firstnme, lastname

FROM employee

WHERE employee.job = jobemployees.job;

Calling function -

�SELECT * FROM TABLE (jobemployees('CLERK'))

�SELECT temp AS tempf, convert_temp(temp, 'F') AS tempc FROM climate_info

Page 37: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

37

Page 38: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

1) While attempting to connect to a database stored on an iSeries

server from a Windows client, the following message was displayed:

�SQL1013N The database alias name or

database name "TEST_DB" could not be found.

�Which of the following actions can be used to help determine why

this message was displayed?

� A. Execute the LIST REMOTE DATABASES command on the

38

� A. Execute the LIST REMOTE DATABASES command on the

server; look for an entry for the TEST_DB database

� B. Execute the LIST DCS DIRECTORY command on the server;

look for an entry for the TEST_DB database

� C. Execute the LIST REMOTE DATABASES command on the client;

look for an entry for the TEST_DB database

� D. Execute the LIST DCS DIRECTORY command on the client; look

for an entry for the TEST_DB database

Page 39: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

2) A database named TEST_DB resides on a z/OS system and listens on

port 446. The TCP/IP address for this system is 192.168.10.20 and the

TCP/IP host name is MYHOST. Which of the following commands is

required to make this database accessible to a Linux client?

� A. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER

192.168.10.20;CATALOG DATABASE zos_db AS test_db AT NODE

zos_srvr;CATALOG DCS DATABASE zos_db AS test_db;

� B. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER

39

� B. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER

192.168.10.20;CATALOG DCS DATABASE zos_db AS test_db AT

NODE zos_srvr;

� C. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER

446;CATALOG DCS DATABASE zos_db AS test_db AT NODE

zos_srvr;

� D. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER

446;CATALOG DATABASE zos_db AS test_db AT NODE

zos_srvr;CATALOG DCS DATABASE zos_db AS test_db;

Page 40: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

3) In which of the following scenarios would a stored procedure

be beneficial?

�A. An application running on a remote client needs to be able

to convert degrees Celsius to degrees Fahrenheit and vice

versa

�B. An application running on a remote client needs to collect

three input values, perform a calculation using the values

40

three input values, perform a calculation using the values

provided, and store the input data, along with the results of the

calculation in two different base tables

�C. An application running on a remote client needs to track

every modification made to a table that contains sensitive data

�D. An application running on a remote client needs to ensure

that every new employee that joins the company is assigned a

unique, sequential employee number

Page 41: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

4) If the following SQL statements are executed in the order shown:

CREATE TABLE orders (order_num INTEGER NOT NULL,

Buyer_name VARCHAR(35), Amount NUMERIC(5,2));

CREATE UNIQUE INDEX idx_orderno ON orders(order_num);

Which of the following describes the resulting behavior?

� A. Every ORDER_NUM value entered must be unique; whenever the

ORDERS table is queried rows should be displayed in order of

increasing ORDER_NUM values

41

increasing ORDER_NUM values

� B. Every ORDER_NUM value entered must be unique; whenever the

ORDERS table is queried rows will be displayed in no particular order

� C. Duplicate ORDER_NUM values are allowed; no other index can be

created for the ORDERS table that reference the ORDER_NUM column

� D. Every ORDER_NUM value entered must be unique; no other index

can be created for the ORDERS table that reference the ORDER_NUM

column

Page 42: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

5) An alias can be an alternate name for which

two of the following DB2 objects?

�A. Sequence

�B. Trigger

42

B. Trigger

�C. View

�D. Schema

�E. Table

Page 43: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

6) Which of the following DB2 objects can be

referenced by an INSERT statement to

generate values for a column?

�A. Sequence

43

�B. Identity column

�C. Trigger

�D. Table function

Page 44: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

7) Which of the following is NOT an attribute of

Declared Global Temporary Tables (DGTTs)?

�A. Each application that defines a DGTT has its own

instance of the DGTT

�B. Two different applications cannot create DGTTs

that have the same name

44

that have the same name

�C. DGTTs can only be used by the application that

creates them, and only for the life of the application

�D. Data stored in a DGTT can exist across transaction

boundaries

Page 45: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

8) Which of the following is an accurate statement

about packages?

�A. Packages provide a logical grouping of database

objects.

�B. Packages contain control structures that are

considered the bound form for SQL statements

45

considered the bound form for SQL statements

�C. Packages describe the objects in a DB2 database

and their relationship to each other

�D. Packages may be used during query optimization

to improve the performance for a subset of SELECT

queries

Page 46: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

9) Which of the following events will NOT cause

a trigger to be activated?

�A. A select operation

�B. An insert operation

46

�B. An insert operation

�C. An update operation

�D. A delete operation

Page 47: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

10) Which of the following DB2 objects is NOT

considered executable using SQL?

�A. Routine

�B. Function

47

�B. Function

�C. Procedure

�D. Trigger

Page 48: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

11) Which of the following is NOT an accurate

statement about views?

�A. Views are publicly referenced names and no

special authority or privilege is needed to use them.

�B. Views can be used to restrict access to columns in

48

�B. Views can be used to restrict access to columns in

a base table that contain sensitive data

�C. Views can be used to store queries that multiple

applications execute on a regular basis in a database

�D. Views support INSTEAD OF triggers

Page 49: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

12) Which of the following SQL statements can be

used to create a DB2 object to store numerical data

as EURO data?

�A. CREATE NICKNAME euro FOR DECIMAL (9,3)

�B. CREATE ALIAS euro FOR DECIMAL (9,3)

49

�B. CREATE ALIAS euro FOR DECIMAL (9,3)

�C. CREATE DISTINCT TYPE euro AS DECIMAL

(9,3)

�D. CREATE DATA TYPE euro AS DECIMAL (9,3)

Page 50: 3) Working With Databases and Database Objects Ppt

IBM DB2 9

Hebrew

Thank You

Merci

Russian

GrazieItalian

GraciasSpanish

ObrigadoPortuguese

Arabic

50

Japanese

Thank You

English

MerciFrench

DankeGerman

Simplified Chinese

Traditional Chinese

Tamil

Thai

Korean