下面一条龙实现linux平台下 DB2数据库的简单操作:
从实例用户(db2inst1) 查实例下的数据库、创建/删除实例下的数据库 查询数据库SAMPLE包含的所有表 查询表数据。
1.切换到实例用户:db2inst1
infosrvr:~/Desktop # su - db2inst1
db2inst1@infosrvr:~>
2.列出实例下的所有数据库
infosrvr:~/Desktop # su - db2inst1
db2inst1@infosrvr:~>
db2inst1@infosrvr:~> db2 list db directory
System Database Directory
Number of entries in the directory = 4
Database 1 entry:
Database alias = XMETA
Database name = XMETA
Local database directory = /opt/IBM/InformationServer/Repos/xmeta
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 2 entry:
Database alias = QS
Database name = QS
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 3 entry:
Database alias = TEST1
Database name = TEST1
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 4 entry:
Database alias = SAMPLE
Database name = SAMPLE
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
3.创建新的数据库
db2inst1@infosrvr:~> db2 create db test2
DB20000I The CREATE DATABASE command completed successfully.
4.删除数据库
db2inst1@infosrvr:~> db2 drop db test2
DB20000I The DROP DATABASE command completed successfully.
5.连接到数据库
db2inst1@infosrvr:~> db2 connect to sample
Database Connection Information
Database server = DB2/LINUX 9.7.4
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
6.查看数据库中的表
db2inst1@infosrvr:~> db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
ACT DB2INST1 T 2012-01-17-09.38.24.314033
ADEFUSR DB2INST1 S 2012-01-17-09.38.30.953823
CATALOG DB2INST1 T 2012-01-17-09.38.45.758734
CL_SCHED DB2INST1 T 2012-01-17-09.38.17.264534
CUSTOMER DB2INST1 T 2012-01-17-09.38.42.650735
DEPARTMENT DB2INST1 T 2012-01-17-09.38.17.922386
DEPT DB2INST1 A 2012-01-17-09.38.19.113115
EMP DB2INST1 A 2012-01-17-09.38.20.001560
EMPACT DB2INST1 A 2012-01-17-09.38.24.312415
EMPLOYEE DB2INST1 T 2012-01-17-09.38.19.114152
EMPLOYEES DB2INST1 T 2012-01-31-12.29.59.242048
EMPLOYEESBCK DB2INST1 T 2012-01-31-12.30.01.379628
EMPMDC DB2INST1 T 2012-01-17-09.38.33.972494
EMPPROJACT DB2INST1 T 2012-01-17-09.38.24.072812
EMP_ACT DB2INST1 A 2012-01-17-09.38.24.313415
EMP_PHOTO DB2INST1 T 2012-01-17-09.38.20.002277
EMP_RESUME DB2INST1 T 2012-01-17-09.38.20.913174
INVENTORY DB2INST1 T 2012-01-17-09.38.41.945239
IN_TRAY DB2INST1 T 2012-01-17-09.38.25.117312
ORDERS DB2INST1 T 2012-01-31-12.30.00.875412
ORDERS_REPORT DB2INST1 T 2018-04-30-03.23.21.691567
ORG DB2INST1 T 2012-01-17-09.38.25.248487
PRODUCT DB2INST1 T 2012-01-17-09.38.40.533568
PRODUCTSUPPLIER DB2INST1 T 2012-01-17-09.38.52.256502
PROJ DB2INST1 A 2012-01-17-09.38.23.450158
PROJACT DB2INST1 T 2012-01-17-09.38.23.450891
PROJECT DB2INST1 T 2012-01-17-09.38.22.005809
PURCHASEORDER DB2INST1 T 2012-01-17-09.38.43.886457
SALES DB2INST1 T 2012-01-17-09.38.25.597600
STAFF DB2INST1 T 2012-01-17-09.38.25.364715
STAFFG DB2INST1 T 2012-01-17-09.38.28.800946
SUPPLIERS DB2INST1 T 2012-01-17-09.38.51.099347
VACT DB2INST1 V 2012-01-17-09.38.26.143195
VASTRDE1 DB2INST1 V 2012-01-17-09.38.26.217231
VASTRDE2 DB2INST1 V 2012-01-17-09.38.26.302266
VDEPMG1 DB2INST1 V 2012-01-17-09.38.26.205180
VDEPT DB2INST1 V 2012-01-17-09.38.25.733555
VEMP DB2INST1 V 2012-01-17-09.38.26.136359
VEMPDPT1 DB2INST1 V 2012-01-17-09.38.26.211423
VEMPLP DB2INST1 V 2012-01-17-09.38.26.520095
VEMPPROJACT DB2INST1 V 2012-01-17-09.38.26.192738
VFORPLA DB2INST1 V 2012-01-17-09.38.26.499173
VHDEPT DB2INST1 V 2012-01-17-09.38.26.132636
VPHONE DB2INST1 V 2012-01-17-09.38.26.515803
VPROJ DB2INST1 V 2012-01-17-09.38.26.139689
VPROJACT DB2INST1 V 2012-01-17-09.38.26.148497
VPROJRE1 DB2INST1 V 2012-01-17-09.38.26.306779
VPSTRDE1 DB2INST1 V 2012-01-17-09.38.26.312796
VPSTRDE2 DB2INST1 V 2012-01-17-09.38.26.494157
VSTAFAC1 DB2INST1 V 2012-01-17-09.38.26.505147
VSTAFAC2 DB2INST1 V 2012-01-17-09.38.26.509563
51 record(s) selected.
7.查询表
db2inst1@infosrvr:~> db2 "select * from DEPT"
DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION
------ ------------------------------------ ------ -------- ----------------
A00 SPIFFY COMPUTER SERVICE DIV. 000010 A00 -
B01 PLANNING 000020 A00 -
C01 INFORMATION CENTER 000030 A00 -
D01 DEVELOPMENT CENTER - A00 -
D11 MANUFACTURING SYSTEMS 000060 D01 -
D21 ADMINISTRATION SYSTEMS 000070 D01 -
E01 SUPPORT SERVICES 000050 A00 -
E11 OPERATIONS 000090 E01 -
E21 SOFTWARE SUPPORT 000100 E01 -
F22 BRANCH OFFICE F2 - E01 -
G22 BRANCH OFFICE G2 - E01 -
H22 BRANCH OFFICE H2 - E01 -
I22 BRANCH OFFICE I2 - E01 -
J22 BRANCH OFFICE J2 - E01 -
14 record(s) selected.
8.查询用户
db2inst1@infosrvr:~> db2 "select GRANTOR,GRANTEE from syscat.dbauth"
GRANTOR GRANTEE
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
DB2INST1 DB2ADMIN
SYSIBM DB2INST1
SYSIBM PUBLIC
3 record(s) selected.
9.查询权限表 前三行
db2inst1@infosrvr:~> db2 "select * from syscat.tabauth fetch first 3 rows only"
GRANTOR GRANTORTYPE GRANTEE GRANTEETYPE TABSCHEMA TABNAME CONTROLAUTH ALTERAUTH DELETEAUTH INDEXAUTH INSERTAUTH REFAUTH SELECTAUTH UPDATEAUTH
-------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------- --------- ---------- --------- ---------- ------- ---------- ----------
SYSIBM S PUBLIC G SYSIBM SYSTABLES N N N N N N Y N
SYSIBM S PUBLIC G SYSIBM SYSCOLUMNS N N N N N N Y N
SYSIBM S PUBLIC G SYSIBM SYSINDEXES N N N N N N Y N
3 record(s) selected.
10.查询GRANTEE='DB2ADMIN'的权限
db2inst1@infosrvr:~> db2 "select * from syscat.tabauth where GRANTEE='DB2ADMIN'"
GRANTOR GRANTORTYPE GRANTEE GRANTEETYPE TABSCHEMA TABNAME CONTROLAUTH ALTERAUTH DELETEAUTH INDEXAUTH INSERTAUTH REFAUTH SELECTAUTH UPDATEAUTH
-------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------- --------- ---------- --------- ---------- ------- ---------- ----------
SYSIBM S DB2ADMIN U DB2ADMIN CUSTS Y G G G G G G G
SYSIBM S DB2ADMIN U DB2INST1 ORDERS_REPORT Y G G G G G G G
DB2INST1 U DB2ADMIN U SAMPLE BONUS N N N N N N Y N
DB2INST1 U DB2ADMIN U SAMPLE DEPT N N N N N N Y N
DB2INST1 U DB2ADMIN U SAMPLE EMP N N N N N N Y N
DB2INST1 U DB2ADMIN U SAMPLE EMP_NEW N N N N N N Y N
DB2INST1 U DB2ADMIN U SAMPLE EMP_TEST N N N N N N Y N
DB2INST1 U DB2ADMIN U SAMPLE SALGRADE N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSCAT COLUMNS N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSCAT KEYCOLUSE N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSCAT TABLES N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSIBM SYSDBAUTH N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSIBM SYSDUMMY1 N N N N N N Y N
DB2INST1 U DB2ADMIN U SYSIBM SYSVIEWS N N N N N N Y N
14 record(s) selected.
11.查询表DEPARTMENT和EMPLOYEE的权限用户
db2inst1@infosrvr:~> db2 "select * from syscat.tabauth where TABNAME in('DEPARTMENT','EMPLOYEE')"
GRANTOR GRANTORTYPE GRANTEE GRANTEETYPE TABSCHEMA TABNAME CONTROLAUTH ALTERAUTH DELETEAUTH INDEXAUTH INSERTAUTH REFAUTH SELECTAUTH UPDATEAUTH
-------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------- --------- ---------- --------- ---------- ------- ---------- ----------
SYSIBM S DB2INST1 U DB2INST1 DEPARTMENT Y G G G G G G G
SYSIBM S DB2INST1 U DB2INST1 EMPLOYEE Y G G G G G G G
2 record(s) selected.
12.我的测试结果
db2inst1@infosrvr:~> db2 "select * from dept_emp"
EMPID EMPNAME LOCATION DEPTID DEPTNAME
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2448 Linda Goodman US 123 IBM Software Group
7621 Subramanian Krishnan Singapore 123 IBM Software Group
1976 Mahima Singh India 324 IBM Lab services
8653 Samuel K India 444
5431 Bradley Chen China 223 Global Business Service
0 0 hylan ETL GROUP
6 record(s) selected.
测试结果:
db2inst1@infosrvr:~> db2 "select * from dept_emp"
EMPID EMPNAME LOCATION DEPTID DEPTNAME
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2448 Linda Goodman US 123 IBM Software Group
7621 Subramanian Krishnan Singapore 123
1976 Mahima Singh India 324 IBM Lab services
8653 Samuel K India 444
5431 Bradley Chen China 223 Global Business Service
5 record(s) selected.
db2inst1@infosrvr:~> db2 "select * from dept_emp"
DEPTID DEPTNAME EMPID EMPNAME LOCATION
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
123 IBM Software Group 2448 Linda Goodman US
123 IBM Software Group 7621 Subramanian Krishnan Singapore
324 IBM Lab services 1976 Mahima Singh India
223 Global Business Service 5431 Bradley Chen China
234 hylan ETL GROUP 0
5 record(s) selected.
db2inst1@infosrvr:~> db2 "select * from dept_emp"
EMPID EMPNAME LOCATION DEPTID DEPTNAME
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2448 Linda Goodman US 123 IBM Software Group
7621 Subramanian Krishnan Singapore 123
1976 Mahima Singh India 324 IBM Lab services
8653 Samuel K India 444
5431 Bradley Chen China 223 Global Business Service
5 record(s) selected.
db2inst1@infosrvr:~> db2 "select * from dept_emp"
EMPID EMPNAME LOCATION DEPTID DEPTNAME
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2448 Linda Goodman US 123 IBM Software Group
7621 Subramanian Krishnan Singapore 123
1976 Mahima Singh India 324 IBM Lab services
8653 Samuel K India 444
5431 Bradley Chen China 223 Global Business Service
1234 hylan-add anhui 223
6 record(s) selected.