您的位置:首页 > 数据库 > Oracle

Oracle系列:(18)小结

2016-09-08 01:14 351 查看
Java访问Oracle服务器--orcl数据库---emp表

private static String driver =
"oracle.jdbc.driver.OracleDriver";//访问oracle服务器的驱动名称

private static String url =
"jdbc:oracle:thin:@127.0.0.1:1521:orcl";//访问oracle服务器的连接字串

private static String username = "scott";//访问orcl数据库的用户名

private static String password = "tiger";//访问orcl数据库的密码

连接类型:在oracle中,用怎样的方式连接服务器,有二种方式连接oracle服务器,分别是thin和oci
1)thin:通过oracle公司提供的jar包
连接简单,但速度相对慢
将该目录下的E:\oracleDB\product\11.2.0\dbhome_1\jdbc\lib\ojdbc5.jar包导入到
你项目的/WEB-INF/lib目录下

2)oci:安装oracle公司提供的客户端安装程序,类似于QQ客户端,得事先安装
连接复杂,但速度相对高
且将该目录下的E:\oracleDB\product\11.2.0\dbhome_1\jdbc\lib\ojdbc5.jar包导入到
你项目的/WEB-INF/lib目录下,与客户端一起使用,速度较快

程序员使用thin方式即可

(01)select to_char(sysdate,'yyyy"年"mm"月"dd"日"day') from dual是什么意思?

(02)decode()函数有什么作用?max(hiredate)和min(hiredate)函数能用于数值型,还能用于什么类型?
decode()能够进行分支选择;日期
(03)select deptno,avg(sal) from emp group by deptno是什么意思?
查看每个部门的平均工资
(04)where和having的哪个先执行?非等值连接只能使用<>或!=吗?
where先执行;非等值连接可以用between..and...
(05)外连接能解决什么问题?
不符合某些条件的数据
(06)子查询能解决什么问题?
分页
(07)select *
from emp
where sal < any/all (800,1200,1500,2200,3000,5000)是什么意思?
小于任意/所有
(08)自连接有什么特点?

(09)A集合 union B集合时,最终结果的列名由A集合还是B集合决定?
A
(10)rownum=1 和 rownum <=4 和rownum >= 4 哪个能查询出记录?

(01)找到员工表中工资最高的前三名
ROWNUM EMPNO ENAME SAL
------ ----- ---------- -------
1 7839 KING 5000
2 7788 SCOTT 3000
3 7902 FORD 3000
select rownum ,empno,ename,sal
from (select * from emp order by sal desc)
where rownum<=3;


(02)找到员工表中薪水大于【本】部门平均薪水的员工
EMPNO ENAME SAL AVGSAL
----- ---------- ------- ----------
7499 ALLEN 1600 1566
7566 JONES 2975 2175
7698 BLAKE 2850 1566
7788 SCOTT 3000 2175
7839 KING 5000 2916
7902 FORD 3000 2175

我的方法:
select empno,ename,trunc(sal,0) "sal",
trunc((select avg(e1.sal) from emp e1 where e1.deptno=e2.deptno),0) "avgsal"
from emp e2
where sal>(select avg(e1.sal) from emp e1 where e1.deptno=e2.deptno);
老师的方法:
select e.ename "姓名",xx.deptno "部门号",round(xx.avgsal,0) "部门平薪",e.sal "员工薪水",e.deptno "员工所在部门号"
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) xx
where (e.deptno=xx.deptno) and (e.sal>xx.avgsal);


(03)有一个员工表empinfo结构如下
create table empinfo(
fempno varchar2(10) not null primary key,
fempname varchar2(20) not null,
fage number(2) not null,
fsalary number(10,2) not null
);
insert into empinfo(fempno,fempname,fage,fsalary) values('1','AA',30,7000);
insert into empinfo(fempno,fempname,fage,fsalary) values('2','BB',31,8000);
insert into empinfo(fempno,fempname,fage,fsalary) values('3','CC',32,9000);
insert into empinfo(fempno,fempname,fage,fsalary) values('4','DD',33,10000);
insert into empinfo(fempno,fempname,fage,fsalary) values('5','EE',34,11000);
insert into empinfo(fempno,fempname,fage,fsalary) values('6','FF',35,12000);
insert into empinfo(fempno,fempname,fage,fsalary) values('7','GG',36,13000);
insert into empinfo(fempno,fempname,fage,fsalary) values('8','FF',37,14000);

假如该表有大约1000万条记录,写一条最高效的SQL语句,计算以下4种人中每种员工的数量
第1种人:fsalary>9999 and fage>35
第2种人:fsalary>9999 and fage<35
第3种人:fsalary<9999 and fage>35
第4种人:fsalary<9999 and fage<35

提示:只用一条SQL搞定

第1种人 第2种人 第3种人 第4种人
2 2 0 3

我的方法
select
(select count(fempno) from empinfo where fsalary>9999 and fage>35) "第1种人",
(select count(fempno) from empinfo where fsalary>9999 and fage<35) "第2种人",
(select count(fempno) from empinfo where fsalary<9999 and fage>35) "第3种人",
(select count(fempno) from empinfo where fsalary<9999 and fage<35) "第4种人"
from dual;


老师的方法:
select
sum(case when e.fsalary>9999 and e.fage>35 then 1 else 0 end) "第1种人",
sum(case when e.fsalary>9999 and e.fage<35 then 1 else 0 end) "第2种人",
sum(case when e.fsalary<9999 and e.fage>35 then 1 else 0 end) "第3种人",
sum(case when e.fsalary<9999 and e.fage<35 then 1 else 0 end) "第4种人"
from empinfo e;


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle