+ All Categories
Home > Documents > IBM DB2 LUW/UDB: Database Creation

IBM DB2 LUW/UDB: Database Creation

Date post: 19-Dec-2015
Category:
Upload: nravi39763
View: 111 times
Download: 24 times
Share this document with a friend
Description:
By www.etraining.guru
Popular Tags:
41
DB2 for Linux, Unix, and Windows - Ravi
Transcript
  • DB2 for Linux, Unix, and Windows

    - Ravi

  • Database Creation

  • CREATE DATABASE $ db2 ? create database$ db2 create database TESTDB20000I The CREATE DATABASE command completed successfully.If no path is specified when a database is created, the DB is created under DFTDBPATH$ db2 list db directory System Database Directory

    Number of entries in the directory = 1

    Database 1 entry:

    Database alias = TEST Database name = TEST Local database directory = /home/db2inst1 Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

  • DFTDBPATH Default database path configuration parameterDB2 UPDATE DBM CFG USING DFTDBPATH /mnt/storage/DBPATH/If no path is specified when a database is created, the DB is created under DFTDBPATHDFTDBPATH update will take effect immediatelyNote: DFTDBPATH should exist locally on the box. dont give any NFS mounted paths.

  • CREATE DATABASE w/ Storage & dbpath $ db2 create database DB1 ON '/mnt/storage/storage_db1' DBPATH ON '/mnt/storage/DBPATHDB20000I The CREATE DATABASE command completed successfully.$ db2 list db directory System Database Directory

    Number of entries in the directory = 1

    Database 1 entry:

    Database alias = DB1 Database name = DB1 Local database directory = /mnt/storage/DBPATH Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

  • DB2 Objects Location after default ROOT installation (10.5)

    DB2 ObjectSCOPELocationUser: db2inst1Product is installed in : /opt/IBM/db2/v10.5 (INSTALLDIR)Instance directory: /home/db2inst1 (INSTHOME)DBPATH: /home/db2inst1Instance Home directoryInstance/home/db2inst1Instance InformationInstance/home/db2inst1/sqllibDBM Configuration FileInstanceINSTHOME/sqllib/db2systmDatabase Directory, which contains - Buffer pool, Storage path, tables space information - History information, Log control filesDatabase DBPATH/db2inst1/NODE0000/SQL00001DB Configuration FileDatabaseDBPATH/db2inst1/NODE00001/SQL00001/SQLDBCONDB2 CommandsProduct (or) InstanceINSTALLDIR/binDB2 Error message fileInstanceINSTHOME/sqllib/db2dump/db2diag.logDirectory for event monitor data (DB Specific)DatabaseDBPATH/db2inst1/NODE0000/SQL00001/db2event

  • SCOPEDirectory for transaction logs (DB Specific)DatabaseDBPATH/db2inst1/NODE0000/SQL00001/LOGSTREAM0000Installation log fileProduct (or) InstanceINSTALLDIR/install/logs/db2install.historyLocal database directory for instanceDatabaseDBPATH/db2inst1/NODE0000/sqldbdirSystem database directoryInstance INSTHOME/sqllib/sqldbdirNode directory for instanceInstanceINSTHOME/sqllib/sqlnodirPartitioned database environment fileInstanceINSTHOME/sqllib/db2nodes.cfg

  • System Database DirectoryContains information about all the databases to which we can connect from DB2 serverSystem db directory is stored at instance level (./sqllib/sqldbdir)db2 list db directoryNote: Indirect (local); remote(remote)To enter information into system database directory, use catalog command:db2 catalog db dbname as alias at node nodenameThe catalog commands are normally used only when adding information for remote databases. For local databases, a catalog entry is automatically created after creating the database with the create database command

  • Local Database DirectoryContains information about databases residing on the server where you are currently workingLocal DB directory doesnt reside inside the database, also doesnt reside at instance level. It is in a layer between two.db2 list db directory on drive/pathNo specific command to enter information into this directory, only to retrieve it. When we create a database with the CREATE DATABASE command, an entry is added to this directory.Location: / / /sqldbdir

  • Node DirectoryConnectivity information for remote database servers$ db2 list node directory$ db2 catalog tcpip node TCPFED remote 192.168.182.133 server 50000$ db2 list node directory

    Node Directory

    Number of entries in the directory = 1

    Node 1 entry:

    Node name = TCPFED Comment = Directory entry type = LOCAL Protocol = TCPIP Hostname = 192.168.182.133 Service name = 50000Location: $DB2_HOME/sqlnodir directory

  • Database Connection Services (DCS) DirectoryConnectivity information for host databases residing on System z (Z/OS or OS/390) or System i (OS/400) serverDB2 Connect Software is neededTo enter information in DCS directory, use db2 catalog dcs db as

    For Example: db2 catalog dcs db mydb as db1gLocation: $DB2_HOME/sqlnodir directoryTo list contents of a DCS directory: db2 list dcs directory

  • List active databasesYou can deactivate a database using deactivate command. For example:$db2 deactivate database DB1You can activate a database using activate command. For example:$db2 activate database DB1To list all active databases:

  • List active databases (contd )

  • List active databases (contd )

  • $ db2 connect to DB1

    Database Connection Information

    Database server = DB2/LINUXX8664 10.5.3 SQL authorization ID = DB2INST1 Local database alias = DB1$ db2 list tablespaces$ db2 list tablespaces show detail$ db2 ? list tablespaces

    LIST TABLESPACES [SHOW DETAIL]

  • [db2inst1@linuxdb2 ~]$ db2 list tablespaces

    Tablespaces for Current Database

    Tablespace ID = 0 Name = SYSCATSPACE Type = Database managed space Contents = All permanent data. Regular table space. State = 0x0000 Detailed explanation: Normal

    Tablespace ID = 1 Name = TEMPSPACE1 Type = System managed space Contents = System Temporary data State = 0x0000 Detailed explanation: Normal

    Tablespace ID = 2 Name = USERSPACE1 Type = Database managed space Contents = All permanent data. Large table space. State = 0x0000 Detailed explanation: Normal

    Tablespace ID = 3 Name = SYSTOOLSPACE Type = Database managed space Contents = All permanent data. Large table space. State = 0x0000 Detailed explanation: Normal

  • [db2inst1@linuxdb2 ~]$ db2 list tablespace containers for 2

  • Tablespaces (by space mangement)

  • $ db2 "create tablespace tbspace1 managed by system USING ('/mnt/storage/sai/tbspace1')

    $ db2 list tablespace containers for 4

    Tablespace Containers for Tablespace 4

    Container ID = 0 Name = /mnt/storage/sai/tbspace1 Type = PathTablespaces (on space management):System managed space (SMS):- OS manages- I/O is buffered by operating system- table space is automatically extended when needed- containers cant be dropped from SMS table spaces- Less maintenance & not optimal performance

  • Database managed space (DMS):- Either as files or as devices- DB2 manages I/O- Extend containers using ALTER TABLESPACE- Unused DMS containers can be released- Ideal for performance sensitive applications- Increased maintenance and monitoring$ db2 list tablespace containers for 2

    Tablespace Containers for Tablespace 2

    Container ID = 0 Name = /mnt/storage/storage_db1/db2inst1/NODE0000/DB1/T0000002/C0000000.LRG Type = File$ db2 CREATE TABLESPACE TBSPACE1 PAGESIZE 8K MANAGED BY DATABASEUSING ( FILE /mnt/storage/storage_db1/dms1.LRG 10 M )

  • Automatic managed space (AMS):- DBM creates and extends containers as and when needed- Automatically handles resizing table spaces- can be either DMS or SMS- DMS for regular/large table spaces- SMS for user or system temporary table spaces- New databases and table spaces use automatic storage by default

  • Tablespaces (by Usage)

  • Tablespaces (on usage):Regular: - To store regular tables and indexes. No temporary data- This type is allowed in both, SMS and DMS table spaces- Only type allowed for SMS table spaces- Example: SYSCATSPACELarge: - To store any kind of data except temporary- only DMS- Ex: USERSPACE1System temporary: Used to store temp data required by DB2. Ex: TEMPSPACE1User temporary: To store declared global temp tables. These are optional

  • DB2 CLP

  • DB2 Command Line Processor (CLP)

  • CLP SYNTAXFor example:db2 ?db2 ? command stringdb2 ? SQLnnnn (nnnn = 4 or 5 digit SQLCODE)db2 ? nnnnn (nnnnn = 5 digit SQLSTATE)

  • USING CLP

  • QUIT/TERMINATE/CONNECT RESET DIFFERENCES

  • CLP COMMAND OPTIONS

  • MODIFY CLP OPTIONS

  • Running SQL Files

  • DB2 CLP HISTORY

  • db2=> history

    db2=> history reverse

    db2=> h r

    db2=> runcmd 9

    db2=> r 9

    db2=> edit 9

    db2=> e 9

  • DB2 Catalog TablesCatalog tables are created under SYSIBM schemaSome catalog tables have parent-child relationship. For example, SYSIBM.SYSCOLAUTH is a child of SYSIBM.SYSTABAUTHFew examples:SYSIBM.SYSCOLAUTH: details column level privilegesSYSIBM.SYSTABAUTH: details table/view level privilegesSYSIBM.SYSDBAUTH: database level privilegesSYSIBM.SYSTABSPACEAUTH: tablespace level privilegesThe catalog tables can be classified into broad categories based on the type of data they store, for example:- Authorization: SYSDBAUTH, SYSTABSPACEAUTH, SYSTABAUTH, SYSCOLAUTH- Data type and routine: SYSDATATYPES, SYSROUTINES, SYSROUTINEPARMS- Constraint: SYSCHECKS, SYSRELS, SYSKEYCOLUSE- Dependency: SYSCONSTDEP, SYSDEPENDENCIES, SYSVIEWDEP- Storage management: SYSTABLESPACES, SYSTABLES- Database partition: SYSNODEGROUPS, SYSPARTITIONMAPS

  • On top of system catalog tables, database manager creates two sets of system catalog views.One set of read-only views is created under the SYSCAT schema, and a smaller set of updatable views is created under the SYSSTAT schemaSo,SYSIBM: All catalog tablesSYSCAT: Useful read only views of catalog tablesSYSSTAT: Updatable viewsdb2 list tables for schema SYSCATdb2 SELECT TABNAME from syscat.tables where tabschema = SYSCAT db2 describe table syscat.systablespacesdb2 describe table syscat.sysbufferpools

  • BUFFERPOOLS Area of main memory used to cache table and index dataEach database must have at least one buffer poolDefault: IBMDEFAULTBPBuffer pools can be created, dropped, or alteredQuery: SYSCAT.BUFFERPOOLSEvery table space must associate with the same buffer pool page sizeCREATE BUFFERPOOL bp8k PAGESIZE 8KCREATE TABLESPACE tbsp1 PAGESIZE 8K BUFFERPOOL bp8kTo get the buffer pool information: db2look -d -lA table space can only use one buffer poolSELECT BPNAME, BUFFERPOOLID, PAGESIZE FROM SYSCAT.BUFFERPOOLS

  • db2 SELECT tbspace, bufferpoolid FROM syscat.tablespacesdb2 describe table

  • DB2 STORAGE GROUPSA storage group is a named set of storage paths where data can be stored. Helps to manage storage automaticallyIBMSTOGROUP: Default storage group when you create a databaseAUTOMATIC STORAGE NO: - If we use this for database, then there is no default storage group- This clause is depreciated in DB2 10 and might be removed from future releasesCREATE STOGROUP sgroup1 ON /mnt/storage/storage1, /mnt/storage/storage2db2pd -db DB1 -storagegroups

  • *http://www.ibm.com/developerworks/data/library/techarticle/dm-0509poon/*http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/index.jsp?topic=%2Fcom.ibm.db2.luw.admin.perf.doc%2Fdoc%2Fc0008930.html

    DB creation needs two directories:One to store database related filesAnother one to store user created objects & its data.

    CREATE DATABASE is the command we use to create database. While creating a database, you need to specify the above two directories. If you dont specify the above two directories, the DBM will take the default path value from DFTDBPATH and creates the above two directories.For Example: db2 create database DB1

    On the other side, if you specify these two paths as below, then database related files are stored in /mnt/storage/DBPATH; User data is stored in /mnt/storage/storage_db1db2 create database DB1 on /mnt/storage/storage_db1 DBPATH on /mnt/storage/DBPATH

    ********Page No: 86 in learning DB2 visually with examples=======[db2inst2@rhel1 sqldbdir]$ db2 list db directory

    System Database Directory

    Number of entries in the directory = 3

    Database 1 entry:

    Database alias = DB2 Database name = DB2 Local database directory = /home/db2inst2 Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    Database 2 entry:

    Database alias = DB3 Database name = DB3 Local database directory = /home/db2inst2 Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    Database 3 entry:

    Database alias = DB1 Database name = DB1 Local database directory = /mnt/storage/db2inst2/dbpath Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    ============[db2inst2@rhel1 sqldbdir]$ db2 list db directory on /home/db2inst2

    Local Database Directory on /home/db2inst2

    Number of entries in the directory = 2

    Database 1 entry:

    Database alias = DB2 Database name = DB2 Database directory = SQL00001 Database release level = 10.00 Comment = Directory entry type = Home Catalog database partition number = 0 Database member number = 0

    Database 2 entry:

    Database alias = DB3 Database name = DB3 Database directory = SQL00002 Database release level = 10.00 Comment = Directory entry type = Home Catalog database partition number = 0 Database member number = 0============================

    *Page No: 86 in learning DB2 visually with examples=======[db2inst2@rhel1 sqldbdir]$ db2 list db directory

    System Database Directory

    Number of entries in the directory = 3

    Database 1 entry:

    Database alias = DB2 Database name = DB2 Local database directory = /home/db2inst2 Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    Database 2 entry:

    Database alias = DB3 Database name = DB3 Local database directory = /home/db2inst2 Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    Database 3 entry:

    Database alias = DB1 Database name = DB1 Local database directory = /mnt/storage/db2inst2/dbpath Database release level = 10.00 Comment = Directory entry type = Indirect Catalog database partition number = 0 Alternate server hostname = Alternate server port number =

    ============[db2inst2@rhel1 sqldbdir]$ db2 list db directory on /home/db2inst2

    Local Database Directory on /home/db2inst2

    Number of entries in the directory = 2

    Database 1 entry:

    Database alias = DB2 Database name = DB2 Database directory = SQL00001 Database release level = 10.00 Comment = Directory entry type = Home Catalog database partition number = 0 Database member number = 0

    Database 2 entry:

    Database alias = DB3 Database name = DB3 Database directory = SQL00002 Database release level = 10.00 Comment = Directory entry type = Home Catalog database partition number = 0 Database member number = 0============================

    ********http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/index.jsp?topic=%2Fcom.ibm.db2.luw.admin.perf.doc%2Fdoc%2Fc0008930.html

    DB creation needs two directories:One to store database related filesAnother one to store user created objects & its data.

    CREATE DATABASE is the command we use to create database. While creating a database, you need to specify the above two directories. If you dont specify the above two directories, the DBM will take the default path value from DFTDBPATH and creates the above two directories.For Example: db2 create database DB1

    On the other side, if you specify these two paths as below, then database related files are stored in /mnt/storage/DBPATH; User data is stored in /mnt/storage/storage_db1db2 create database DB1 on /mnt/storage/storage_db1 DBPATH on /mnt/storage/DBPATH

    *database tablespaces containers : There is a parent-child relation between them. 1 database, multiple tablespaces; 1 tablespace, multiple containers.

    db2 "create tablespace tbspace1 managed by system USING ('/mnt/storage/sai/tbspace1')

    db2 list tablespace containers for 4

    Tablespace Containers for Tablespace 4

    Container ID = 0 Name = /mnt/storage/sai/tbspace1 Type = Path

    *database tablespaces containers : There is a parent-child relation between them. 1 database, multiple tablespaces; 1 tablespace, multiple containers.*database tablespaces containers : There is a parent-child relation between them. 1 database, multiple tablespaces; 1 tablespace, multiple containers.**http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/index.jsp?topic=%2Fcom.ibm.db2.luw.admin.perf.doc%2Fdoc%2Fc0008930.html

    DB creation needs two directories:One to store database related filesAnother one to store user created objects & its data.

    CREATE DATABASE is the command we use to create database. While creating a database, you need to specify the above two directories. If you dont specify the above two directories, the DBM will take the default path value from DFTDBPATH and creates the above two directories.For Example: db2 create database DB1

    On the other side, if you specify these two paths as below, then database related files are stored in /mnt/storage/DBPATH; User data is stored in /mnt/storage/storage_db1db2 create database DB1 on /mnt/storage/storage_db1 DBPATH on /mnt/storage/DBPATH

    *The following tools and SQL administrative routines use the SYSTOOLSPACE table space:ADMIN_COPY_SCHEMA procedureADMIN_DROP_SCHEMA procedureADMIN_MOVE_TABLE procedureADMIN_MOVE_TABLE_UTIL procedureAdministrative task schedulerAlter TablenotebookALTOBJ procedureAutomatic Reorganization (including thedb.tb_reorg_reqhealth indicator)Automatic Statistics Collection (including thedb.tb_runstats_reqhealth indicator)Configure Automatic Maintenancewizarddb2lookcommandDesign AdvisorGET_DBSIZE_INFO procedureStorage Management toolSYSINSTALLOBJECTS procedure

    Following is an example of how to create the SYSTOOLSPACE and SYSTOOLSTMPSPACE table spaces manually. This example uses the same definitions that are used when table spaces are created automatically.If the database is using automatic storage:CREATE TABLESPACE SYSTOOLSPACE IN IBMCATGROUP MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 4 CREATE USER TEMPORARY TABLESPACE SYSTOOLSTMPSPACE IN IBMCATGROUP MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 4If the database is not using automatic storage:CREATE TABLESPACE SYSTOOLSPACE IN IBMCATGROUP MANAGED BY DATABASE USING ( FILE 'SYSTOOLSPACE' 32 M ) AUTORESIZE YES EXTENTSIZE 4 CREATE USER TEMPORARY TABLESPACE SYSTOOLSTMPSPACE IN IBMCATGROUP MANAGED BY SYSTEM USING ( 'SYSTOOLSTMPSPACE' ) EXTENTSIZE 4By default, the use of SYSTOOLSTMPSPACE will be granted to PUBLIC as long as the database is not created using restricted access.

    **db2 connect reset db2 terminate***db2 connect reset db2 terminate**This better can be explained with multiple sessions. doing insertions in one session, watching the count in another one!***DB2_CLP_EDITOR**SYSSTAT views are updatable catalog views that are found in the SYSSTAT schema. The updatable views contain statistical information that is used by the optimizer. The values in some columns in these views can be changed to test performance. (Before changing any statistics, it is recommended that the RUNSTATS command be invoked so that all the statistics reflect the current state.) ***db2 => SELECT VARCHAR(STORAGE_GROUP_NAME, 30) AS STOGROUP, VARCHAR(DB_STORAGE_PATH, 40) AS STORAGE_PATH FROM TABLE(ADMIN_GET_STORAGE_PATHS('',-1)) AS T

    STOGROUP STORAGE_PATH------------------------------ ----------------------------------------IBMSTOGROUP /mnt/storage/storage_db1SG1 /mnt/storage/sgroups/fs1SG1 /mnt/storage/sgroups/fs2SG1 /mnt/storage/sgroups/fs3

    *


Recommended