{"id":1955,"date":"2013-03-22T16:56:52","date_gmt":"2013-03-22T08:56:52","guid":{"rendered":"http:\/\/rmohan.com\/?p=1955"},"modified":"2013-03-22T16:56:52","modified_gmt":"2013-03-22T08:56:52","slug":"db2-commands","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1955","title":{"rendered":"DB2 Commands"},"content":{"rendered":"<p><span style=\"color: #0000ff;\">Start an instance <\/span><\/p>\n<p>As an instance owner on the host running db2, issue the following command<\/p>\n<p>$ db2start<\/p>\n<p><span style=\"color: #0000ff;\">Stopping the instance <\/span><\/p>\n<p>$ db2stop<\/p>\n<p><span style=\"color: #0000ff;\">Connect to the database as instance owner <\/span><\/p>\n<p>$ db2<\/p>\n<p>as a user of the database:<\/p>\n<p>$source ~instance\/sqllib\/db2cshrc (csh users)<\/p>\n<p>$ . ~instance\/sqllib\/db2profile (sh users)<\/p>\n<p>$ db2 connect to databasename<\/p>\n<p><span style=\"color: #0000ff;\">Create a table <\/span><\/p>\n<p>$ db2-&gt; create table employee<\/p>\n<p>(ID SMALLINT NOT NULL,<\/p>\n<p>NAME VARCHAR(9),<\/p>\n<p>DEPT SMALLINT CHECK (DEPT BETWEEN 10 AND 100),<\/p>\n<p>JOB CHAR(5) CHECK (JOB IN (&#8216;Sales&#8217;, &#8216;Mgr&#8217;, &#8216;Clerk&#8217;)),<\/p>\n<p>HIREDATE DATE,<\/p>\n<p>SALARY DECIMAL(7,2),<\/p>\n<p>COMM DECIMAL(7,2),<\/p>\n<p>PRIMARY KEY (ID),<\/p>\n<p>CONSTRAINT YEARSAL CHECK (YEAR(HIREDATE) &gt; 1986 OR SALARY &gt; 40500) )<\/p>\n<p> A simple version:<\/p>\n<p>db2-&gt; create table employee ( Empno smallint, Name varchar(30))<\/p>\n<p><span style=\"color: #0000ff;\">Create a schema <\/span><\/p>\n<p>If a user has SYSADM or DBADM authority, then the user can create a schema with any valid name. When a database is created, IMPLICIT_SCHEMA authority is granted to PUBLIC (that is, to all users). The following example creates a schema for an individual user with the authorization ID &#8216;joe&#8217;<\/p>\n<p>CREATE SCHEMA joeschma AUTHORIZATION joe<\/p>\n<p><span style=\"color: #0000ff;\">Create an alias <\/span><\/p>\n<p>The following SQL statement creates an alias WORKERS for the EMPLOYEE table:<\/p>\n<p>CREATE ALIAS WORKERS FOR EMPLOYEE<\/p>\n<p>You do not require special authority to create an alias, unless the alias is in a schema other than the one owned by your current authorization ID, in which case DBADM authority is required.<\/p>\n<p><span style=\"color: #0000ff;\">Create an Index: <\/span><\/p>\n<p>The physical storage of rows in a base table is not ordered. When a row is inserted, it is placed in the most convenient storage location that can accommodate it. When searching for rows of a table that meet a particular selection condition and the table has no indexes, the entire table is scanned. An index optimizes data retrieval without performing a lengthy sequential search. The following SQL statement creates a<\/p>\n<p>non-unique index called LNAME from the LASTNAME column on the EMPLOYEE table, sorted in ascending order:<\/p>\n<p>CREATE INDEX LNAME ON EMPLOYEE (LASTNAME ASC)<\/p>\n<p>The following SQL statement creates a unique index on the phone number column:<\/p>\n<p>CREATE UNIQUE INDEX PH ON EMPLOYEE (PHONENO DESC)<\/p>\n<p><span style=\"color: #0000ff;\">Drop a database: <\/span><\/p>\n<p>Db2 drop database sample<\/p>\n<p><span style=\"color: #0000ff;\">Alter tablespace <\/span><\/p>\n<p>Adding a Container to a DMS Table Space You can increase the size of a DMS table space (that is, one created with the MANAGED BY DATABASE clause) by adding one or more containers to the table<\/p>\n<p>space. The following example illustrates how to add two new device containers (each with 40 000 pages) to a table space on a UNIX-based system:<\/p>\n<p>ALTER TABLESPACE RESOURCE<\/p>\n<p>ADD (DEVICE &#8216;\/dev\/rhd9&#8217; 10000,<\/p>\n<p>DEVICE &#8216;\/dev\/rhd10&#8217; 10000)<\/p>\n<p>The following SQL statement drops the table space ACCOUNTING:<\/p>\n<p>DROP TABLESPACE ACCOUNTING<\/p>\n<p>You can reuse the containers in an empty table space by dropping the table space but you must COMMIT the DROP TABLESPACE command, or have had AUTOCOMMIT on, before attempting to reuse the containers. The following SQL statement creates a new temporary table space called TEMPSPACE2:<\/p>\n<p>CREATE TEMPORARY TABLESPACE TEMPSPACE2 MANAGED BY SYSTEM USING (&#8216;d&#8217;)<\/p>\n<p>Once TEMPSPACE2 is created, you can then drop the original temporary table space TEMPSPACE1 with the command: DROP TABLESPACE TEMPSPACE1<\/p>\n<p>Add Columns to an Existing Table<\/p>\n<p>When a new column is added to an existing table, only the table description in the system catalog is modified, so access time to the table is not affected immediately. Existing records are not physically altered<\/p>\n<p>until they are modified using an UPDATE statement. When retrieving an existing row from the table, a null or default value is provided for the new column, depending on how the new column was defined. Columns that are added after a table is created cannot be defined as NOT NULL: they must be defined as either NOT NULL WITH DEFAULT or as nullable. Columns can be added with an SQL statement. The following statement uses the ALTER TABLE statement to add three columns to the EMPLOYEE table:<\/p>\n<p>ALTER TABLE EMPLOYEE<\/p>\n<p>ADD MIDINIT CHAR(1) NOT NULL WITH DEFAULT<\/p>\n<p>ADD HIREDATE DATE<\/p>\n<p>ADD WORKDEPT CHAR(3)<\/p>\n<p><span style=\"color: #0000ff;\">GrantPermissions by Users <\/span><\/p>\n<p>The following example grants SELECT privileges on the EMPLOYEE table to the user HERON:<\/p>\n<p>GRANT SELECT ON EMPLOYEE TO USER HERON<\/p>\n<p>The following example grants SELECT privileges on the EMPLOYEE table to the group HERON:<\/p>\n<p>GRANT SELECT ON EMPLOYEE TO GROUP HERON<\/p>\n<p>GRANT SELECT,UPDATE ON TABLE STAFF TO GROUP PERSONNL<\/p>\n<p>If a privilege has been granted to both a user and a group with the same name, you must specify the GROUP or USER keyword when revoking the privilege. The following example revokes the SELECT privilege on the EMPLOYEE table from the user HERON:<\/p>\n<p>REVOKE SELECT ON EMPLOYEE FROM USER HERON<\/p>\n<p>To Check what permissions you have within the database<\/p>\n<p>SELECT * FROM SYSCAT.DBAUTH WHERE GRANTEE = USER AND GRANTEETYPE = &#8216;U&#8217;<\/p>\n<p>SELECT * FROM SYSCAT.COLAUTH WHERE GRANTOR = USER<\/p>\n<p>At a minimum, you should consider restricting access to the SYSCAT.DBAUTH, SYSCAT.TABAUTH, SYSCAT.PACKAGEAUTH, SYSCAT.INDEXAUTH, SYSCAT.COLAUTH, and SYSCAT.SCHEMAAUTH catalog views. This would prevent information on user privileges, which could be used to target an authorization name for break-in, becoming available to everyone with access to the database. The following statement makes the view available to every authorization name:<\/p>\n<p> GRANT SELECT ON TABLE MYSELECTS TO PUBLIC<\/p>\n<p>And finally, remember to revoke SELECT privilege on the base table:<\/p>\n<p> REVOKE SELECT ON TABLE SYSCAT.TABAUTH FROM PUBLIC<\/p>\n<p><span style=\"color: #0000ff;\">Delete Records from a table <\/span><\/p>\n<p>db2-&gt; delete from employee where empno = &#8216;001&#8217;<\/p>\n<p>db2-&gt; delete from employee<\/p>\n<p>The first example will delete only the records with emplno field = 001 The second example deletes all the records<\/p>\n<p><span style=\"color: #0000ff;\">Import Command <\/span><\/p>\n<p>Requires one of the following options: sysadm, dbadm, control privileges on each participating table or view, insert or select privilege, example:<\/p>\n<p>db2-&gt;import from testfile of del insert into workemployee<\/p>\n<p>where testfile contains the following information 1090,Emp1086,96613.57,55,Secretary,8,1983-8-14<\/p>\n<p>or your alternative is from the command line:<\/p>\n<p>db2 &#8221; import from &#8216;testfile&#8217; of del insert into workemployee&#8221;<\/p>\n<p>db2 &lt; test.sql where test.sql contains the following line:<\/p>\n<p>db2 import from test file of del insert into workemployee<\/p>\n<p><span style=\"color: #0000ff;\">Load Command: <\/span><\/p>\n<p>Requires the following auithority: sysadm, dbadm, or load authority on the database:<\/p>\n<p>example: db2 &#8220;load from &#8216;testfile&#8217; of del insert into workemployee&#8221;<\/p>\n<p>You may have to specify the full path of testfile in single quotes<\/p>\n<p>Authorization Level:<\/p>\n<p>One of the following:<\/p>\n<p>sysadm<\/p>\n<p>dbadm<\/p>\n<p>load authority on the database and<\/p>\n<p>INSERT privilege on the table when the load utility is invoked in INSERT mode, TERMINATE mode<\/p>\n<p>(to terminate a previous load insert operation), or RESTART mode (to restart a previous load insert<\/p>\n<p>operation)<\/p>\n<p>INSERT and DELETE privilege on the table when the load utility is invoked in REPLACE mode,<\/p>\n<p>TERMINATE mode (to terminate a previous load replace operation), or RESTART mode (to restart a<\/p>\n<p>previous load replace operation)<\/p>\n<p>INSERT privilege on the exception table, if such a table is used as part of the load operation.<\/p>\n<p>Caveat:<\/p>\n<p>If you are performing a load operation and you CTRL-C out of it, the tablespace is left in a load pending state. The only way to get out of it is to reload the data with a terminate statement<\/p>\n<p>First to view tablestate:<\/p>\n<p>Db2 list tablespaces show detail will display the tablespace is in a load pending state.<\/p>\n<p>Db2tbst &lt;tablespace state&gt;<\/p>\n<p>Here is the original query<\/p>\n<p>Db2 &#8220;load from &#8216;\/usr\/seela\/a.del&#8217; of del insert into A&#8221;;<\/p>\n<p>If you break out of the load illegally (ctrl-c), the tablespace is left load pending.<\/p>\n<p>To correct:<\/p>\n<p>Db2 &#8220;load form &#8216;\/usr\/seela\/a.del&#8217; of del terminate into A&#8221;;<\/p>\n<p>This will return the table to it&#8217;s original state and roll back the entries that you started loading.<\/p>\n<p>If you try to reset the tablespace with quiesce, it will not work . It&#8217;s an integrety issue<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #0000ff;\">DB2BATCH- command <\/span><\/p>\n<p>Reads SQL statements from either a flat file or standard input, dynamically prepares and describes the statements and returns an answer set: Authorization: sysadmin .and Required Connection -None..eg<\/p>\n<p>db2batch -d databasename -f filename -a userid\/passwd -r outfile<\/p>\n<p><span style=\"color: #0000ff;\">DB2expln &#8211; DB2 SQL Explain Tool <\/span><\/p>\n<p>Describes the access plan selection for static SQL statements in packages that are stored in the DB2 common server systems catalog. Given the database name, package name ,package creator abd section<\/p>\n<p>number the tool interprets and describes the information in these catalogs.<\/p>\n<p> <span style=\"color: #0000ff;\">DB2exfmt &#8211; Explain Table Format Tool <\/span><\/p>\n<p><span style=\"color: #0000ff;\">DB2icrt &#8211; Create an instance <\/span><\/p>\n<p><span style=\"color: #0000ff;\">DB2idrop &#8211; Dropan instance <\/span><\/p>\n<p><span style=\"color: #0000ff;\">DB2ilist &#8211; List instances <\/span><\/p>\n<p><span style=\"color: #0000ff;\">DB2imigr &#8211; Migrate instances <\/span><\/p>\n<p><span style=\"color: #0000ff;\">DB2iupdt &#8211; Update instances <\/span><\/p>\n<p><span style=\"color: #0000ff;\">Db2licm &#8211; Installs licenses file for product ; <\/span><\/p>\n<p>db2licm -a db2entr.lic<\/p>\n<p><span style=\"color: #0000ff;\">DB2look &#8211; DB2 Statistics Extraction Tool <\/span><\/p>\n<p>Generates the updates statements required to make the catalog statistics of a test database match those of a production. It is advantageous to have a test system contain asubset of your production system&#8217;s data.<\/p>\n<p>This tool queries the system catalogs of a database and outputs a tablespace n table index, and column information about each table in that database Authorization: Select privelege on system catalogs Required<\/p>\n<p>Connection &#8211; None. Syntax<\/p>\n<p>db2look -d databasename -u creator -t Tname -s -g -a -p -o<\/p>\n<p>Fname -e -m -c -r -h<\/p>\n<p>where -s : generate a postscript file, -g a graph , -a for all users in the database, -t limits output to a particular tablename, -p plain text format , -m runs program in mimic mode, examples:<\/p>\n<p>db2look -d db2res -o output will write stats for tables created in db<\/p>\n<p>db2res in latex format<\/p>\n<p>db2look -p -a -d db2res -o output &#8211; will write stats in plain text format<\/p>\n<p><span style=\"color: #0000ff;\"> DB2 -list tablespaces show detail <\/span><\/p>\n<p>displays the following information as an example:<\/p>\n<p>Tablespaces for Current Database<\/p>\n<p>Tablespace ID = 0<\/p>\n<p>Name = SYSCATSPACE<\/p>\n<p>Type = System managed space<\/p>\n<p>Contents = Any data<\/p>\n<p>State = 0x0000<\/p>\n<p>Detailed explanation:<\/p>\n<p>Normal<\/p>\n<p>Total pages = 2925<\/p>\n<p>Useable pages = 2925<\/p>\n<p>Used pages = 2925<\/p>\n<p>Free pages = Not applicable<\/p>\n<p>High water mark (pages) = Not applicable<\/p>\n<p>Page size (bytes) = 4096<\/p>\n<p>Extent size (pages) = 32<\/p>\n<p>Prefetch size (pages) = 32<\/p>\n<p>Number of containers = 1<\/p>\n<p> <span style=\"color: #0000ff;\">db2tbst &#8211; Get tablespace state. <\/span><\/p>\n<p>Authorization &#8211; none , Required connection none, syntax db2tbst tabpespace-state:The state value is part of the output of list tablespaces example<\/p>\n<p>db2tbst 0X0000 returns state normal<\/p>\n<p>db2tbst 2 where 2 indicates tablespace id 2 will also work<\/p>\n<p> <span style=\"color: #0000ff;\">DB2dbdft &#8211; environment variable <\/span><\/p>\n<p>Defining this environment variable with the database you want to connect to automatically connects you to the database . example setenv db2dbdft sample will allow you to connect to sample by default.<\/p>\n<p><span style=\"color: #0000ff;\">CLP &#8211; Command Line Processor Invocation: <\/span><\/p>\n<p>db2 starts the command line processor. The clp is used to execute database utilities, sql statements and online help. It offers a variety of command options and can be started in :<\/p>\n<p>1. interactive mode : db2-&gt;<\/p>\n<p>2. command mode where each command is prefixed by db2<\/p>\n<p>3. batch mode which uses the -f file input option<\/p>\n<p> <span style=\"color: #0000ff;\">Update the configuration in the database : <\/span><\/p>\n<p>Db2 =&gt;update db cfg for sample using maxappls 60<\/p>\n<p>MAXFILOP = 64 2 &#8211; 9150<\/p>\n<p>db2 =&gt; update db cfg for sample using maxappls 160<\/p>\n<p>db2 =&gt; update db cfg for sample using AVG_APPLS 4<\/p>\n<p>db2 =&gt;update db cfg for sample using MAXFILOP 256<\/p>\n<p>can see updated parameters from client<\/p>\n<p>tcpip &#8230;.. not started up properly Check the DB2COMM variable if it it is set<\/p>\n<p>db2set DB2COMM &lt;enter&gt;<\/p>\n<p>How to terminate the database if processes are still attached:<\/p>\n<p>db2 force applications all<\/p>\n<p>db2stop<\/p>\n<p>db2start<\/p>\n<p>db2 connect to dbname (locally)<\/p>\n<p>How to trace logs withing the db2diag.log file:<\/p>\n<p>Connections to db fails:<\/p>\n<p>Move the db2diag.log from the sqllib\/db2dump directory to some other working directory ( mv db2diag.log &lt;some other working directory )<\/p>\n<p>db2 update dbm cfg using diaglevel 4<\/p>\n<p>db2stop<\/p>\n<p>db2start<\/p>\n<p>db2trc on -l 8000000 -e 10<\/p>\n<p>db2 connect to dbname (locally)<\/p>\n<p>db2trc dump 01876.trc<\/p>\n<p>db2trc flw 01876.trc 01876.flw<\/p>\n<p>db2trc fmt 01876.trc 01876.fmt<\/p>\n<p>db2trc off<\/p>\n<p>Import data from ascii file to database<\/p>\n<p>db2 &#8221; import from inp.data of del insert into test&#8221;<\/p>\n<p>db2 &#8220;load from &#8216;\/cs\/home\/tech1\/seela\/inp.data&#8217; of del insert into seela.seela&#8221;<\/p>\n<p>db2 &lt; test.sql<\/p>\n<p>Revoke permissions from the database from public:<\/p>\n<p>db2 =&gt; create database GO3421<\/p>\n<p>DB20000I The CREATE DATABASE command completed successfully.<\/p>\n<p>Now I want to revoke connect, createtab bindadd on database from public<\/p>\n<p>On server: db2 =&gt; revoke connect , createtab, bindadd on database from public<\/p>\n<p>Now on client, as techstu, I tried to connect to go3421<\/p>\n<p>db2 =&gt; connect to go3421<\/p>\n<p>SQL1060N User &#8220;TECHSTU &#8221; does not have the CONNECT privilege. SQLSTATE=08004<\/p>\n<p>Now I have to grant connect privilege to group ugrad<\/p>\n<p>On server:<\/p>\n<p>db2 =&gt; grant connect, createtab on database to group ugrad<\/p>\n<p>DB20000I The SQL command completed successfully.<\/p>\n<p>Tested on client I can connect successfully.<\/p>\n<p>Now on the client, I can connect as a student, list tables but not select. I<\/p>\n<p>can still describe tables<\/p>\n<p>To prevent this:<\/p>\n<p>On server<\/p>\n<p>revoke select on table syscat.columns from public<\/p>\n<p>Now on client, I cannot describe but also on my tables.<\/p>\n<p>db2 =&gt; revoke select on table syscat.columns from public<\/p>\n<p>DB20000I The SQL command completed successfully.<\/p>\n<p>db2 =&gt; grant select on table syscat.columns to group ugrad<\/p>\n<p> On server:<\/p>\n<p>db2 =&gt; revoke select on table syscat.indexes from public<\/p>\n<p>DB20000I The SQL command completed successfully.<\/p>\n<p>select * from syscat.dbauth will display all the privileges for<\/p>\n<p>dbadm authority:<\/p>\n<p>DBADMAUTH CREATETABAUTH BINDADDAUTH CONNECTAUTH<\/p>\n<p>NOFENCEAUTH IMPLSCHEMAAUTH LOAD AUTH<\/p>\n<p>select<\/p>\n<p>TABNAME,DELETEAUTH,INSERTAUTH,SELECTAUTH from<\/p>\n<p>syscat.tabauth<\/p>\n<p>grant connect, createtab<\/p>\n<p>grant connect, createtab on database to user techstu<\/p>\n<p>to group ugrad<\/p>\n<p> <span style=\"color: #0000ff;\">Instance Level Authority <\/span><\/p>\n<p>db2 get dbm cfg<\/p>\n<p>db2 get admin cfg<\/p>\n<p>db2 get db cfg<\/p>\n<p>CLP using filename on the command line<\/p>\n<p>Db2 -f filename.clp<\/p>\n<p>The -f option directs the clp to accept input from file.<\/p>\n<p>Db2 +c -v +t infile .. The option can be prefixed by a + sign or turned on by a letter with a -sign<\/p>\n<p>+c is turned off, -v turned on and -f turned on<\/p>\n<p>c is for commit, v for verbose and f for filename<\/p>\n<p>-t termination character is set to semicolon<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Start an instance <\/p>\n<p>As an instance owner on the host running db2, issue the following command<\/p>\n<p>$ db2start<\/p>\n<p>Stopping the instance <\/p>\n<p>$ db2stop<\/p>\n<p>Connect to the database as instance owner <\/p>\n<p>$ db2<\/p>\n<p>as a user of the database:<\/p>\n<p>$source ~instance\/sqllib\/db2cshrc (csh users)<\/p>\n<p>$ . ~instance\/sqllib\/db2profile (sh users)<\/p>\n<p>$ db2 connect to databasename<\/p>\n<p> [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1955"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1955"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1955\/revisions"}],"predecessor-version":[{"id":1957,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1955\/revisions\/1957"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}