DB2學習筆記 實用

2022-12-26 11:06:02 字數 4139 閱讀 4962

into d_user(name,birthday) values ('張三','1997-2-1');

insert into d_user(name,birthday) values ('趙七','1995-2-3'),('王五','1973-6-3');---不能夠回車換行,要麼都插入要麼都不插入

2.主鍵要指定不能為空s_id varchar(10) not null primary key

與max,mi的對應關係

> any(sub-qurey) --- > min(sub-qurey) 大於any時,取的是滿足條件中的最小值;小於any時取的是滿足條件中的最小值;any任何乙個

< any(sub-query) --- < max(sub-qurey)

> all(sub-query) --- > max(sub-qurey) all所有的

< all(sub-query) --- < min(sub-qurey)

4 . 特別注意:all 如果有空值的話,max和min自動忽略空值,會有結果。而是用all則會不。chinese 有空值

(1) select name from student where class='五年級a 班' and chinese < all

(select chinese from student where class='五年級b 班'

) 空值

(2) select name from student where class='五年級a 班' and chinese <

(select min(chinese) from student where class='五年級b 班'

)有結果

求並集intersect 求交集

except 求差集這三個加上all後,不去重複

6.存在的修改,不存在的插入用merge。利用manager表中的資料對員工表進行更新

merge into employe as em (目標表為employe)

using manager as ma

on 條件)

when matched and < then update set 條件滿足時更新employe表中的對應記錄)

when matched and > then delete(刪除employe表中的記錄,而manager中的記錄保留)

when not matched then insert values (將manager中的值插入到employe中)

else ignore;(其他情況忽略)

7.取樣資料

select * from fetch first 10 rows only;

中盡量避免用以下語句:

(1) 少使用 or 用 in代替

(2) where 子句中應該盡量避免在字段上使用函式,因為這樣做會使該字

段上的索引失效,影響sql 語句的效能。即使該字段上沒有索引,也應該避免在字段上使

用函式。select * from user where date(registerdate)='2009-9-24';

(3) 盡量避免在sql語句中使用like

9.在使用join注意一下:

(1)left join 只連線不過濾

insert into employ(name,deptno) values('張三',10),('李四',20),('王五',10),('趙六',20)

insert into department1(deptno,deptname) values (10,'技術部'),(20,'客服部');

select * from employ e left join department1 d on and 有結果的)

結果:張三 10 10 null

李四 20 20 null

王五 10 10 null

趙六 20 20 null

列字段不去重,當不滿足時,有一部分為空。第一張的資料全部選出來,第二張表的根據情況顯示,有則顯示,木有則為null。只做連線不做過濾在統計函式時一定要注意.

(2) select from employ e left join (

select * from department1 where deptname='客服部'

)as d on

結果:張三 10 null

李四 20 客服部

王五 10 null

趙六 20 客服部

(3) select * from employ e left join department1 d on where 客服部'

結果:李四 20 20 客服部

趙六 20 20 客服部

10.函式

coalesce(a,0) 如果欄位a的值為空時將其轉化成0

11.資料的裝載

語句實現:

(1) 匯出

export to d:\ of del select * from employe;

(2) 匯入

資料清空 load from of del replace into sales1;

-- 資料匯入

--load from of del insert into sales1;

--select current timestamp from sales fetch first row only;

--load from of del replace into sales1;

--select current timestamp from sales fetch first row only;

--load from of del replace into sales1;

--select current timestamp from sales fetch first row only;

--import from of del insert into sales1;

--select current timestamp from sales fetch first row only;

-- 分批資料匯出和匯入

---export to of del select * from sales1 where nodenumber(sales)=0;

---export to of del select * from sales1 where nodenumber(sales)=1;

--db2_all "|| export to sales1##.txt of del select * from sales1 where nodenumber(sales)=current node";

--db2_all "|| load from sales1##.txt of del insert into sales1";

方法一在控制中心的物件檢視視窗中,選擇所要匯出表結構的資料表,按住ctrl或shift可多選,單擊滑鼠右鍵,選擇->生成ddl即可。

方法二第一步:開啟db2的命令列工具,在db2安裝目錄的bin資料夾下新建乙個資料夾data,並且進入該目錄。

建立該目錄: mkdir data

進入該目錄: cd data

|第二步:匯出表結構,命令列如下:

db2look -d dbname -e -a -x -i username -w password -o

執行成功之後,你會在剛才新建的資料夾下找到該sql檔案。

第三步:匯出資料,命令列如下:

db2move databasename export -u username -p password

至此,匯出資料結束。

2匯出表中資料

export to [path(例:d:" of ixf select [字段(例: * or col1,col2,col3)] from table1;

export to [path(例:d:" of del select [字段(例: * or col1,col2,col3)] from table1;

匯入表的資料

import from [path(例:d:" of ixf insert into table1;

load from [path(例:d:" of ixf insert into table1;

DB2匯出表結構 表資料小結

2010 12 5 一 db2命令列匯出資料庫全庫表結構 1 win r進入到db2安裝目錄的bin目錄下,執行命令 db2cmd,進入到db2 clp視窗。命令 db2cmd 2 建立乙個data資料夾 命令 mkdir data 說明 將資料庫全表結構的sql語句匯出到data目錄下 3 進入到...

Oracle與DB2資料型別對應說明

通過下表介紹與db2 400資料型別之間的對應關係,是一對多的關係,具體採用哪種對應關係,應具體問題具體分析。注意事項 oracle中的date含有年 月 日 時 分 秒,它和db2 400中的date不相互對應,db2 400中的date只有年 月 日,time型別含有時 分 秒,因此日期和時間型...

在DB2中如何實現Oracle的相關功能 二

作者 ccbzzp 在現實的應用中大家可能經常會遇到在db2中如何實現oracle的某些功能 在此我簡單地總結一下 實現某一功能可能會有很多種方法 在此就沒有全部列出 歡迎大家繼續 以便和大家共享 共同 共同近步 以下主要以oracle 8i,9i和db2 為例 1.如何檢視資料庫的版本的oracl...