• oracle DBA操作


    sqlplus sys/tiger  as sysdba;

    alter user scott account unlock;

    用户已更改

    切换用户:conn scott/tiger as sysdba;

    修改密码:alter user scott indentified by tiger;

    1.select语句:

    select sysdate from dual;

     

    select ename,sal*12+comm from emp;(结果是错的,如果comm为空,那么sal*12+comm整个为空)

     

    select ename ,sal*12 year_sal from emp;

    select ename||sal from emp;

    select ename || 'dhfaf' from emp;

    select ename || 'jfkjdk''kdjfkla' from emp;

    distinct:

    select distinct deptno from emp; //重复的编号去除掉

    select distinct deptno,job from emp; //查询的结果会出现重复的编号和job,但deptno+job不重复

    2.where:

    select ename,sal,comm from emp where comm is null;

    select ename,sal,comm from emp where comm is not null;

    select ename,sal,comm from emp where sal in (800,1500,2000);

    select ename,sal,comm from emp where sal in ('Simth','abc','ABC');

    如果想查询大于2007225日以后入职的员工信息

    首先select sysdate from emp;

    查询结果是25-2-07

    必须得按这个字符串特定的格式来写:

    select ename,sal,hiredate from emp where hiredate >'25-2-07';

    select ename,sal,from emp where deptno =10 and/or sal>1000;

    where ename,sal from emp where sal not in (800,1500); //取反

    select ename from emp where ename like '%ALL%'; //%表示0个或多个字母

    select enaem from emp where ename like '_A%'; //_表示占位符

    如果名字里面本身就有%

    select ename from emp where ename like '%$%%' escape '$'; //指定$为转义字符

    3.order by

    select * from dept order by deptno desc; //降序排列

    select empno,ename from emp order by empno asc; //升序排列

     

    select empno,ename from emp where empno <>10 order by empno asc;//先过滤再排序

     

    按照两个字段排序:

    select ename,sal,deptno from emp order by deptno asc,ename desc; //先按deptno升序排列再在相同deptnoename用降序排列

     

    4.函数lower

    select lower(ename) from emp;

     

    查询员工名字不管大小写,第二个字母是a的名字:

    select ename from emp where lower(ename) like '_a%';

    或者:select ename from emp where ename like '_a%' or ename like '_A%';

     

    5.函数substr 截取

    select substr(ename,1,3) from emp; //从第一个字符开始截取,长度为3

     

    6.函数chr 把而刺客码转换成字符

    select chr(65) from dual; //查询结果为A

     

    7.函数ascii 把字符转换成ASCII

    select ascii('A') from dual; //查询结果为65

     

    8.函数round 四舍五入

    select round(23.652) from dual; //24

    select round(23.652,2) from dual; //23.65

    select round(23.652,1) from dual; //23.7

    select round(23.652,-1) from dual; //20

     

    *9.函数to_char 把数字/日期转换成相关字符串并且有特定的格式控制

    select to_char(sal,'$99,999.9999') from emp;

    select to_char(sal,'L99,999.9999') from emp; //人民币

     

    select to_char(hiredate,'YYYY-MM-DD HH24:MI:SS') from emp;

     

    *10.to_date 把特定格式的字符串转换成日期类型

    select ename,hiredate from emp where hiredate >to_date('1981-2-20 12:34:56','YYYY-MM-DD HH24:MI:SS');

     

    *11.to_number 把特定格式的数字转换成相关类型

    select sal from emp where sal > to_number('$1.250.00','$9,999.99');

     

    *12.nvl 处理空值的

    select ename,sal*12 + nvl(comm,0) from emp;

     

    *13.组函数:输入好多条记录,输出只有一条

    max(sal) min(sal) avg(sal) sum(sal)

    select to_char(avg(sal),'99999.99999') from emp;

    select round(avg(sal),2) from emp;

     

    count(*) //表示表中有多少条记录(不是空值的有多少个)

    select count(*) from emp where deptno=10;

    select count(distinct deptno) from emp; //部门编号有多少个

     

    *14.group by

    哪个部门的平均薪水是多少:

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

    按照多个字段进行分组:

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

     

    输出薪水最高的员工的名字:

    错误的是:select enamemax(sal) from emp;

    真确的是:select ename from emp where sal=(select max(sal) from emp);

     

    输出每个部门挣钱最多人的名字

    错误的是:select ename,max(sal) from emp group by deptno;

    真确的是:select deptno,max(sal) from emp group by deptno; //deptno在分组里面确确实实只有一个

     

    *15.having 是对分组进行限制

    where语句是对单条的记录进行过滤

     

    把平均薪水大于2000的部门取出来

    select avg(sal,deptno from emp group by deptno having avg(sal)>2000;

     

    顺序:select * from emp

          where sal>1000

          group by deptno

          having

          order by

     

    例子:薪水大于1200的雇员按照部门编号进行分组,分组后的平均薪水必须大于1500,查询分组后的平均工资,按工资的倒叙排列

    select avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;

     

    *16.子查询:在一个select语句里面套了个另外的select语句

                套的select语句可以出现在where里面,也可以出现在from语句后面

     

        求哪些人的工资位于所有平均工资之上

      select ename,sal from emp where sal>(select avg(sal) from emp);

     

        按照部门进行分组后,每个部门里面挣钱最多的员工信息

      select ename,sal,deptno from emp

      where sal=(select max(sal) from emp group by deptno);//错误的

     

      select ename,sal,deptno from emp

      where sal in (select max(sal) from emp group by deptno);//错误的

     

      select ename,sal from emp

      join (select max(sal)  max_sal,deptno from emp group by deptno ) t

      on (emp.sal=t.max_sal and emp.deptno=t.deptno); //正确的

     

    *17.自连接:其实是在同一张表内进行两次查询.为同一张表其不同别名,当成两张表来用

     

        找出员工对应的经理的名字(分析:员工名字和经理名字都在emp表的ename列中)

        select t1.ename,t2.ename from emp t1 ,emp t2 where t1.mgr=t2.empno;

     

    *18.

        等值连接:找出每位员工的部门名称

        select ename,dname from emp join dept on (emp.deptno=dept.deptno);

        求部门平均薪水的等级

        select deptno,avg_sal,grade from

        (select avg(sal) avg_sal,deptno from emp group by deptno) t

        join salgrade s on (t.avg_sal between s.losal and s.hisal);

     

        求部门平均的薪水等级

        select deptno ,avg(grade) from

        (select deptno,ename,grade from emp join salgrade s on (emp.sal between s.losal and s.hisal)) t

        group by deptno;

     

        雇员中有哪些人是经理

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

     

        面试题:

        1.不用组函数,求出薪水的最高值(自连接)

        左边的一张表中的薪水小于右边一张表的薪水,有一条或多条是连接不上右边,就是左边的最大值没有小于右边表的薪水

        select distinct sal from emp where sal not in

        (select distinct e1.sal from emp e1 join emp e2 on (e1.sal<e2.sal));

     

         2.平均薪水最高的部门的部门编号

         select deptno,avg_sal from

         (select avg(sal) avg_sal,deptno from emp group by deptno) t

         where avg_sal=

         (select max(avg_sal) from t);

     

          

         3.平均薪水最高的部门的部门

    select dname from dept where deptno =

    (

         select deptno,avg_sal from

         (select avg(sal) avg_sal,deptno from emp group by deptno) t

         where avg_sal=

         (select max(avg_sal) from t)

    );

  • 相关阅读:
    myeclipse源码相关操作
    来自一个程序员内心深处的心声
    编程乐趣--汉字转拼音
    MyEclipse下安装FreeMark插件
    java web 加载Spring --web.xml 篇
    注解方式实现声明式事务管理
    spring与struts简单整合案例
    创建对象与对象依赖关系
    几种对数据的处理以及对数据的封装
    Action开发、通配符、路径问题和struts中常量用法
  • 原文地址:https://www.cnblogs.com/dingzp/p/11245555.html
Copyright © 2020-2023  润新知