/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

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;



posted @ 2016-04-09 00:01  一品堂.技术学习笔记  阅读(627)  评论(0编辑  收藏  举报