26
DB2 Commands - UDB www.srinimf.com

DB2 Basic Commands - UDB

Embed Size (px)

DESCRIPTION

DB2 Basic Commands - UDB

Citation preview

Page 1: DB2 Basic Commands - UDB

DB2 Commands - UDB

www.srinimf.com

Page 2: DB2 Basic Commands - UDB

Begin

to start typedb2start

To stop db2stop

Page 3: DB2 Basic Commands - UDB

Commands

db2 => connect to sample db2 => bind sample.bndHelp command:Db2 => ?Db2 => ? CATALOG DATABASE - It gives more info on particular commandQuit Commnad:Db2 => quit

Page 4: DB2 Basic Commands - UDB

db2 => db2start This takes some time (maybe 30 seconds)DB20000I The DB2START command completed successfully.

db2 => ? ACTIVATE DATABASE GET CONTACTGROUPS RECONCILE ADD CONTACT GET CONTACTS RECOVER ADD CONTACTGROUP GET/UPDATE DB CFG REDISTRIBUTE DB PARTITION ADD DATALINKS MANAGER GET/UPDATE DBM CFG REFRESH LDAP ADD DBPARTITIONNUM GET DBM MONITOR SWITCHES REGISTER LDAP ADD XMLSCHEMA GET DESCRIPTION FOR HEALTH REGISTER XMLSCHEMA ARCHIVE LOG GET NOTIFICATION LIST REGISTER XSROBJECT ATTACH GET HEALTH SNAPSHOT REORG INDEXES/TABLE AUTOCONFIGURE GET INSTANCE REORGCHK BACKUP DATABASE GET MONITOR SWITCHES RESET ADMIN CFG BIND GET RECOMMENDATIONS RESET ALERT CFG CATALOG APPC NODE GET ROUTINE RESET DB CFG CATALOG APPN NODE GET SNAPSHOT RESET DBM CFG CATALOG DATABASE HELP RESET MONITOR CATALOG DCS DATABASE HISTORY RESTART DATABASE CATALOG LDAP DATABASE IMPORT RESTORE DATABASE CATALOG LDAP NODE INITIALIZE TAPE REWIND TAPE CATALOG LOCAL NODE INSPECT ROLLFORWARD DATABASE CATALOG NPIPE NODE LIST ACTIVE DATABASES RUNCMD CATALOG NETBIOS NODE LIST APPLICATIONS RUNSTATS CATALOG ODBC DATA SOURCE LIST COMMAND OPTIONS SET CLIENT CATALOG TCPIP NODE LIST DATABASE DIRECTORY SET RUNTIME DEGREE CHANGE DATABASE COMMENT LIST DB PARTITION GROUPS SET TABLESPACE CONTAINERS CHANGE ISOLATION LEVEL LIST DATALINKS MANAGERS SET TAPE POSITION COMPLETE XMLSCHEMA LIST DBPARTITIONNUMS SET UTIL_IMPACT_PRIORITY CREATE DATABASE LIST DCS APPLICATIONS SET WRITE

Page 5: DB2 Basic Commands - UDB

CREATE TOOLS CATALOG LIST DCS DIRECTORY START DATABASE MANAGER DEACTIVATE DATABASE LIST DRDA INDOUBT START HADR DECOMPOSE XML DOCUMENT LIST HISTORY STOP DATABASE MANAGER DECOMPOSE XML DOCUMENTS LIST INDOUBT TRANSACTIONS STOP HADR DEREGISTER LIST NODE DIRECTORY TAKEOVER HADR DESCRIBE LIST ODBC DATA SOURCES TERMINATE DETACH LIST PACKAGES/TABLES UNCATALOG DATABASE DROP CONTACT LIST TABLESPACE CONTAINERS UNCATALOG DCS DATABASE DROP CONTACTGROUP LIST TABLESPACES UNCATALOG LDAP DATABASE DROP DATABASE LIST UTILITIES UNCATALOG LDAP NODE DROP DATALINKS MANAGER LOAD UNCATALOG NODE DROP DBPARTITIONNUM LOAD QUERY UNCATALOG ODBC DATA DROP TOOLS CATALOG MIGRATE DATABASE UNQUIESCE DATABASE ECHO PING UNQUIESCE INSTANCE EDIT PREP/PRECOMPILE UPDATE ALERT CFG EXPORT PRUNE HISTORY/LOGFILE UPDATE COMMAND OPTIONS FORCE APPLICATION PUT ROUTINE UPDATE CONTACT GET/UPDATE ADMIN CFG QUERY CLIENT UPDATE CONTACTGROUP GET ALERT CFG QUIESCE DATABASE UPDATE NOTIFICATION LIST GET AUTHORIZATIONS QUIESCE INSTANCE UPDATE HISTORY GET/UPDATE CLI CFG QUIESCE TABLESPACES UPDATE LDAP NODE GET CONNECTION STATE QUIT UPDATE MONITOR SWITCHES GET CONTACTGROUP REBIND XQUERY

Note: Some commands are operating system specific and may not be available.

For further help: ? db2-command - help for specified command ? OPTIONS - help for all command options ? HELP - help for reading help screens The preceding three options can be run as db2 <option> from an OS prompt.

!db2ic - DB2 Information Center (Windows only) This command can also be run as db2ic from an OS prompt.

Page 6: DB2 Basic Commands - UDB

Create Database

db2 => create database slo DB20000I The CREATE DATABASE command completed successfully.

db2 => connect to slo

Database Connection Information

Database server = DB2/SUN64 9.7.0 SQL authorization ID = DB90 <- my

db2 account Local database alias = SLO

Page 7: DB2 Basic Commands - UDB

Create Table

db2 => create table EMPLOYEE \db2 (cont.) => (SSN varchar(9) not null, \db2 (cont.) => LastNamdb2 (cont.) => e varchar(20), \db2 (cont.) => Firsdb2 (cont.) => tName varchar(10), \db2 (cont.) => Sex varchar(1), \db2 (cont.) => DeptNo char(3), \db2 (cont.) => primary key(SSN))DB20000I The SQL command completed successfully.

Page 8: DB2 Basic Commands - UDB

Insert

db2 => insert into DEPARTMENT values('SG7','Safety Department','Springy')

DB20000I The SQL command completed successfully.

Page 9: DB2 Basic Commands - UDB

JOIN

db2 => select LastName, DeptName from Employee as e, Department as d \

db2 (cont.) => where e.deptno = d.deptno

LASTNAME DEPTNAME -------------------- --------------------Simpson Safety Department Green Safety Department Bing Accounting Flanders Personnel Geller Personnel

5 record(s) selected.

Page 10: DB2 Basic Commands - UDB

Join 3 Tables

db2 => select Lastname, projectname \db2 (cont.) => from employee e, department d, project p \db2 (cont.) => where e.deptno = d.deptno and d.deptno = p.deptno

LASTNAME PROJECTNAME -------------------- --------------------Simpson New Pay Green New Pay Flanders GetThings Geller GetThings Simpson Alpha Green Alpha Simpson Omega Green Omega Bing Delta Simpson Pi Green Pi

11 record(s) selected.

Page 11: DB2 Basic Commands - UDB

Plain & Nested Query

db2 => select lastname from employee as e, department as d \db2 (cont.) => where e.deptno = d.deptno and deptname = 'Safety Department' (line wrapped by powerpoint)

LASTNAME --------------------Simpson Green

2 record(s) selected.

db2 => select lastname from employee where deptno in \db2 (cont.) => (select deptno from department \db2 (cont.) => where deptname = 'Safety Department')

LASTNAME --------------------Simpson Green

2 record(s) selected.

Page 12: DB2 Basic Commands - UDB

Cartesian Product

db2 => select * from employee, department

SSN LASTNAME FIRSTNAME SEX DEPTNO DEPTNO DEPTNAME LOCATION --------- -------------------- ---------- --- ------ ------ -------------------- ----------245 Simpson Homer M SG7 SG7 Safety Department Springy 245 Simpson Homer M SG7 S8P Accounting London 245 Simpson Homer M SG7 G6H Personnel London 245 Simpson Homer M SG7 K9J InfoTech Toronto 34 Flanders Ned M G6H SG7 Safety Department Springy 34 Flanders Ned M G6H S8P Accounting London 34 Flanders Ned M G6H G6H Personnel London 34 Flanders Ned M G6H K9J InfoTech Toronto 222 Green Rachel F SG7 SG7 Safety Department Springy 222 Green Rachel F SG7 S8P Accounting London 222 Green Rachel F SG7 G6H Personnel London 222 Green Rachel F SG7 K9J InfoTech Toronto 333 Bing Changler M S8P SG7 Safety Department Springy 333 Bing Changler M S8P S8P Accounting London 333 Bing Changler M S8P G6H Personnel London 333 Bing Changler M S8P K9J InfoTech Toronto 111 Geller Monica F G6H SG7 Safety Department Springy 111 Geller Monica F G6H S8P Accounting London 111 Geller Monica F G6H G6H Personnel London 111 Geller Monica F G6H K9J InfoTech Toronto

20 record(s) selected.

Page 13: DB2 Basic Commands - UDB

UNION

db2 => select location from department union \db2 (cont.) => (select projectlocation from project)

1 ----------Hong Kong London Springy Toronto

4 record(s) selected.

Page 14: DB2 Basic Commands - UDB

Views

db2 => create view deptlocations as select distinct location from departmentDB20000I The SQL command completed successfully.db2 => select * from deptlocations

LOCATION ----------London Springy Toronto

3 record(s) selected.

Page 15: DB2 Basic Commands - UDB

Difference & Union

db2 => select * from projlocations except (select * from deptlocations)

1 ----------Hong Kong

1 record(s) selected.

db2 => select * from projlocations union select * from deptlocations

1 ----------Hong Kong London Springy Toronto

4 record(s) selected.

Page 16: DB2 Basic Commands - UDB

Renaming

db2 => select SSN as SIN from employee

SIN ---------111 222 245 333 34

5 record(s) selected.

Page 17: DB2 Basic Commands - UDB

Intersect

db2 => select * from projlocations intersect select * from deptlocations

1 ----------London Springy Toronto

3 record(s) selected.

Page 18: DB2 Basic Commands - UDB

Wildcard

db2 => select * from employee where firstname like 'N%'

SSN LASTNAME FIRSTNAME SEX DEPTNO--------- -------------------- ---------- --- ------34 Flanders Ned M G6H

1 record(s) selected.

Page 19: DB2 Basic Commands - UDB

Aggregate

db2 => select count(*) from projemp

1 ----------- 6

1 record(s) selected.

Page 20: DB2 Basic Commands - UDB

Group By

db2 => select count(*) as numberpergroup, projectid from projemp group by projectid

NUMBERPERGROUP PROJECTID-------------- --------- 2 P13 3 P35 1 P43

3 record(s) selected.

Page 21: DB2 Basic Commands - UDB

Update

db2 => update employee set firstname = 'Bozo' where firstname = 'Ned'DB20000I The SQL command completed successfully.db2 => select * from employee

SSN LASTNAME FIRSTNAME SEX DEPTNO--------- -------------------- ---------- --- ------245 Simpson Homer M SG7 34 Flanders Bozo M G6H 222 Green Rachel F SG7 333 Bing Changler M S8P 111 Geller Monica F G6H

5 record(s) selected.

Page 22: DB2 Basic Commands - UDB

Delete

db2 => delete from employee where lastname like 'G%'DB20000I The SQL command completed successfully.db2 => select * from employee

SSN LASTNAME FIRSTNAME SEX DEPTNO--------- -------------------- ---------- --- ------245 Simpson Homer M SG7 34 Flanders Bozo M G6H 333 Bing Changler M S8P

3 record(s) selected.

Page 23: DB2 Basic Commands - UDB

Alter Table

db2 => create table junk (firstattr integer, secondattr decimal(10,2))DB20000I The SQL command completed successfully.db2 => select * from junk

FIRSTATTR SECONDATTR ----------- ------------

0 record(s) selected.

db2 => alter table junk add third varchar(20)DB20000I The SQL command completed successfully.db2 => select * from junk

FIRSTATTR SECONDATTR THIRD ----------- ------------ --------------------

0 record(s) selected.

Page 24: DB2 Basic Commands - UDB

Dropdb2 => list tables

Table/View Schema Type Creation time------------------------------- --------------- ----- --------------------------DEPARTMENT SYLVIA T 2004-06-21-15.38.51.804343DEPTLOCATIONS SYLVIA V 2004-06-21-16.05.12.051085EMPLOYEE SYLVIA T 2004-06-21-15.38.35.567546JUNK SYLVIA T 2004-06-21-16.26.20.210302PROJECT SYLVIA T 2004-06-21-15.39.42.940604PROJEMP SYLVIA T 2004-06-21-15.40.05.233869PROJLOCATIONS SYLVIA V 2004-06-21-16.06.37.274142

7 record(s) selected.

db2 => drop table junkDB20000I The SQL command completed successfully.db2 => list tables

Table/View Schema Type Creation time------------------------------- --------------- ----- --------------------------DEPARTMENT SYLVIA T 2004-06-21-15.38.51.804343DEPTLOCATIONS SYLVIA V 2004-06-21-16.05.12.051085EMPLOYEE SYLVIA T 2004-06-21-15.38.35.567546PROJECT SYLVIA T 2004-06-21-15.39.42.940604PROJEMP SYLVIA T 2004-06-21-15.40.05.233869PROJLOCATIONS SYLVIA V 2004-06-21-16.06.37.274142

6 record(s) selected.

Page 25: DB2 Basic Commands - UDB

Terminate

db2 => terminateDB20000I The TERMINATE command completed successfully.obelix[33]% db2stop10/01/2009 14:16:06 0 0 SQL1064N DB2STOP processing was successful.SQL1064N DB2STOP processing was successful.

Page 26: DB2 Basic Commands - UDB

Thank Youwww.srinimf.com