表查询
1.oracle表的简单查询
①查看表结构
desc dept;
②查询所有列
select * from dept;--影响速度
set timing on;-- 打开执行时间的命令行
insert into users values(userid,username,userpassw)
select * from users;--疯狂复制
select count(*) from users;--查看表的记录数
③查询指定列
select ename,sal,job,deptno from emp;
④取消重复行
select distinct deptno,job from emp;
⑤查询特定记录
select deptno,job,sal from emp where ename = ‘SIMTH’;
--sql语句大小写不区分,但‘’内匹配的内容是区分的
⑥使用算术表达式
select ename,(sal+ nvl(comm,0))*12“年工资” form emp;
--注意算术表达式、别名“年薪”和nvl()函数处理null值
--oracle表达式有一值为null,则整个表达式也为空
⑦使用where子句
select ename ,sal from emp where sal > 3000;--薪水高于3000的员工
select ename ,sal from emp where sal > 2000 and sal < 3000;
或者
select * from (select ename,sal from emp where sal > 2000)
where sal < 3000;
--薪水在2000到2500区间的员工
select ename ,hiredate from emp
where hiredate > '1-1月-1982';--1982.1.1后入职的员工
⑧使用like操作符和in
显示员工名第一个大写字母为S的员工姓名和薪水
select ename ,sal from emp where ename = 'S%';
显示员工名第三个大写字母为O的员工姓名和薪水
select ename,sal from emp where ename = ‘__O%’;
显示员工号为123,456,789员工信息
select * from emp where empno in(123,456,789);
显示没有上级的员工信息
select * from emp where mgr is null;
⑨使用逻辑操作符号
select * from emp where (sal > 500 or job = 'MANAGER')and ename like 'J%';
⑩排序
默认order by asc 如果从高到低则为order by desc;
select * form emp order by deptno ,sal desc;
使用别名排序
select ename,(sal +nvl(comm,0))*12"年薪" from emp order by “年薪”;
别名前可以加as 别名为英文不要双引
select ename,(sal +nvl(comm,0))*12 yearsal from emp order by yearsal;
2.oracle表复杂的查询
①数据分组 max, min,avg, sum, count
select max(sal),min(sal) from emp;
select ename ,sal from emp where sal = (select max(sal) from emp);
select ename ,sal from emp where sal > (select avg(sal) from emp);
②group by 和 having子句
group by 用于对查询的结果进行统计
having 子句用于限制分组显示结果
显示每个部门的平均工资和最高工资
select avg(sal),max(sal),depno from group by deptno;
显示每个部门的每个岗位的平均工资
select avg(sal),max(sal)from emp group by deptno,job;
显示每个部门的平均工资多余于2000部分
select avg(sal),max(sal),deptno from emp
group by deptno having avg(sal)> 2000;
③对数据分组的总结
分组函数只能出现在选择列表、having、order by 子句中
如果在select语句中同时包含有group by,having,order by
那么他们的顺序是group by ,having,order by
在选择列中如果有列、表达式和分组函数,那么这些“列和表达式”必须有一个出现在group by子句中,
否则就会出错
例如:select avg(sal),max(sal),deptno from emp
group by deptno having avg(sal)> 2000;
deptno必须出现在group by 中
④多表查询
笛卡尔积:多表查询的选择列不能少于表的个数少一。
显示雇员名,雇员工资及所在部门
select t1.ename,t1.sal,t2.dname from emp t1,dept t2 where t1.deptno = t2.deptno;
显示雇员名,雇员工资及所在10号部门
select t1.ename,t1.sal,t2.dname from emp t1,dept t2
where t1.deptno = t2.deptno and t1.deptno =10;
显示员工的姓名,薪水及薪水等级
select a1.ename ,a1.sal,a2.grade from emp a1,salgrade a2
where a1.sal between a2.losal and a2.htsal;
多表排序
select a1.ename,a2.dname,a1.sal from emp a1,dept a2
where a1.deptno = a2.deptno order by a1.deptno;
自连接
查询FORD的上级
select worker.ename,leader.ename from emp worker ,emp leader
where worker.mgr = leader.empno and worker.ename = 'FORD';
⑤子查询(也叫嵌套查询)
Ⅰ.单行子查询
只返回一行数据的子查询语句
select ename from emp
where deptno =(select deptno from emp where ename ='SMITH');
解析sql语句的编译器是从右往左编译
Ⅱ.多行子查询
查询工作与10号部门相同的员工的名字、岗位、工资和部门号
select ename,job,sal,deptno from emp
where job in (select job from emp where deptno = 10);
Ⅲ.在多行子查询中使用all操作符 any操作符
如何显示工资比部门30的所有员工的工资高的员工姓名、工资和部门号
select ename,sal,deptno from emp
where sal > all(select sal from emp where deptno = 30);
下面的查询方法效率更高:
select ename,sal,deptno from emp
where sal > (select max(sal) from emp where deptno = 30);
举几个例子来说明ALL和ANY的用法
SELECT * FROM TABLEA WHERE FLD > ALL(SELECT FLD FROM TABLEA);
这相当于
SELECT * FROM TABLEA WHERE FLD > (SELECT MAX(FLD) FROM TABLEA);
SELECT * FROM TABLEA WHERE FLD > ANY(SELECT FLD FROM TABLEA);
这相当于
SELECT * FROM TABLEA WHERE FLD > (SELECT MIN(FLD) FROM TABLEA);
SELECT * FROM TABLEA WHERE FLD = ANY(SELECT FLD FROM TABLEA);
这相当于
SELECT * FROM TABLEA WHERE FLD IN (SELECT FLD FROM TABLEA);
Ⅳ.多列子查询
查询如SMITH的部门和岗位完全相同的所有雇员
select * from emp where (deptno,job) =
(select deptno,job from emp where ename ='SMITH');
Ⅴ.在from子句中使用子查询(内嵌视图)
显示高于自己部门平均工资的员工信息
select * from emp a1, (select deptno,avg(sal) mysal from emp group by deptno)a2
where a1.sal> a2.mysal and a1.deptno = a2.deptno order by a1.deptno;
注意:给列取别名可以加as 但表不可以
Ⅵ.用查询结果创建新表
这个命令是一种快捷的建表方法
create table mytable(id,name,sal,job,deptno)
as select empno,ename,sal,job,deptno from emp;
Ⅶ.使用子查询插入数据
insert into kkk(myid, myname,mydeptno) select empno,ename,deptno from emp;
Ⅷ.使用子查询更新数据
update table emp set (job,sal,comm)=(select job,sal,comm from emp
where ename = 'SMITH') where ename = 'SCOTT';
⑥合并查询
为了合并多个select语句的结果,可以使用集合操作符号union,union all,
intersect,minus
Ⅰ.union
取结果集的并集,自动去掉结果集中的重复行。
select ename,sal,job from emp where sal > 2500 union
select ename,sal,job from emp where job = 'manager';
Ⅱ.union all
和union相似,但不会取消重复行,也不会排序 加法
select ename,sal,job from emp where sal > 2500
union all select ename,sal,job from emp where job = 'manager';
Ⅲ.intersect
取两个结果集的交集
select ename,sal,job from emp where sal > 2500
intersect select ename,sal,job from emp where job = 'manager';
Ⅳ.minus
去两个结果集的差集 减法
select ename,sal,job from where sal > 250 minus
select ename,sal,job from emp where job = 'manager';
3.oracle分页
oracle 分页一共有三种方式
①rownum 是oracle分配的,只能使用一下
select t1.*,rownum rn from (select * from emp)t1;
select t1.*,rownum rn from (select * from emp)t1 where rownum <= 10;
select * from (select t1.*,rownum rn from (select * from emp)t1
where rownum <= 10) where rownum >=6;
select * from (select t1.*,rownum rn from (select * from emp order by sal desc)t1
where rownum <= 10) where rownum >=6;
执行时间0.1秒
②按分析函数分
select * from (select t1.*,row_number() over (order by cid desc) rk from
t_xiaoxi t1) where rk <10000 and rk>9980;
执行时间1.01
③根据rowid来分
select * from t_xiaoxi where rowid in
(selcet rid from
(select rownum rn,rid from
(select rowid rid,cid from t_xiaoxi order by cid desc)
where rownum <10000)where rn >9980)order by desc;
执行时间0.03秒
4.创建新的数据库
创建数据库的两种方法:
通过oracle提供的向导工具和手工步骤直接创建
①向导工具:Database Configuration Assistant 即dbca 数据库配置助手
一般选择不包括定制文件的定制数据库(new Database)
②手工步骤设计函数,将在函数部分补充