1. 建立數(shù)據(jù)庫DB2_GCB
CREATE DATABASE DB2_GCB ON G: ALIAS DB2_GCB
USING CODESET GBK TERRITORY CN COLLATE USING SYSTEM DFT_EXTENT_SZ 32
2. 連接數(shù)據(jù)庫
connect to sample1 user db2admin using 8301206
3. 建立別名
create alias db2admin.tables for sysstat.tables;
CREATE ALIAS DB2ADMIN.VIEWS FOR SYSCAT.VIEWS
create alias db2admin.columns for syscat.columns;
create alias guest.columns for syscat.columns;
4. 建立表
create table zjt_tables as
(select * from tables) definition only;
create table zjt_views as
(select * from views) definition only;
5. 插入記錄
insert into zjt_tables select * from tables;
insert into zjt_views select * from views;
6. 建立視圖
create view V_zjt_tables as select tabschema,tabname from zjt_tables;
7. 建立觸發(fā)器
CREATE TRIGGER zjt_tables_del
AFTER DELETE ON zjt_tables
REFERENCING OLD AS O
FOR EACH ROW MODE DB2SQL
Insert into zjt_tables1 values(substr(o.tabschema,1,8),substr(o.tabname,1,10))
8. 建立唯一性索引
CREATE UNIQUE INDEX I_ztables_tabname
[size=3]ON zjt_tables(tabname);
9. 查看表
select tabname from tables
where tabname='ZJT_TABLES';
10. 查看列
select SUBSTR(COLNAME,1,20) as 列名,TYPENAME as 類型,LENGTH as 長度
from columns
where tabname='ZJT_TABLES';
11. 查看表結(jié)構(gòu)
db2 describe table user1.department
db2 describe select * from user.tables
12. 查看表的索引
db2 describe indexes for table user1.department
13. 查看視圖
select viewname from views
where viewname='V_ZJT_TABLES';
14. 查看索引
select indname from indexes
where indname='I_ZTABLES_TABNAME';
15. 查看存貯過程
SELECT SUBSTR(PROCSCHEMA,1,15),SUBSTR(PROCNAME,1,15)
FROM SYSCAT.PROCEDURES;
16. 類型轉(zhuǎn)換(cast)
ip datatype:varchar
select cast(ip as integer)+50 from log_comm_failed
17. 重新連接
connect reset
18. 中斷數(shù)據(jù)庫連接
disconnect db2_gcb
19. view application
LIST APPLICATION;
20. kill application
FORCE APPLICATION(0);
db2 force applications all (強(qiáng)迫所有應(yīng)用程序從數(shù)據(jù)庫斷開)
21. lock table
lock table test in exclusive mode
22. 共享
lock table test in share mode
23. 顯示當(dāng)前用戶所有表
list tables
24. 列出所有的系統(tǒng)表
list tables for system
25. 顯示當(dāng)前活動(dòng)數(shù)據(jù)庫
list active databases
26. 查看命令選項(xiàng)
list command options
27. 系統(tǒng)數(shù)據(jù)庫目錄
LIST DATABASE DIRECTORY
28. 表空間
list tablespaces
29. 表空間容器
LIST TABLESPACE CONTAINERS FOR
Example: LIST TABLESPACE CONTAINERS FOR 1
30. 顯示用戶數(shù)據(jù)庫的存取權(quán)限
GET AUTHORIZATIONS
31. 啟動(dòng)實(shí)例
DB2START
32. 停止實(shí)例
db2stop
33. 表或視圖特權(quán)
grant select,delete,insert,update on tables to user
grant all on tables to user WITH GRANT OPTION
34. 程序包特權(quán)
GRANT EXECUTE
ON PACKAGE PACKAGE-name
TO PUBLIC
35. 模式特權(quán)
GRANT CREATEIN ON SCHEMA SCHEMA-name TO USER
36. 數(shù)據(jù)庫特權(quán)
grant connect,createtab,dbadm on database to user
37. 索引特權(quán)
grant control on index index-name to user
38. 信息幫助 (? XXXnnnnn )
例:? SQL30081
39. SQL 幫助(說明 SQL 語句的語法)
help statement
例如,help SELECT
40. SQLSTATE 幫助(說明 SQL 的狀態(tài)和類別代碼)
? sqlstate 或 ? class-code
41. 更改與"管理服務(wù)器"相關(guān)的口令
db2admin setid username password
42. 創(chuàng)建 SAMPLE 數(shù)據(jù)庫
db2sampl
db2sampl F:(指定安裝盤)
43. 使用操作系統(tǒng)命令
! dir
44. 轉(zhuǎn)換數(shù)據(jù)類型 (cast)
SELECT EMPNO, CAST(RESUME AS VARCHAR(370))
FROM EMP_RESUME
WHERE RESUME_FORMAT = 'ascii'
45. UDF
要運(yùn)行 DB2 Java 存儲(chǔ)過程或 UDF,還需要更新服務(wù)器上的 DB2 數(shù)據(jù)庫管理程序配置,以包括在該機(jī)器上安裝 JDK 的路徑
db2 update dbm cfg using JDK11_PATH d:sqllibjavajdk
TERMINATE
update dbm cfg using SPM_NAME sample
46. 檢查 DB2 數(shù)據(jù)庫管理程序配置
db2 get dbm cfg
47. 檢索具有特權(quán)的所有授權(quán)名
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'DATABASE' FROM SYSCAT.DBAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'TABLE ' FROM SYSCAT.TABAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'PACKAGE ' FROM SYSCAT.PACKAGEAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'INDEX ' FROM SYSCAT.INDEXAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'COLUMN ' FROM SYSCAT.COLAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'SCHEMA ' FROM SYSCAT.SCHEMAAUTH
UNION
SELECT DISTINCT GRANTEE, GRANTEETYPE, 'SERVER ' FROM SYSCAT.PASSTHRUAUTH
ORDER BY GRANTEE, GRANTEETYPE, 3
create table yhdab
(id varchar(10),
password varchar(10),
ywlx varchar(10),
kh varchar(10));
create table ywlbb
(ywlbbh varchar(8),
ywmc varchar(60))
48. 修改表結(jié)構(gòu)
alter table yhdab ALTER kh SET DATA TYPE varchar(13);
alter table yhdab ALTER ID SET DATA TYPE varchar(13);
alter table lst_bsi alter bsi_money set data type int;
insert into yhdab values
('20000300001','123456','user01','20000300001'),
('20000300002','123456','user02','20000300002');
49. 業(yè)務(wù)類型說明
insert into ywlbb values
('user01','業(yè)務(wù)申請'),
('user02','業(yè)務(wù)撤消'),
('user03','費(fèi)用查詢'),
('user04','費(fèi)用自繳'),
('user05','費(fèi)用預(yù)存'),
('user06','密碼修改'),
('user07','發(fā)票打印'),
('gl01','改用戶基本信息'),
('gl02','更改支付信息'),
('gl03','日統(tǒng)計(jì)功能'),
('gl04','沖帳功能'),
('gl05','對帳功能'),
('gl06','計(jì)費(fèi)功能'),
('gl07','綜合統(tǒng)計(jì)')