Presentation is loading. Please wait.

Presentation is loading. Please wait.

® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 2: The DB2 Environment.

Similar presentations


Presentation on theme: "® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 2: The DB2 Environment."— Presentation transcript:

1 ® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 2: The DB2 Environment

2 IBM Software Group Agenda  Introduction  Instances  Databases  The DAS  DB2 Configuration

3 IBM Software Group DB2: The big picture

4 IBM Software Group The DB2 Environment

5 IBM Software Group Creating an instance  Windows (as Admin):  db2icrt myinst  Linux (as root)  /home/db2inst1/instance>./db2icrt -u db2fenc2 db2inst2  db2fenc2 and db2inst2 must exist before hand.  To create an instance using the GUI:  /home/db2inst1/instance>./db2isetup  In this case, the user IDs are created for you.  Note if you run as non-root, you can only see the current configuration of existing instances.

6 IBM Software Group Switching instances  Windows (as Admin)  set DB2INSTANCE=  Linux  su –  An instance maps to a Linux user  The db2profile file is normally added to the.profile or.login files when the instance was created.  The db2profile file has a line for DB2INSTANCE=

7 IBM Software Group Reviewing instance information  List all instances on the server:  db2ilist  Showing the current instance:  db2 get instance

8 IBM Software Group Starting and stopping instance  db2start  db2stop [force]

9 IBM Software Group Dropping an instance  Windows (as local Admin)  db2stop  db2idrop myinst  Linux  db2stop (as the instance owner, eg: db2inst1)  /opt/ibm/db2/V9.5/instance>./db2idrop db2inst1 (as root)

10 IBM Software Group The DB2 Environment

11 IBM Software Group Creating and connecting to a Database CREATE DATABASE mydb1 [AS dbalias] [ON drive (On Windows) / path (On Linux)]  NOTE:  If the database alias (dbalias) is not provided, it will use the same as the database name CONNECT TO dbalias [user userID using password]  NOTE:  userId/password are required for remote connections

12 IBM Software Group The DB2 Administration Server (DAS)  Must be running to allow for remote GUI administration of a DB2server, other than that, not needed  Only one DAS per server  DAS created at installation.  On Windows it’s called: DB2DAS00  On Linux it’s called: dasusr1  On Linux dasusr1 maps to a Linux user

13 IBM Software Group DAS commands  Creating/dropping the DAS Windows (As admin):  db2admin create  db2admin drop Linux (As root)  /opt/ibm/db2/V9.5>./dascrt –u  /opt/ibm/db2/V9.5>./dasdrop  Starting/stopping the DAS (as the DAS user)  db2admin start  db2admin stop

14 IBM Software Group DB2 data server big picture  One physical machine has:  One Admin Server  One or multiple Instances  Each Instance (Database Manager):  Is a logical server context  Can have one or more databases

15 IBM Software Group DB2 Tools Start  Programs  IBM DB2 ...

16 IBM Software Group Control Center  The main DB2 administration tool  The Control Center allows you to:  create, modify and manage databases  Manage database objects (tables, indexes, etc.)  Launch the other GUI tools

17 IBM Software Group Launching Control Center for the First Time  The first time Control Center is launched, you are asked to choose what view you would like to use.  The choice of view determines what types of options and database objects are exposed

18 IBM Software Group Launching the Control Center  Navigating through the Windows Start menu  By executing db2cc on a command prompt  By clicking the Control Center icon in the toolbar of any of the other DB2 GUI tools  From the DB2 icon in the Windows system tray

19 IBM Software Group Control Center

20 IBM Software Group Changing to Control Center’s Advanced View  Control Center > Tools (menu) > Customize Control Center > (select) Advanced

21 IBM Software Group Advanced Control Center View

22 IBM Software Group Command Editor  Using Command Editor, you can execute DB2 commands and SQL, analyze the execution plan of an SQL statement, and view/update query result sets  You can launch Command Editor several ways:  As a standalone application  Navigate through the Windows Start Menu  START -> PROGRAMS -> IBM DB2 -> COMMAND LINE TOOLS ->  COMMAND EDITOR  From a command prompt, type db2ce  From the Control Center, click the Command Editor icon on the Control Center Toolbar  From the Tools menu in Control Center

23 IBM Software Group Command Editor Add (database connection) Dialog

24 IBM Software Group Command Editor – Commands Tab

25 IBM Software Group The SQL Assist Wizard

26 IBM Software Group SQL Assist Wizard

27 IBM Software Group Show SQL Button  Most of the GUI tools/wizards allow you to see the actual command or  SQL statement that is created as a result of using the tool/wizard to perform an action This is very handy for: Learning SQL syntax Saving the statement to a file (or the Windows clipboard) for later use can be later imported directly into Command Editor

28 IBM Software Group Command Window  Only on Windows. On Linux use the Linux shell  Can be started with “db2cmd”

29 IBM Software Group Task Center

30 IBM Software Group Task Center – No Tools Catalog Warning Dialog  Task Center requires that a tools catalog be created  This catalog stores all the meta data and information about scheduled tasks  If you did not create the tools catalog when the database was created, an error message dialog will be displayed like the one below when Task Center is launched.  In order to schedule tasks, you must first create the tools catalog (you can do this when you schedule the first task)

31 IBM Software Group Scheduling With Task Center  Any type of script can be scheduled using Task Center (whether or not it was created through a DB2 GUI tool)  The advantage of creating tasks through a DB2 GUI tool is that they can be edited again later using the original GUI tool used to create them  Tasks are run at their scheduled time from the system where you created the tools catalog, BUT run on the system where the database exists  No dependencies on the client where the task was created  View previous executions of your tasks in the Journal

32 IBM Software Group A Basic SQL Script  Suppose the following commands are saved in a file called script1.db2 CONNECT TO EXPRESS; CREATE TABLE user1.mytable (col1 INTEGER NOT NULL, col2 VARCHAR(40), col3 DECIMAL(9,2)); SELECT * FROM user1.mytable FETCH FIRST 10 ROWS ONLY; COMMIT;

33 IBM Software Group Executing SQL Scripts  An SQL script can be executed from Command Editor or the operating system command line  To run the previous script from the command line (DB2 Command Window), you would use the following command: db2 –t –v –f script1.db2 –z script1.log  -t indicates statements use the default statement termination character (semicolon)  -v indicates verbose mode; causes db2 to output the command being executed  -f indicates the following filename contains the SQL statements  -z indicates the following message filename should be used for appending screen output for later analysis (optional, but recommended)  Note: It is a good idea to delete these message files before the execution of DB2 scripts so that output from a previous script execution is not mixed with output from the current script execution

34 IBM Software Group When a different statement termination character is needed  A script containing SQL PL code needs to use a different statement termination character other than the default (semicolon)  This is due to the fact that semicolons are used within SQL PL object definitions to terminate procedural statements  Delimit the end of each SQL PL application object with a different statement termination character  e.g. CREATE FUNCTION f1() …. END! CREATE FUNCTION f1() … END! When executing the script, inform DB2 that a different statement termination character is being used: db2 –td! –v –f functions.db2 –z functions.log

35 IBM Software Group A Simple Operating System (Shell) Script  Suppose the following statements are saved in a file called create_database.bat  To execute this script from the command line, you would issue the following command: create_database.bat db2admin ibmdb2 set DBPATH=c: set DBNAME=PRODEXPR set MEMORY=25 db2 CREATE DATABASE %DBNAME% ON %DBPATH% AUTOCONFIGURE USING MEM_PERCENT %MEMORY% APPLY DB AND DBM db2 CONNECT TO %DBNAME% USER %1 USING %2 del schema.log triggers.log app_objects.log db2 set schema user1 db2 –t –v –f schema.db2 –z schema.log db2 -v –f triggers.db2 –z triggers.log db2 -v –f functions.db2 –z functions.log

36 IBM Software Group Tables

37 IBM Software Group Data Types  DB2 Data Types

38 IBM Software Group Large Objects  To store large character strings or files  To store large binary strings or files

39 IBM Software Group User-Defined Types CREATE DISTINCT TYPE POUND AS INTEGER WITH COMPARISONS CREATE DISTINCT TYPE KILOGRAM AS INTEGER WITH COMPARISONS CREATE TABLE person (f_name varchar(30), weight_p POUND NOT NULL, weight_k KILOGRAM NOT NULL ) SELECT F_NAME FROM PERSON WHERE weight_p > POUND(30) SELECT F_NAME FROM PERSON WHERE weight_p > weight_k  Where fails ?

40 IBM Software Group Null Values  A null value represents an unknown state  The CREATE TABLE statement can contain the phrase NOT NULL following the definition of each column.  This will ensure that the column contains a known data value.  Can specify a default value if NOT NULL is entered CREATE TABLE Staff ( ID SMALLINT NOT NULL, NAME VARCHAR(9), DEPT SMALLINT not null with default 10, JOB CHAR(5), YEARS SMALLINT, SALARY DECIMAL(7,2), COMM DECIMAL(7,2) with default 15 )

41 IBM Software Group Views  Data for view not stored separately  Nested view supported  View information kept in: SYSCAT.VIEWS, SYSCAT.VIEWDEP, SYSCAT.TABLES

42 IBM Software Group Indexes  Index Characteristics:  ascending or descending  Unique or non-unique  compound  cluster  bi-directional (default behavior))  Examples: create unique index artno_ix on artists (artno)


Download ppt "® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 2: The DB2 Environment."

Similar presentations


Ads by Google