oracle 查询优化改写
-----------书籍: 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 查询优化改写的更多相关文章
- 《Oracle查询优化改写技巧与案例》学习笔记-------使用数字篇
一个系列的读书笔记,读的书是有教无类和落落两位老师编写的<Oracle查询优化改写技巧与案例>. 用这个系列的读书笔记来督促自己学习Oracle,同时,对于其中一些内容,希望大家看到以后, ...
- 【书评:Oracle查询优化改写】第14章 结尾章
[书评:Oracle查询优化改写]第14章 结尾章 一.1 相关参考文章链接 前13章的链接参考相关连接: [书评:Oracle查询优化改写]第一章 http://blog.itpub.net/26 ...
- 【书评:Oracle查询优化改写】第五至十三章
[书评:Oracle查询优化改写]第五至十三章 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知 ...
- 【书评:Oracle查询优化改写】第四章
[书评:Oracle查询优化改写]第四章 BLOG文档结构图 一.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① check的 ...
- 【书评:Oracle查询优化改写】第三章
[书评:Oracle查询优化改写]第三章 BLOG文档结构图 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 隐含参数 ...
- 【书评:Oracle查询优化改写】第二章
[书评:Oracle查询优化改写]第二章 BLOG文档结构图 在上一篇中http://blog.itpub.net/26736162/viewspace-1652985/,我们主要分析了一些单表查询的 ...
- 《 Oracle查询优化改写 技巧与案例 》电子工业出版社
第1章单表查询 11.1 查询表中所有的行与列 11.2 从表中检索部分行 21.3 查找空值 31.4 将空值转换为实际值 41.5 查找满足多个条件的行 51.6 从表中检索部分列 61.7 为列 ...
- Oracle查询优化改写--------------------报表和数据仓库运算
一.行转列 二.列传行 '
- Oracle查询优化改写--------------------高级查询
一.给结果集分页 二.重新生成房间号 三.跳过表中n行 四.排列组合去重
随机推荐
- 页面打印(js/jquery)
1.js实现(可实现局部打印) <html> <title>js打印</title> <head></head><body> ...
- PhpStorm配置PHP解释器(wampServer版)
PHPStorm(以下简称为PS)和wampServer集成环境安装简单,不再赘述. 本人使用PhpStrom版本为2017.1.4版本. PS刚开始使用会使用自带服务器,但是有几率不能自动匹配到PH ...
- JAVA中Socket的用法模拟服务端和客户端
<看透springMvc源代码分析与实践>学习笔记 Socket分为ServerSocket和Socket两个大类 ServerSocket用于服务端,可以通过accept方法监听请求,监 ...
- 【JS小技巧】JavaScript 函数用作对象的隐藏问题
用户反馈 @消失的键盘 在论坛反馈了一个问题,在 AppBoxMvc 中的 Title 模型中,如果将 Name 属性改名为小写的 name 属性,就会报错: 因为这是一个 ASP.NET MVC 的 ...
- 使用C#创建Windows服务
本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NET版本:.NET Framework 4.0 目标平台:X86 ...
- Android系统--输入系统(十五)实战_使用GlobalKey一键启动程序
Android系统--输入系统(十五)实战_使用GlobalKey一键启动程序 1. 一键启动的过程 1.1 对于global key, 系统会根据global_keys.xml发送消息给某个组件 & ...
- mysql sql 基础总结
1 mysql top n使用 select * from table limit n; 2 统配符使用必须和like结合使用 like % 通配符 描述 % 替代一个或多个字符 _ 仅替代一个 ...
- Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...
- Ambari安装之部署 (Metrics Collector和 Metrics Monitor) Install Pending ...问题
问题的由来 我这里,是因为,拿这个Ambari Metrics服务在做试验!所以先删除它,再添加它,出现了安装被挂起的问题.... Am bari里如何删除某指定的服务(图文详解) 问题详细描述如下: ...
- linux基础(五)
一.linux网络原理及基础设置 ifconfig 命令:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址. [root@bogon ~]# ifconfig ens33: flags=41 ...