DB2学习笔记chensy(8)

2019-03-09 16:09

深入浅出DB2

10.3 修改表格

1. 增加字段

ALTER TABLE CHENSYTEST3 ADD EMP_NO char(20) ; 2. 删除字段

ALTER TABLE CHENSYTEST3 DROP COLUMN EMP_NO; 3. 修改字段类型

ALTER TABLE CHENSYTEST3 ALTER COLUMN EMP_NO SET DATA TYPE VARCHAR(40); 4. Reorg表

Call sysproc.admin_cmd(?reorg table libra.employee?);

10.4 建立索引

CREATE INDEX index_chensy ON CHENSYTEST3(ID);

10.5 删除

DROP TABLE CHENSYTEST1;

10.6 更新

? 更新一项

UPDATE CHENSYTEST3 SET EMP_NO='BK20150001' WHERE ID=1; ? 更新多项

UPDATE A

SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID) ? 更新多项merge into用法

merge into ARIES.BNK_PRDCT A using PARAM.BNK_PRDCT B on A.PRDCT_CD=B.PRDCT_CD

when matched then update set A.DAT_SRC=B.DAT_SRC,A.CODE=B.CODE else ignore;

10.7 回滚

在事务上把自动提交前的“√”取消

第31页

深入浅出DB2

10.8 插入

INSERT INTO EMPLOYEE( EMP_NO, EMP_NAME, DEPT, BIRTHDAY ) VALUES

('BK20150002','陈善语1? , '研发部','2015-09-23'), ('BK20150003','陈善语2? ,?研发部','2015-09-23'), ('BK20150004','陈善语3? ,?研发部','2015-09-23');

10.9 查询

1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。 复制代码代码如下:

select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),'日期不详') birthday from employee

order by dept

2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称 复制代码代码如下: 第32页

深入浅出DB2

select emp_no,emp_name,dept,title from employee

where emp_name<>'喻自强' and dept in (select dept from employee

where emp_name='喻自强')

3、按部门进行汇总,统计每个部门的总工资 复制代码代码如下: select dept,sum(salary) from employee

group by dept

4、查找商品名称为14寸显示器商品的销售情况,显示该商品的编号、销售数量、单价和金额

复制代码代码如下:

select a.prod_id,qty,unit_price,unit_price*qty totprice from sale_item a,product b

where a.prod_id=b.prod_id and prod_name='14寸显示器'

5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额 复制代码代码如下:

select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice from sale_item

group by prod_id

6、使用convert函数按客户编号统计每个客户1996年的订单总金额 复制代码代码如下:

select cust_id,sum(tot_amt) totprice from sales

where convert(char(4),order_date,120)='1996' group by cust_id

7、查找有销售记录的客户编号、名称和订单总额 复制代码代码如下:

select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b where a.cust_id=b.cust_id

group by a.cust_id,cust_name

8、查找在1997年中有销售记录的客户编号、名称和订单总额

第33页

深入浅出DB2

复制代码代码如下:

select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b

where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997' group by a.cust_id,cust_name

9、查找一次销售最大的销售记录 复制代码代码如下:

select order_no,cust_id,sale_id,tot_amt from sales where tot_amt= (select max(tot_amt)

from sales)

10、查找至少有3次销售的业务员名单和销售日期 复制代码代码如下:

select emp_name,order_date from employee a,sales b

where emp_no=sale_id and a.emp_no in (select sale_id from sales group by sale_id having count(*)>=3)

order by emp_name

11、用存在量词查找没有订货记录的客户名称 复制代码代码如下: select cust_name from customer a where not exists (select * from sales b

where a.cust_id=b.cust_id)

12、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要显示时间,日期格式为yyyy-mm-dd按客户编号排序,同一客户再按订单降序排序输出 复制代码代码如下: 第34页

深入浅出DB2

select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt from customer a left outer join sales b on a.cust_id=b.cust_id

order by a.cust_id,tot_amt desc

13、查找16M DRAM的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用男、女表示 复制代码代码如下:

select emp_name 姓名, 性别= case a.sex when 'm' then '男' when 'f' then '女' else '未' end,

销售日期= isnull(convert(char(10),c.order_date,120),'日期不详'), qty 数量, qty*unit_price as 金额

from employee a, sales b, sale_item c,product d

where d.prod_name='16M DRAM' and d.prod_id=c.prod_id and

a.emp_no=b.sale_id and b.order_no=c.order_no

14、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、产品名称、数量、单价、金额和销售日期 复制代码代码如下:

select emp_no 编号,emp_name 姓名, 性别= case a.sex when 'm' then '男' when 'f' then '女' else '未' end,

prod_name 产品名称,销售日期= isnull(convert(char(10),c.order_date,120),'日期不详'), qty 数量, qty*unit_price as 金额

from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d where d.prod_id=c.prod_id and b.order_no=c.order_no

15、查找销售金额最大的客户名称和总货款 复制代码代码如下:

select cust_name,d.cust_sum from customer a, (select cust_id,cust_sum

from (select cust_id, sum(tot_amt) as cust_sum from sales

group by cust_id ) b where b.cust_sum = ( select max(cust_sum)

第35页


DB2学习笔记chensy(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:表7-2 钢筋安装质量检验记录(梁、板)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: