-----------书籍: oracle 查询优化改写
-----------第1个“C###oracle”为登录数据库的用户名,第2个“oracleChange”为登录数据库的密码“oracleChange”为欲登录的数据库名称。

/*
create tablespace oracleChange
datafile 'F:\devlopment\databases\oracle\oracleChange\oracleChange.def' size 100M --生成数据文件并定义文件大小
autoextend on next 100M maxsize unlimited logging --设置自动扩展
extent management local autoallocate
segment space management auto;

create user C###oracle identified by oracleChange default tablespace oracleChange quota 500m on users;
---- 这里第一个PERSONNEL_MANAGE为用户名,第二个MWQ为密码,第三个DBSQL为表空间名。然后执行。
grant all privileges to C###oracle;
--- 执行该语句给PERSONNEL_MANAGE用户授权,此时PERSONNEL_MANAGE用户就可以登录了。

---创建 emp员工表
create table emp (
empno number(4) ,
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date ,
sal number(7,2),
comm number(7,2) default 0 ,
deptno number(2) );

-- Add comments to the columns
comment on column emp.empno is '编码';
comment on column emp.ename is '名称';
comment on column emp.job is '工作';
comment on column emp.mgr is '主管';
comment on column emp.hiredate is '聘用日期';
comment on column emp.sal is '工资';
comment on column emp.comm is '提成';
comment on column emp.deptno is '部门编码';
----------------------------------------
----插入数据
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'smith', 'clerk', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'allen', 'salesman', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600, 300, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'ward', 'salesman', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250, 500, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7556, 'jones', 'manager', 7698, to_date('02-04-1981', 'dd-mm-yyyy'), 2975, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'maritn', 'salesman', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250, 1400, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'blake', 'manager', 7839, to_date('01-01-1981', 'dd-mm-yyyy'), 2850, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7782, 'clark', 'manager', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450, null, 10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'scott', 'analyst', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'king', 'president', null, to_date('17-11-9181', 'dd-mm-yyyy'), 5000, null, 10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'turner', 'salesman', 7698, to_date('08-09-9181', 'dd-mm-yyyy'), 1500, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'adams', 'clerk', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'james', 'clerk', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'ford', 'analyst', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'milier', 'clerk', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300, null, 10);

*/

-----从表中检索部分行 (查询数据时 只需要添加过滤条件即可)
select * from emp where job ='salesman';

----查找空值

select * from emp where comm is null;---null 不支持加减乘除 大小比较 相等比较 否则只能为空
select * from emp where comm is not null;--- 不为空值查询

---将空值转换为实际值
select nvl(emp.comm,0),emp.* from emp where comm is null;---单列 查询
select coalesce(comm,comm) as c ,emp.* from emp where comm is null;

----查找满足多个条件的行
--- 查询 员工表 部门为10 的所有员工、所有得到提成的员工、以及部门为20的工资不超过2000美元的员工;
select *
from emp
where (deptno = 10
or comm is not null
or (sal <= 2000 and deptno = 20));

--- 从表中检索部分列 <在实际开发中;常常只需返回部分需要的列的数据>
select empno ,ename,hiredate,sal from emp where deptno =10;

--- 为列取 有意义的名称
select ename as 姓名 ,deptno as 部门编号 from emp order by 2 ;
----在where子句中引用取别名的列
select * from (select sal as 工资,comm as 提成 from emp ) x where 工资 <1000;

----拼接列
select ename || '的工作是' ||job as msg from emp where deptno =10;
---动态生成 删除表数据的语句
--- select 'truncate table '|| owner ||'.'|| table_name || ';'as 清空表 from all_tables;

----在select 语句中使用条件逻辑
select 档次,count(*) as 人数
from (select (case
when sal <=1000 then '0000-1000'
when sal <=2000 then '1000-2000'
when sal <=3000 then '2000-3000'
when sal <=4000 then '3000-4000'
when sal <=5000 then '4000-5000'
else '好高'
end ) as 档次,ename,sal from emp )
group by 档次
order by 1;

----限制 返回的行数 (rownum 依次返回的每一条数据做一个标识;)
select * from emp where rownum <=2;---取出前2行的数据
select * from (select rownum as sn ,emp.* from emp where rownum <=100) where sn=2;---取出第二行的数据

----从表中随机返回n条记录(先dbms_random 来对数据进行随机排序,然后取其中三行)
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
select empno,ename from (select empno ,ename from emp order by dbms_random.value())where rownum <=3;
---------等价于下面语句 ---下面语句执行顺序: 1:select 2:rownum 3:order by ;即:先取出数据 然后生成序号 最后才是排序
select empno ,ename,dbms_random.value ran from emp where rownum <=3 order by ran;

-----模糊查询 like '% 表示任意数量的字符' '_表示任意一个字符' 'M 表示任意长度的字符'
---创建视图
create or replace view empView1 as
select 'abcedf' as vname from dual
union all
select '_bcefg' as vname from dual
union all
select '_bcedf' as vname from dual
union all
select '_\bcedf' as vname from dual
union all
select 'xyceg' as vname from dual ;
--- 要求1:查出vname 中包含字符串 ’ced‘的
select * from empView1 where vname like '%ced%';
--- 要求2:查出vname z中包含字符串“_bce” 的
select * from empView1 where vname like '\_bce%' escape '\'; ---注:escape 把'\'标识为转义字符 ;而'\'把'_'转义为'字符';而非其原意

-----以指定的次序返回查询结果
---实际提取数据或者生产报表时;一般是根据一定顺序查看
---如:查看单位所有员工信息
select empno,ename,hiredate from emp where deptno=10 order by hiredate asc;
select empno,ename,hiredate from emp where deptno=10 order by 3 asc; ---该写法 中的数字只能出现在order by 中
------
select empno ,ename,sal from emp where deptno=10 order by 3 asc;---
select empno,ename ,sal from emp where comm is not null order by 3 asc;
----注:如果order by 后使用的列名 就需要注意前后保持一致;否则开发中带来些 小麻烦

--- 按多个字段排序、 ordey by desc/asc/number
---按部门编号升序并按工资降序排序
select empno ,deptno ,ename,job from emp order by 2 asc,3 desc;
-----按子串排序
select last_name as 名称,
phone_number as 号码,
substr(phone_number, -4) as 尾号
from hr.employees
where rownum <= 5
order by 4;

----translate
---translate(expr,from_string,to_string) --from_string to_string 以字符为单位 对应字符一一替换
select translate('ab您好!bcadefg','abcdefg','1234567890') as new_str from dual;
---如果to_string 为空 则返回空值
select translate('ab您好!bcadefg','abcdefg','') as new_str from dual;
---如果to_string 对应的位置没有字符,删除from_string 中列出的字符
select translate('ab您好!bcadefg','abcdefg','1') as new_str from dual;

----按数字和字母混合字符串中的字母排序
create or replace view numberView
as
select empno || '' || ename as data from emp;

---- 查看视图
select * from numberView;
---要求:按其中字母排序
select nv.data ,translate(nv.data,'-1234567890','-') as ename from numberView nv order by 2;
select nv.data from numberView nv order by translate(nv.data,'-1234567890','-');

---处理排序空值
select ename,sal,comm,nvl(comm,-1) order_col from emp order by 4 asc;
select * from emp order by nvl(comm,-1) desc;
---使用关键字 nulls first; nulls last
select *  from emp order by comm nulls first;
select * from emp order by comm nulls last;

oracle 查询优化改写的更多相关文章

  1. 《Oracle查询优化改写技巧与案例》学习笔记-------使用数字篇

    一个系列的读书笔记,读的书是有教无类和落落两位老师编写的<Oracle查询优化改写技巧与案例>. 用这个系列的读书笔记来督促自己学习Oracle,同时,对于其中一些内容,希望大家看到以后, ...

  2. 【书评:Oracle查询优化改写】第14章 结尾章

    [书评:Oracle查询优化改写]第14章 结尾章 一.1  相关参考文章链接 前13章的链接参考相关连接: [书评:Oracle查询优化改写]第一章 http://blog.itpub.net/26 ...

  3. 【书评:Oracle查询优化改写】第五至十三章

    [书评:Oracle查询优化改写]第五至十三章 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知 ...

  4. 【书评:Oracle查询优化改写】第四章

    [书评:Oracle查询优化改写]第四章 BLOG文档结构图 一.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① check的 ...

  5. 【书评:Oracle查询优化改写】第三章

    [书评:Oracle查询优化改写]第三章 BLOG文档结构图       导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 隐含参数 ...

  6. 【书评:Oracle查询优化改写】第二章

    [书评:Oracle查询优化改写]第二章 BLOG文档结构图 在上一篇中http://blog.itpub.net/26736162/viewspace-1652985/,我们主要分析了一些单表查询的 ...

  7. 《 Oracle查询优化改写 技巧与案例 》电子工业出版社

    第1章单表查询 11.1 查询表中所有的行与列 11.2 从表中检索部分行 21.3 查找空值 31.4 将空值转换为实际值 41.5 查找满足多个条件的行 51.6 从表中检索部分列 61.7 为列 ...

  8. Oracle查询优化改写--------------------报表和数据仓库运算

    一.行转列 二.列传行 '

  9. Oracle查询优化改写--------------------高级查询

    一.给结果集分页 二.重新生成房间号 三.跳过表中n行 四.排列组合去重

随机推荐

  1. 钉钉企业应用C#开发笔记之一(免登)

    关于钉钉 钉钉是阿里推出的企业移动OA平台,本身提供了丰富的通用应用,同时其强大的后台API接入能力让企业接入自主开发的应用成为可能,可以让开发者实现几乎任何需要的功能. 近期因为工作需要研究了一下钉 ...

  2. [随笔]_ELVE_git命令复习

    mkdir: XX (创建一个空目录 XX指目录名) pwd: 显示当前目录的路径. git init 把当前的目录变成可以管理的git仓库,生成隐藏.git文件. git add XX 把xx文件添 ...

  3. Spring Security4实例(Java config版)——ajax登录,自定义验证

    本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...

  4. UVa455 Periodic String 的wronganswer问题探讨,以及AC的新思路

    题目的意思是一个字符串有某个长度为k的字符串通过不断重复形成的,而k被称为该字符串的周期.而我们所要做的是找出该字符串的最小周期. input The first line is an integer ...

  5. springmvc 前端 发ajax请求的几种方式

    一.传json单值或对象 1.前端 var data = {'id':id,'name':name}; $.ajax({ type:"POST", url:"user/s ...

  6. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  7. CJOJ 2044 【一本通】最长公共子序列(动态规划)

    CJOJ 2044 [一本通]最长公共子序列(动态规划) Description 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X,则另一序列Z是X的子序列是指存在一个 ...

  8. 中缀表达式变后缀表达式、后缀表达式(逆波兰)求值(python版本)

    定义: 中缀表达式: 在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为中缀表达式 后缀表达式: 又叫逆波兰表达式 ,不包含括号,运算符放在两个运算对象的后面,所有的计算 ...

  9. Java IO 之 BIO、NIO、AIO

    1.BIO.NIO.AIO解释 Java BIO : 同步并阻塞 (Blocking IO) 一个连接一个线程 即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不 ...

  10. linux下查看jdk路径

    jdk安装后 centos中: 执行 rpm -ql java-1.7.0-openjdk-devel | grep '/bin/javac' 命令确定, 执行后会输出一个路径,除去路径末尾的 &qu ...