Вы находитесь на странице: 1из 14

IBM DB2 9

Section 3 - Working with Databases and


Database Objects (17%)
IBM DB2 9

 Ability to identify and connect to DB2 servers


Section -3) Working with and databases
Databases and Database Objects
 Ability to identify DB2 objects
 Knowledge of basic characteristics and
properties of DB2 objects
 Given a DDL SQL statement, ability to identify
results (ability to create DB2 objects)
© 2008 IBM Corporation
2

IBM DB2 9 IBM DB2 9

Servers, Instances, and Databases More on instances


 During the installation process, program files for a background  The default instance for a particular system is defined by the
process known as the DB2 Database Manager are physically DB2INSTANCE environment variable. Initially for Windows
copied to a specific location on the server. default is ‘DB2’ and for Linux/Unix it is ‘db2inst1’.
 DB2 9 Instance Management Commands:

Command Purpose
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
 A DB2 instance is a logical database server environment.
db2stop Stops the DB2 Database Manager background
There can be multiple DB2 instances on a server. Multiple
processes for the current instance
Databases can be created within a DB2 instance.
3 4

1
IBM DB2 9 IBM DB2 9

DB2 Storage Model 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
- 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
5 6

IBM DB2 9 IBM DB2 9

Tablespaces Types of Tablespaces


 A tablespace is a logical layer between data and storage  System-Managed Space (SMS):
devices. - OS’s file system manager allocates and manages the space.
- Consists of one or more containers.  Database-Managed Space (DMS):
- Tables, regular data, indexes, and long data reside in - Here, the database manager controls the storage space.
tablespaces  Automatic Storage With DMS:
 SQL: CREATE / DROP / ALTER TABLESPACE - Databases that are enabled for automatic storage have a set
 To display Table space info: of one or more storage paths associated with them.
- LIST TABLESPACES [SHOW DETAIL] - A table space can be defined as "managed by automatic
 Details about containers can be obtained with following storage" and its containers assigned and allocated by DB2
command. based on those storage paths.
- You cannot enable/disable automatic storage once it is
- LIST TABLESPACE CONTAINERS FOR tablespace_id
defined for database.
[SHOW DETAIL]
- Default type in DB2 9.
7 8

2
IBM DB2 9 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).
 Some database commands -
CREATE DATABASE To create a database. (Database name
<dbname> 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
9 10

IBM DB2 9 IBM DB2 9

With database, DB2 automatically creates Cataloging


 Three table spaces are created.  DB2 automatically catalogs databases when they are
created. It catalogs an entry for the DB in the local database
- SYSCATSPACE - This is where the DB2 system directory and another entry in the system database directory.
catalog is kept that tracks all of the metadata
 Without catalog information, an application cannot connect to a
associated with DB2 objects. DB. DB2 has multiple directories that are used to access DBs.
- TEMPSPACE1 – A temporary work area where - The database directory contains a list and pointer to where
DB2 can place intermediate results. all the known databases can be found. (list db directory)
- USERSPACE1 – A place where all user objects - The node directory contains information relating to how and
(tables, indexes) reside by default. where remote instances can be found. (list node directory)
 Configuration information (DB CFG) - The Database Connection Services (DCS) Directory
contains information relating to how and where databases on
 Log files, History files etc. DRDA systems can be found. (list dcs directory)
11 12

3
IBM DB2 9 IBM DB2 9

3 ways of cataloging a database at a client Get connected using Configuration Assistant


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

IBM DB2 9 IBM DB2 9

Configuration Assistant - Search the Network (1) Configuration Assistant - Search the Network (2)

15 16

4
IBM DB2 9 IBM DB2 9

Configuration Assistant - Search the Network (3) Configuration Assistant - Search the Network (4)

Enter User ID and


Password through
which you want to
connect and click
Test Connection

Result tab will show


connection results.
Click
17
Click 18

IBM DB2 9 IBM DB2 9

Configuration Assistant – Manually (1) Configuration Assistant - Manually (2)

Choose protocol
(e.g. TCP/IP)

Enter IP address or
Default port on Machine Name
Windows : 50000

19 20
Click Click

5
IBM DB2 9 IBM DB2 9

Configuration Assistant – Manually (3) Configuration Assistant – Manually (4)

Enter : Alias of database


which don't exist on client Enter Database name

21 22
Click Click

IBM DB2 9 IBM DB2 9

Configuration Assistant - Manually (5) Configuration Assistant – Manually (6)

23 24
Click Click

6
IBM DB2 9 IBM DB2 9

Configuration Assistant - Manually (7) 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
Enter User ID and  Settings on the server:
Password through - Value of DB2COMM is tcpip
which you want to
connect and click - Services file contains valid TCP/IP port number , in our case
Test Connection default is 50000
 Windows client machine:
Result tab will show - Catalog the server
connection results.
- Catalog the database
Click
25 26

IBM DB2 9 IBM DB2 9

CLP - Cataloging the Server and Database Database Objects - Schemas


 Cataloging a server:  Schemas are objects that are used to logically classify and
- CATALOG TCPIP NODE db2server group other objects in the database. Schemas have privileges
associated with them that allow the schema owner to control
REMOTE 9.186.128.141 SERVER 50000
which users can create, alter, and drop objects within them.
 Cataloging a database:  When an object is created without specifying a schema, object
- CATALOG DATABASE sample AS srv_samp will be associated with an implicit schema using the
AT NODE db2server authorization ID.
- UNCATALOG DATABASE sample  Most database objects are named using a two-part naming
convention (SCHEMA_NAME.OBJECT_NAME).
 Cataloging a DCS database (DRDA Server)
 The CURRENT SCHEMA special register contains the default
- CATALOG DCS DATABASE test_db AS dsn_db qualifier to be used for unqualified objects.
WITH "DB2 z/OS database"  CREATE SCHEMA inventory

27 28

7
IBM DB2 9 IBM DB2 9

Database Objects - Tables Database Objects - Views


 A Table is an unordered set of rows. Rows consist of columns. Views can be seen as virtual tables derived from one
Each column is based on a data type. Types of tables can be: or more tables or views and
 Base tables: User-defined tables designed to hold persistent
- Created to limit access to sensitive data while allowing
user data, created with the CREATE TABLE SQL statement.
more general access to other data.
 Temporary (Derived) tables: Tables populated with rows
- Views do not contain real data.
retrieved from one or more base tables in response to a query.
- Can be deletable, updatable, insertable, and read-only.
 Temporary (Declared) tables: User-defined tables used to
hold non-persistent data temporarily, on behalf of a single - When changes are made to data through a view, the data
application. These tables are explicitly created by an is changed in underlying table itself.
application when they are needed and implicitly destroyed - Can be used interchangeably with tables when retrieving
when the application terminates its last database connection. data.
Declared temporary table is created with the DECLARE CREATE VIEW dept_view AS
GLOBAL TEMPORARY TABLE statement. SELECT * FROM department
29 30

IBM DB2 9 IBM DB2 9

Database Objects - Indexes Database Objects – Aliases & Nicknames


An index is an object that contains an ordered set of An alias is simply an alternate name for a table or
pointers that refer to rows in a base table, based upon view.
one or more columns but stored as a separate entity. Like tables and views, an alias can be created,
Indexes are used to enforce uniqueness in a table dropped, and have comments associated with it.
(that is, to make sure that there are no duplicate Aliases can also be created for nicknames that refer
values) and to improve performance when retrieving to tables or views found on federated systems.
information. Aliases are publicly referenced names, so no special
Indexes are maintained automatically by DB2 as authority or privilege is required to use them.
data is inserted, updated, and deleted.
 CREATE UNIQUE INDEX empno_indx ON CREATE ALIAS empinfo FOR employees
employees(empno)
31 32

8
IBM DB2 9 IBM DB2 9

Database Objects – Sequences Database Objects – Triggers


 A sequence is an object that is used to generate data values  A trigger is used to define a set of actions that are to be
automatically. Unlike an identity column, a sequence is not tied executed whenever an insert, update, or delete operation is
to any specific column or any specific table. performed against a table or updatable view.
 Like constraints, triggers are often used to enforce data
 Values generated can be any exact numeric data type that
integrity and business rules.
has a scale of zero (SMALLINT, BIGINT, INTEGER, or
 Unlike constraints, triggers can also be used to update other
DECIMAL).The default increment value is 1.
tables, automatically generate or transform values for inserted
 The PREVIOUS VALUE expression returns the most recently or updated rows, and invoke functions to perform tasks such
generated value for the specified sequence, while the NEXT as issuing errors or alerts.
VALUE expression returns the next sequence value. Ex. CREATE TRIGGER pay_raise
BEFORE UPDATE ON employees
CREATE SEQUENCE emp_id START WITH 100 FOR EACH ROW
INCREMENT BY 10 CACHE 5 SET new.salary = salary * 1.1
33 34

IBM DB2 9 IBM DB2 9

Database Objects – Stored Procedures Calling a Stored Procedure


 An SQL stored procedure is composed entirely of SQL CREATE PROCEDURE get_sales
statements. It is created by executing one form of the CREATE (IN quota INTEGER, OUT retcode CHAR(5))
PROCEDURE SQL statement. DYNAMIC RESULT SETS 1 LANGUAGE SQL
 When an SQL stored procedure is used to implement a BEGIN
business rule, the logic can be incorporated into any DECLARE sqlstate CHAR(5);
application simply by invoking the stored procedure. DECLARE sales_results CURSOR WITH RETURN FOR
SELECT sales_person, SUM(sales) AS total_sales
 An external stored procedure is a stored procedure that is FROM sales GROUP BY sales_person
written using a high-level programming language such as C, HAVING SUM(sales) > quota;
C++, Java, or COBOL. DECLARE EXIT HANDLER FOR SQLEXCEPTION
 External stored procedures can be more powerful than SQL SET retcode = sqlstate; OPEN sales_results;
stored procedures because they can take advantage of system SET retcode = sqlstate;
calls and administrative APIs along with SQL statements. END
 CALL get_sales (25, ?)
35 36

9
IBM DB2 9 IBM DB2 9

Database Objects – Packages Database Objects – UDFs


 A package is an object that contains information needed to process SQL  User-defined functions (UDFs) are special objects that are
statements associated with a source code file of an application program. used to extend and enhance the support provided by the built-
 During the pre-compile process, in functions available with DB2 9.
- a source code file containing embedded SQL statements is converted
into a source code file that is made up entirely of high-level programming  Unlike DB2's built-in functions, user-defined functions can take
language statements. advantage of system calls and DB2's administrative APIs.
- At the same time, a corresponding package that contains the access  User-defined functions are created (or registered) by executing
plans that will be used to process each SQL statement embedded in the the CREATE FUNCTION SQL statement.
source code file is also produced.
 SQL Scalar, Table, or Row. constructed using only SQL
- This package must reside in a DB2 database that contains the data
objects referenced by the package before the corresponding application statements and can return a value, row or table.
can be executed against that database.  External Scalar/Table written using a high-level programming
 The process of creating and storing a package in a DB2 database is known language such as C, C++, or Java and returns a single value
as binding, and by default, packages are automatically bound to a or table.
database during the precompile process.
37 38

IBM DB2 9 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
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
39 40

10
IBM DB2 9 IBM DB2 9

1) While attempting to connect to a database stored on an iSeries 2) A database named TEST_DB resides on a z/OS system and listens on
server from a Windows client, the following message was displayed: 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
 SQL1013N The database alias name or required to make this database accessible to a Linux client?
database name "TEST_DB" could not be found.  A. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER
 Which of the following actions can be used to help determine why 192.168.10.20;CATALOG DATABASE zos_db AS test_db AT NODE
this message was displayed? zos_srvr;CATALOG DCS DATABASE zos_db AS test_db;
 B. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER
 A. Execute the LIST REMOTE DATABASES command on the
192.168.10.20;CATALOG DCS DATABASE zos_db AS test_db AT
server; look for an entry for the TEST_DB database
NODE zos_srvr;
 B. Execute the LIST DCS DIRECTORY command on the server;
 C. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER
look for an entry for the TEST_DB database
446;CATALOG DCS DATABASE zos_db AS test_db AT NODE
 C. Execute the LIST REMOTE DATABASES command on the client; zos_srvr;
look for an entry for the TEST_DB database  D. CATALOG TCPIP NODE zos_srvr REMOTE myhost SERVER
 D. Execute the LIST DCS DIRECTORY command on the client; look 446;CATALOG DATABASE zos_db AS test_db AT NODE
for an entry for the TEST_DB database zos_srvr;CATALOG DCS DATABASE zos_db AS test_db;
41 42

IBM DB2 9 IBM DB2 9

3) In which of the following scenarios would a stored procedure 4) If the following SQL statements are executed in the order shown:
be beneficial? CREATE TABLE orders (order_num INTEGER NOT NULL,
Buyer_name VARCHAR(35), Amount NUMERIC(5,2));
 A. An application running on a remote client needs to be able
to convert degrees Celsius to degrees Fahrenheit and vice CREATE UNIQUE INDEX idx_orderno ON orders(order_num);
versa Which of the following describes the resulting behavior?
 A. Every ORDER_NUM value entered must be unique; whenever the
 B. An application running on a remote client needs to collect
ORDERS table is queried rows should be displayed in order of
three input values, perform a calculation using the values increasing ORDER_NUM values
provided, and store the input data, along with the results of the  B. Every ORDER_NUM value entered must be unique; whenever the
calculation in two different base tables ORDERS table is queried rows will be displayed in no particular order
 C. An application running on a remote client needs to track  C. Duplicate ORDER_NUM values are allowed; no other index can be
every modification made to a table that contains sensitive data created for the ORDERS table that reference the ORDER_NUM column
 D. An application running on a remote client needs to ensure  D. Every ORDER_NUM value entered must be unique; no other index
that every new employee that joins the company is assigned a can be created for the ORDERS table that reference the ORDER_NUM
column
unique, sequential employee number
43 44

11
IBM DB2 9 IBM DB2 9

5) An alias can be an alternate name for which 6) Which of the following DB2 objects can be
two of the following DB2 objects? referenced by an INSERT statement to
generate values for a column?
- A. Sequence
- B. Trigger A. Sequence
- C. View B. Identity column
- D. Schema C. Trigger
- E. Table D. Table function

45 46

IBM DB2 9 IBM DB2 9

7) Which of the following is NOT an attribute of 8) Which of the following is an accurate statement
Declared Global Temporary Tables (DGTTs)? about packages?
A. Each application that defines a DGTT has its own A. Packages provide a logical grouping of database
instance of the DGTT objects.
B. Two different applications cannot create DGTTs B. Packages contain control structures that are
that have the same name considered the bound form for SQL statements
C. DGTTs can only be used by the application that C. Packages describe the objects in a DB2 database
creates them, and only for the life of the application and their relationship to each other
D. Data stored in a DGTT can exist across transaction D. Packages may be used during query optimization
boundaries to improve the performance for a subset of SELECT
queries

47 48

12
IBM DB2 9 IBM DB2 9

9) Which of the following events will NOT cause 10) Which of the following DB2 objects is NOT
a trigger to be activated? considered executable using SQL?

A. A select operation A. Routine


B. An insert operation B. Function
C. An update operation C. Procedure
D. A delete operation D. Trigger

49 50

IBM DB2 9 IBM DB2 9

11) Which of the following is NOT an accurate 12) Which of the following SQL statements can be
statement about views? used to create a DB2 object to store numerical data
as EURO data?
A. Views are publicly referenced names and no
special authority or privilege is needed to use them. A. CREATE NICKNAME euro FOR DECIMAL (9,3)
B. Views can be used to restrict access to columns in B. CREATE ALIAS euro FOR DECIMAL (9,3)
a base table that contain sensitive data C. CREATE DISTINCT TYPE euro AS DECIMAL
C. Views can be used to store queries that multiple (9,3)
applications execute on a regular basis in a database D. CREATE DATA TYPE euro AS DECIMAL (9,3)
D. Views support INSTEAD OF triggers

51 52

13
IBM DB2 9

Grazie Hebrew
Italian

Gracias
Spanish
Russian

Obrigado

Thank
Portuguese

Arabic

Merci
French

Traditional Chinese
You English
Danke
German

Thai
Simplified Chinese Tamil

Japanese Korean

53

14

Вам также может понравиться