oracle细节(一)

 

连接oracle:

普通用户:        sqlplus scott/tiger

管理员:           sqlplus sys/password as sysdba

查看当前的用户名:      show user;

查看当前用户下的所有表: select * from tab;

查看表结构: desc emp;

设置行字符数:      set linesize 200;

设置列字符数: col ename for a8;  col : column ename: 列名 for: format  a8:8位字符

设置每页显示的字段数:      set pagesize 20;

改变上一条命令: 定位上一条命令的行号 ====>>  c /修改前内容/修改后内容

修改日期格式:

查看所有的格式参数:  select * from v$nls_parameters;

修改日期格式:        alter session set NLS_DATE_FORMAT='yyyy-mm-dd';

条件查询:

between…and…    select * from emp where sal between 1000 and 3000;

模糊查询条件中有”_”: select * from emp where ename like '%\_%' escape '\'; 转义

大于等于:        select * from emp where sal>=2000;

小于等于:        select * from emp where sal<=2000;

空值:                     select * from emp where comm is null;

不为空:           select * from emp where comm is not null;

替换空值:        select ename, nvl(comm,0) from emp;

单行查询:

字符类:

转为小写: select lower('hello world') from dual;

转为大写: select upper('hello world') from dual;

连接字符串: select concat('hello','world') from dual;

首字母大写: select initcap('hello world') from dual;

截取字符串: select substr('abcdefg',2,3) from dual;  //从第2位开始,截取3位字符,最后的参数可省略

字符串长度:

select length('北京') 字符,lengthb('北京') 字节 from dual;

              length:字符长度    lengthb:字符串的字节数

查找位置: select instr('helloworld','el') from dual;

左填充: select lpad('12345',10,'0') from dual;   //长度为10 ,不足10,左边用0填充

右填充: select rpad('12345',10,'0') from dual;  //长度为10 ,不足10,右边用0填充

去掉前后指定的字符: select trim('y' from 'community') from dual;

替换指定字符: select replace('commooo','o','-') from dual;  //将所有的字符’o’都换成’-’

数字类:

四舍五入:

select     round(45.926,2) ,     //保留两位小数              ===>> 45.93

round(45.926,1) ,     //保留一位小数              ===>> 45.9

round(45.926,0) ,     //向个位四舍五入    ===>> 46

round(45.926,-1) ,    //向十位四舍五入    ===>> 50

round(45.926,-2)        //向百位四舍五入     ===>> 0

       from dual;

截断数字:

       select     trunc(45.926,2) ,       //截断到两位小数     ===>> 45.92

trunc(45.926,1) ,       //截断到一位小数     ===>> 45.9

trunc(45.926,0) ,       //截断到整数位        ===>> 45

trunc(45.926,-1) ,      //截断到十位           ===>> 40

trunc(45.926,-2)        //截断到百位           ===>> 0

       from dual;

日期:

将日期按指定格式的字符显示: select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

日期加数字(): select sysdate+1 from dual; +1是一天后的日期,-1是一天前的日期

       注意:日期可以加数字,但是不能与日期相加

获取间隔月数: select ename, months_between(sysdate,hiredate) from emp;

xx个月之后的日期: select add_months(sysdate,10) from dual; //参数2可以为负,表示xx个月之前

指定日期的月末: select last_day(sysdate) from dual;

下一个指定期别的日期: select next_day(sysdate,'星期二') from dual;  //下个星期二的日期

              next_day 的应用:每个星期一自动备份数据

1. 分布式数据库

2.触发器 快照

日期的四舍五入: select round(sysdate,'month'),round(sysdate,'year') from dual;

日期格式中非日期标识: select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss"今天是"day') from dual;

货币:

添加货币符号: L本地货币符 $美元符 

添加千位符: select ename,to_char(sal,'L9,999') from emp;

设置小数位: select ename,to_char(sal,'L9,999.99') from emp;

处理空值: select ename, nvl2(comm,comm,0) from emp;

              nvl2(参数1,参数2,参数3): 参数1为空,返回参数3,否则,返回参数2

select nullif(10,10) from dual;

nullif(参数1,参数2) 参数1=参数2,返回null,否则,返回a

返回第一个不为空的值: select ename,comm,sal, coalesce(comm,sal) from emp;

if-then-else

1.     select ename,job,sal 涨前,

           case job when 'PRESIDENT' then sal+1000

                        when 'MANAGER' then sal+800

                        else sal+400

                end 涨后

2.     select ename,job,sal 涨前,

           decode(job,'PRESIDENT',sal+1000,

                      'MANAGER',sal+800,

                                sal+400) 涨后

    from emp;

      

多行查询

求最大值: select max(sal) from emp;

求最小值: select min(sal) from emp;

求平均值: select avg(sal) from emp;

求计数值:

select count(*) 记录数, count(comm) 奖金获得者 from emp;   //count会自动滤空

       阻止自动滤空:

       select count(*) 记录数, count(nvl(comm,0)) 奖金获得者 from emp;

分组:

       select deptno,job, avg(sal) from emp group by deptno, job order by deptno;

group by的增强

select deptno,job,sum(sal) from emp group by deptno,job

 +

 select deptno,sum(sal) from emp group by deptno

+

 select sum(sal) from emp

 ==

 select deptno,job,sum(sal) from emp group by rollup(deptno,job)

结果的筛选:

       select deptno,sum(sal) from emp group by deptno having sum(sal)>3000;

       having后跟组函数,where后跟列名

调整格式:

break on deptno skip 2  deptno分组,并有2行空行

       break on null  撤销格式调整

多表查询

等值连接:

       select e.ename, e.deptno, d.dname from emp e, dept d where e.deptno= d.deptno;

不等值连接:

       select * from emp where sal>3000;

左外连接:

       select e.ename,e.deptno,d.dname from emp e, dept d where e.deptno=d.deptno(+);

右外连接:

select e.ename, d.deptno,d.dname from emp e, dept d where e.deptno(+)=d.deptno;

自连接:

select e.ename,e.mgr,b.ename from emp e,emp b where e.mgr=b.empno;

层次查询:

       select level,ename,mgr from emp e     //level是伪列

       connect by prior empno=mgr           //关联关系是前层的empno与后层的mgr相等

       start with mgr is null                  //从根节点开始查询

       order by 1;

子查询

1.where后使用子查询: select * from emp where sal>(select sal from emp where empno=7788);

2.select后使用子查询:

select ename,deptno,(select dname from dept where deptno=10) from emp;

3.having后使用子查询:

       select deptno,avg(sal) from emp group by deptno having avg(sal)>(select sal from emp where empno=7844);

4.from后使用子查询: select * from (select ename,deptno,sal from emp);

5. 和集合中的所有值比较:all

select ename,deptno,sal from emp where sal>all(select sal from emp where deptno=30);

//查询比集合中的所有数值都大的集合,等同于:

select ename,deptno,sal from emp where sal>(select max(sal) from emp where deptno=30);

6. 和集合中的任意一个值比较:any

select ename,deptno,sal from emp where sal>any(select sal from emp where deptno=10);

//查询比集合内任意一个数字大的emp集合,等同于

select ename,deptno,sal from emp where sal>(select min(sal) from emp where deptno=10);

7.not in in

       xx not in(a,b,c)==>> 等同于 xx !=a and xx != b and xx!=c,因此,若其中有一个为null,则全为假

       xx in (a,b,c)===>>等同于 xx == a or xx ==b or xx ==c

//处理not in中的null

select ename,mgr from emp where empno not in(select mgr from emp where mgr is not null);

//innull值可不做处理

select empno,ename,mgr from emp where empno in (select mgr from emp);

8.相关子查询

select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e

 where sal> (select avg(sal) from emp ep where deptno=e.deptno);

:

select e.empno,e.ename,e.sal,d.avgsal

   from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d

   where e.deptno=d.deptno and e.sal > d.avgsal;

9. wm_concat 组函数(行转列) 

select deptno,wm_concat(ename) namelist from emp group by deptno;

DEPTNO   NAMELIST

---------- ---------------------------------------

        10 CLARK,KING,MILLER

        20 SMITH,FORD,ADAMS,SCOTT,JONES

        30 ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD

10.top-n问题

备注:rownum是默认的索引,只能从第一条开始查询,因此,对于rownum只能<<=,不能>>=

因此,分页显示第6条到第10条的数据

select *

     from

      (select rownum r,empno,ename,sal

             from

             (select * from emp order by sal desc))

      e1

  where e1.r>=6 and e1.r<=10

你可能感兴趣的:(oracle基础细节part1)