第一范文网 - 专业文章范例文档资料分享平台

DB2学习笔记(实用)

来源:用户分享 时间:2025/6/28 4:49:48 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

1.insert 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

3.any,all与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 班' )有结果

5.union 求并集 intersect 求交集

except 求差集 这三个加上all后,不去重复

6.存在的修改,不存在的插入 用Merge。利用manager表中的数据对员工表进行更新 MERGE INTO EMPLOYE AS EM (目标表为employe) USING MANAGER AS MA ON EM.EMPLOYEID=MA.MANAGERID(条件) WHEN MATCHED AND EM.SALARYMA.SALARY THEN DELETE(删除employe表中的记录,而manager中的记录保留) WHEN NOT MATCHED THEN INSERT VALUES (MA.MANAGERID,MA.NAME,MA.SALARY)(将manager中的值插入到employe中) ELSE IGNORE;(其他情况忽略)

7.采样数据

SELECT * FROM FETCH FIRST 10 ROWS ONLY;

8.sql中尽量避免用以下语句:

(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 e.DEPTNO=D.DEPTNO and D.DEPTNO=40(有 结果的) 结果:张三 10 10 null 李四 20 20 null 王五 10 10 null 赵六 20 20 null

列字段不去重,当不满足时,有一部分为空。第一张的数据全部选出来,第二张表的根据情况显示,有 则显示,木有则为null。只做连接 不做过滤 在统计函数时一定要注意. (2) select e.NAME,E.DEPTNO,D.DEPTNAME from employ e left join ( select * from DEPARTMENT1 where DEPTNAME='客服部' )AS D on e.DEPTNO=D.DEPTNO 结果:张三 10 null 李四 20 客服部 王五 10 null 赵六 20 客服部

(3) select * from employ e left join DEPARTMENT1 D on e.DEPTNO=D.DEPTNO where D.DEPTNAME=' 客服部' 结果:李四 20 20 客服部 赵六 20 20 客服部

10.函数

coalesce(A,0) 如果字段A的值为空时将其转化成0 11.数据的装载 语句实现: (1) 导出

export to D:\\employe.txt of del select * from employe; (2) 导入

数据清空 load from null.txt of del replace into sales1; -------------------------------------- -- 数据导入

--------------------------------------

--load from sales1.txt of del insert into sales1;

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

--load from null.txt of del replace into sales1;

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

--load from null.txt of del replace into sales1;

--select current timestamp from sales fetch first row only; --import from sales1.txt of del insert into sales1;

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

--------------------------------------

-- 分批数据导出和导入 --------------------------------------

---export to sales10.txt of del select * from sales1 where nodenumber(sales)=0; ---export to sales11.txt of del select * from sales1 where nodenumber(sales)=1;

--db2_all \node\;

--db2_all \; 方法一

在控制中心的对象视图窗口中,选择所要导出表结构的数据表,按住Ctrl或Shift可多选,单击鼠标右键,选择->生成DDL即可。

方法二

第一步:打开DB2的命令行工具,在DB2安装目录的BIN文件夹下新建一个文件夹data,并且进入该目录。

创建该目录: mkdir data 进入该目录: cd data |

第二步:导出表结构,命令行如下:

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

执行成功之后,你会在刚才新建的文件夹下找到该sql文件。

第三步:导出数据,命令行如下:

db2move databasename export -u username -p password

至此,导出数据结束。

2导出表中数据

export to [path(例:D:\字段(例: * or col1,col2,col3)] from TABLE1;

export to [path(例:D:\字段(例: * or col1,col2,col3)] from TABLE1;

导入表的数据

import from [path(例:D:\

load from [path(例:D:\

load from [path(例:D:\装入数据前,先删除已存在记录

load from [path(例:D:\当装入失败时,重新执行,并记录导出结果和错误信息

import from [path(例:D:\例:D:\into TABLE1;// 其中,savecount表示完成每1000条操作,记录一次.

存在自增长字段的数据导入:

load from [path(例:D:\of ixf modified by identityignore insert into TABLE1;// 加入modified by identityignore.

解除装入数据时,发生的检查挂起.

SET INTEGRITY FOR TABLE1 CHECK IMMEDIATE UNCHECKED;

命令只对数据通过约束检查的表有效,如果执行还不能解除,有必要检查数据的完整性,是否不符合约束条件,并试图重新整理数据,再执行装入操作.

另外,对load和import,字面上的区别是:装入和导入,但仍未理解两者之间的区别.

只是性能上load显然优于import.(load 需要更多的权限)

搜索更多关于: DB2学习笔记(实用) 的文档
DB2学习笔记(实用).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1ei8w0mxwn0a0pl1tz3w_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top