DB2常用SQL的寫法_第1頁
DB2常用SQL的寫法_第2頁
DB2常用SQL的寫法_第3頁
DB2常用SQL的寫法_第4頁
DB2常用SQL的寫法_第5頁
已閱讀5頁,還剩39頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、DB2常用SQL的寫法DB2常用SQL的寫法(未整理) UPDATE FROM update a set aa =(select bb from b where a.cc = b.cc) where exists (select b.cc from a.cc =b.cc) 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 d

2、b2admin 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; creat

3、e 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

4、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=3ON zjt_tables(tabname); 9. 查看表 select tabname from tables where tabname=ZJT_TABLES; 10. 查看列 select SUBSTR(COLNAME,1,20) a

5、s 列名,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 ind

6、name 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

7、LIST APPLICATION; 20. kill application FORCE APPLICATION(0); db2 force applications all (強(qiáng)迫所有應(yīng)用程序從數(shù)據(jù)庫斷開) 21. lock tablelock 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)前活動數(shù)據(jù)庫 list active databases 26. 查看命令選項(xiàng) l

8、ist 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 1. 啟動實(shí)例(db2inst1):db2start2. 停止實(shí)例(db2inst1):db2stop3. 列出所有實(shí)例(db2inst1)db2ilist5.列出當(dāng)前實(shí)例:db2 get insta

9、nce4. 觀察例如配置文件:db2 get dbm cfg|more5. 更新數(shù)據(jù)庫管理器參數(shù)信息:db2 update dbm cfg using para_name para_value6. 創(chuàng)立數(shù)據(jù)庫:db2 create db test7. 觀察數(shù)據(jù)庫配置參數(shù)信息db2 get db cfg for test|more8. 更新數(shù)據(jù)庫參數(shù)配置信息db2 update db cfg for test using para_name para_value10.刪除數(shù)據(jù)庫:db2 drop db testdb2 connect to test12.列出所有表空間的詳細(xì)信息。db2 list

10、 tablespaces show detail13.查詢數(shù)據(jù):db2 select * from tb114.數(shù)據(jù):db2 delete from tb1 where id=115.創(chuàng)立索引:db2 create index idx1 on tb1(id);16.創(chuàng)立視圖:db2 create view view1 as select id from tb117.查詢視圖:db2 select * from view1db2 catalog tcp node node_name remote server_ip server server_portdb2 get dbm cfg|grep S

11、VCENAMEdb2 attach to node_namedb2 list node direcotrydb2 uncatalog node node_namedb2 catalog db db_name as db_alias at node node_namedb2 list db directorydb2 connect to db_alias user user_name using user_passworddb2 uncatalog db db_aliasdb2 export to myfile of ixf messages msg select * from tb1db2 i

12、mport from myfile of ixf messages msg replace into tb1db2move test exportdb2 create db test1db2move db_alias importdb2 reorgchkdb2 reorg table tb1db2 runstats on table tb1db2 backup db testdb2 restore db test399.列出容器的信息db2 list tablespace containers for tbs_id show detail40.創(chuàng)立表:db2 ceate table tb1(i

13、d integer not null,name char(10)db2 list tables42.插入數(shù)據(jù):db2 insert into tb1 values(1,sam);db2 insert into tb2 values(2,smitty); - 創(chuàng)立一個自定義單值類型create distinct type var_newtypeas decimal(5,2) with comparisons;- var_newtype 類型名- decimal(5,2) 實(shí)際的類型- 刪除一個自定義單值類型drop distinct type var_newtype;- 創(chuàng)立一個自定義結(jié)構(gòu)數(shù)據(jù)類

14、型create type my_type as(username varchar(20),department integer,salary decimal(10,2)not finalmode db2sql;- 修改自定義結(jié)構(gòu)數(shù)據(jù)類型,我目前還沒有發(fā)現(xiàn)刪除屬性的方法.alter type my_typeadd attribute hiredate date;- 刪除自定義結(jié)構(gòu)數(shù)據(jù)類型drop type my_type;- 獲取系統(tǒng)當(dāng)前日期 select current date from sysibm.sysdummy1; select current time from sysibm.sy

15、sdummy1;select current timestamp from sysibm.sysdummy1;-sysibm.sysdummy1表是一個特殊的內(nèi)存中的表,用它可以發(fā)現(xiàn)如上面演示的 DB2 存放器的值。您也可以使用關(guān)鍵字 VALUES 來對存放器或表達(dá)式求值。VALUES current date; VALUES current time;VALUES current timestamp;- VALUES的更多用法VALUES 2+5;VALUES hello lavasoft!;values 56union allvalues 45;values 1,2,3,4,5,6unio

16、n allvalues 7,8,9,10,11,12order by 1;- 更多變態(tài)級DB2 SQL寫法,AnyOneTable表示任意一個存在的表select 234 from AnyOneTable;select distinct 234 from AnyOneTable;select distinct 234 as 1 from AnyOneTable;select DB2變態(tài)級的SQL哈哈 from AnyOneTable;select distinct DB2變態(tài)級的SQL哈哈 from AnyOneTable;select distinct DB2變態(tài)級的SQL哈哈 as 1 f

17、rom AnyOneTable; -(嘿嘿,好玩吧,你可以用任意一個表來當(dāng)sysibm.sysdummy1用.不過不推薦這么做,除非你不記得sysibm.sysdummy1怎么寫了,Oracle中(對應(yīng)dual)也一樣!哈哈哈哈!)- 定義變量,還可以設(shè)定默認(rèn)值,給變量賦值declare var1 char(2);declare var2 int default 0;set var1 = aa;set var2 =23;-創(chuàng)立一個動態(tài)游標(biāo)變量declare d_cur integer;- 給變量賦值的另一種方法values expr1, expr2, expr3 into a, b, c;-

18、相當(dāng)于set a = expr1;set b = expr2;set c = expr3;- 還有一種賦值方式set prodname = (case when (name is not null) then name when (namestr is not null) then namestr else defaultname end);- 相當(dāng)于set prodname = coalesce(name, namestr, defaultname);-這個類似oracle的decode()和nvl()函數(shù)的合并.- 定義一個游標(biāo)declare cur1 cursor with return

19、 to client for select * from dm_hy;declare cur2 cursor for select * from dm_hy; - 靜態(tài)游標(biāo)- 創(chuàng)立數(shù)據(jù)表,并添加注釋,插入數(shù)據(jù).CREATE TABLE tbr_catalog ( id bigint not null generated by default as identity, type smallint not null, name varchar(255), parentid bigint, cataloglevel bigint, description varchar(255), PRIMARY

20、KEY (id);comment on table tbr_catalog is Birt報(bào)表目錄表;comment on column tbr_catalog.ID is 標(biāo)識;comment on column tbr_catalog.type is 目錄類型;comment on column tbr_ is 目錄名稱;comment on column tbr_catalog.parentid is 目錄父標(biāo)識;comment on column tbr_catalog.cataloglevel is 目錄層次;comment on column tbr_cat

21、alog.description is 目錄描述;- 給數(shù)據(jù)表插入數(shù)據(jù)insert into tbr_catalog(id, type, name, parentid, cataloglevel, description)values (1, 0, 系統(tǒng)報(bào)表, 0, 0, );insert into tbr_catalog(id, type, name, parentid, cataloglevel, description)values (2, 1, 用戶報(bào)表, 0, 0, );- 創(chuàng)立外鍵alter table tbr_storageadd constraint fk_tbr_storag

22、eforeign key (catalogid)references tbr_catalog(id);- 更改表,添加列alter table aaa add sex varchar(1);- 更改表,刪除列alter table aaa drop column sex;- 去掉參數(shù)前后的空格rtrim(dm_hy.mc);- 定義臨時表,通過已有person表來創(chuàng)立declare global temporary table gbl_temp like person on commit delete rows -提交時刪除數(shù)據(jù)not logged - 不在日志中紀(jì)錄in usr_tbsp -

23、 選用表空間- 此語句創(chuàng)立一個名為 gbl_temp 的用戶臨時表。定義此用戶臨時表 所使用的列的名稱和說明與 person 的列的名稱和說明完全相同。- 創(chuàng)立有兩個字段的臨時表 - 定義一個全局臨時表tmp_hy ( dm varchar(10), mc varchar(10) ) with replace - 如果存在此臨時表,那么替換 not logged; - 不在日志里紀(jì)錄 - 給臨時表插入三條數(shù)據(jù) insert into session.tmp_hy values(1,1); insert into session.tmp_hy values(1,1); insert into s

24、ession.tmp_hy values(1,1); - 通過查詢批量插入數(shù)據(jù)inster into tab_bk(select code,name from table book);- select . into的用法select * into :h1, :h2, :h3, :h4 from emp where empno = 528671;- 語句的流程控制if() thenopen cur1fetch cur1 into t_equipid;while(at_end1)do .set t_temp=0; end while;close cur1;else.end if; - 外連接sel

25、ect empno,deptname,projname from (emplyoee left outer join project on respemp=empon) left outer join department on mgrno=empno;- in、like、order by(. ASC|DESC)的用法select * from book twhere like %J_編程%and t.code inJ565333,J565222);order by asc- 匯總表(概念復(fù)雜,難以理解,不常用create summary table sumy_st

26、able1 as (select workdept, count(*) as reccount, sum(salary) as salary, sum(bonus) as bonus from employee group by workdept)data initially deferredrefresh immediate;- 使用SQL一次處理一個集合語義- 優(yōu)化前 select語句中每行的過程層和數(shù)據(jù)流層之間都有一個上下文切換declare cur1 cursor for col1,col2 from tab_comp;open cur1;fetch cur1 into v1,v2;w

27、hile SQLCODE 100 doif (v120) then insert into tab_sel values(20,v1);else insert into tab_sel values(v1,v2);end if;fetch cur1 into v1,v2;end while;- 優(yōu)化后沒有過程層和數(shù)據(jù)流層之間的上下文切換declare cur1 cursor for col1,col2 from tab_comp;open cur1;fetch cur1 into v1,v2;while SQLCODE 100 doinsert into tab_sel(select (cas

28、e when col120 then 20 else col1 end), col2 from tab_comp);fetch cur1 into v1,v2;end while;- DB2函數(shù)分三類:列函數(shù)、標(biāo)量函數(shù)、表函數(shù)- 列函數(shù)輸入一組數(shù)據(jù),輸出單一結(jié)果。- 標(biāo)量函數(shù)接收一個值,返回另外一個值。- 表函數(shù)只能用于SQL語句的from字句中,它返回一個表的列,類似于一個已創(chuàng)立的常規(guī)表。- 下面是個標(biāo)量函數(shù)的例子。create function (salary int,bonus_percent int)returns intlanguage SQL contains SQLreturn(

29、salary * bonus_percent/100)- 下面是表函數(shù)create function get_marks(begin_range int,end_range int)returns table(cid candidate_id, number test_id, score score)language SQL reads SQL DATAreturn select cid,number,score from test_taken where salary between (begin_range) and score(end_range)example 1: define a

30、scalar function that returns the tangent of a value using the existing sine and cosine functions. create function tan (x double) returns double language sql contains sql no external action deterministic return sin(x)/cos(x) example 2: define a transform function for the structured type person. creat

31、e function fromperson (p person) returns row (name varchar(10), firstname varchar(10) language sql contains sql no external action deterministic return values (, p.firstname)example 3: define a table function that returns the employees in a specified department number. create function deptempl

32、oyees (deptno char(3) returns table (empno char(6), lastname varchar(15), firstname varchar(12) language sql reads sql data no external action deterministic return select empno, lastname, firstnme from employeeexample 4: define a scalar function that reverses a string. create function reverse(instr

33、varchar(4000) returns varchar(4000) deterministic no external action contains sql begin atomic declare revstr, reststr varchar(4000) default ; declare len int; if instr is null then return null; end if; set (reststr, len) = (instr, length(instr); while len 0 do set (revstr, reststr, len) = (substr(r

34、eststr, 1, 1) concat revstr, substr(reststr, 2, len - 1), len - 1); end while; return revstr; endexample 4: define the table function from example 4 with auditing. create function deptemployees (deptno char(3) returns table (empno char(6), lastname varchar(15), firstname varchar(12) language sql mod

35、ifies sql data no external action deterministic begin atomic insert into audit values (user, table: employee prd: deptno = concat deptno); return select empno, lastname, firstnme from employee end- for循環(huán)語句的用法 begin atomicdeclare fullname char(40);for vl as select firstnme, midinit, lastname from emp

36、loyee do set fullname = lastname concat , concat firstnme concat concat midinit; insert into tnames values (fullname);end forend - leave的用法create procedure leave_loop(out counter integer)language sqlbegin declare v_counter integer; declare v_firstnme varchar(12); declare v_midinit char(1); declare v

37、_lastname varchar(15); declare at_end smallint default 0; declare not_found condition for sqlstate 02000; declare c1 cursor for select firstnme, midinit, lastname from employee; declare continue handler for not_found set at_end = 1; set v_counter = 0; open c1; fetch_loop: loop fetch c1 into v_firstn

38、me, v_midinit, v_lastname; if at_end 0 then leave fetch_loop; end if; set v_counter = v_counter + 1; end loop fetch_loop; set counter = v_counter; close c1;end - if語句的用法 create procedure update_salary_if (in employee_number char(6), inout rating smallint) language sql begin declare not_found conditi

39、on for sqlstate 02000; declare exit handler for not_found set rating = -1; if rating = 1 then update employee set salary = salary * 1.10, bonus = 1000 where empno = employee_number; elseif rating = 2 then update employee set salary = salary * 1.05, bonus = 500 where empno = employee_number; else upd

40、ate employee set salary = salary * 1.03, bonus = 0 where empno = employee_number; end if; end - loop的用法 create procedure loop_until_space(out counter integer) language sql begin declare v_counter integer default 0; declare v_firstnme varchar(12); declare v_midinit char(1); declare v_lastname varchar

41、(15); declare c1 cursor for select firstnme, midinit, lastname from employee; declare continue handler for not found set counter = -1; open c1; fetch_loop: loop fetch c1 into v_firstnme, v_midinit, v_lastname; if v_midinit = then leave fetch_loop; end if; set v_counter = v_counter + 1; end loop fetc

42、h_loop; set counter = v_counter; close c1; end - return的用法 begin . goto fail . success: return 0 fail: return -200 end- 創(chuàng)立一個自定義單值類型create distinct type var_newtypeas decimal(5,2) with comparisons;- var_newtype 類型名- decimal(5,2) 實(shí)際的類型- 刪除一個自定義單值類型drop distinct type var_newtype;- 創(chuàng)立一個自定義結(jié)構(gòu)數(shù)據(jù)類型create t

43、ype my_type as(username varchar(20),department integer,salary decimal(10,2)not finalmode db2sql;- 修改自定義結(jié)構(gòu)數(shù)據(jù)類型,我目前還沒有發(fā)現(xiàn)刪除屬性的方法.alter type my_typeadd attribute hiredate date;- 刪除自定義結(jié)構(gòu)數(shù)據(jù)類型drop type my_type;- 獲取系統(tǒng)當(dāng)前日期 select current date from sysibm.sysdummy1; select current time from sysibm.sysdummy1;s

44、elect current timestamp from sysibm.sysdummy1;-sysibm.sysdummy1表是一個特殊的內(nèi)存中的表,用它可以發(fā)現(xiàn)如上面演示的 DB2 存放器的值。您也可以使用關(guān)鍵字 VALUES 來對存放器或表達(dá)式求值。VALUES current date; VALUES current time;VALUES current timestamp;- VALUES的更多用法VALUES 2+5;VALUES hello lavasoft!;values 56union allvalues 45;values 1,2,3,4,5,6union allvalu

45、es 7,8,9,10,11,12order by 1;- 更多變態(tài)級DB2 SQL寫法,AnyOneTable表示任意一個存在的表select 234 from AnyOneTable;select distinct 234 from AnyOneTable;select distinct 234 as 1 from AnyOneTable;select DB2變態(tài)級的SQL哈哈 from AnyOneTable;select distinct DB2變態(tài)級的SQL哈哈 from AnyOneTable;select distinct DB2變態(tài)級的SQL哈哈 as 1 from AnyOn

46、eTable; -(嘿嘿,好玩吧,你可以用任意一個表來當(dāng)sysibm.sysdummy1用.不過不推薦這么做,除非你不記得sysibm.sysdummy1怎么寫了,Oracle中(對應(yīng)dual)也一樣!哈哈哈哈!)- 定義變量,還可以設(shè)定默認(rèn)值,給變量賦值declare var1 char(2);declare var2 int default 0;set var1 = aa;set var2 =23;-創(chuàng)立一個動態(tài)游標(biāo)變量declare d_cur integer;- 給變量賦值的另一種方法values expr1, expr2, expr3 into a, b, c;- 相當(dāng)于set a

47、= expr1;set b = expr2;set c = expr3;- 還有一種賦值方式set prodname = (case when (name is not null) then name when (namestr is not null) then namestr else defaultname end);- 相當(dāng)于set prodname = coalesce(name, namestr, defaultname);-這個類似oracle的decode()和nvl()函數(shù)的合并.- 定義一個游標(biāo)declare cur1 cursor with return to clien

48、t for select * from dm_hy;declare cur2 cursor for select * from dm_hy; - 靜態(tài)游標(biāo)- 創(chuàng)立數(shù)據(jù)表,并添加注釋,插入數(shù)據(jù).CREATE TABLE tbr_catalog ( id bigint not null generated by default as identity, type smallint not null, name varchar(255), parentid bigint, cataloglevel bigint, description varchar(255), PRIMARY KEY (id);

49、comment on table tbr_catalog is Birt報(bào)表目錄表;comment on column tbr_catalog.ID is 標(biāo)識;comment on column tbr_catalog.type is 目錄類型;comment on column tbr_ is 目錄名稱;comment on column tbr_catalog.parentid is 目錄父標(biāo)識;comment on column tbr_catalog.cataloglevel is 目錄層次;comment on column tbr_catalog.description is 目錄描述;- 給數(shù)據(jù)表插入數(shù)據(jù)insert into tbr_catalog(id, type,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論