Oracle 经典技术

  1. 查询员工姓名中第三个字母是e的员工姓名(不能使用like,substr函数);
    可以利用instr函数进行筛选,如果不要红色的数字3也可以筛选出来,但是筛选出数据不是完整数据,图(1)的结果是正确,图(二)结果筛选数据不完整漏Neena,会筛漏第三字母e的左相邻也是e的数据如Neena;
    Select first_name from emp where instr(first_name,’e’,3)=3;
    在这里插入图片描述
    图(一)
    在这里插入图片描述

图(二)
2. 将字符串2019-3月-26 15:13:12 转换为日期格式;
Select to_date(‘2019-3月-26 15:13:12’,’yyyy-mm”月”-dd hh24:mi:ss’) from emp;
注:时间格式为24小时制,需要在转化中hh加上24

  1. 员工入职的日期的下一个星期日的日期
    Select next_day(hire_date,’星期日’) from emp
    注:没有注意到只能以汉子形式的星期日

4.oracle中分不清楚左右链接的主表是哪个,与SQL999语法 左右链接不同
4.1.orale(左)外链接:select table1.column, table2.column
from table1, table2 -------table1为主表
where table1.column = table2.column(+);

4.2.orale(右)外链接:select table1.column, table2.column
from table1, table2 -------table2为主表
where table1.column (+)= table2.column;
4.3. SQL:1999语法(左)外链接: select table1.column, table2.column
from table1 left join table2 -------table1为主表
on table1.column= table2.column;

4.4. SQL:1999语法(左)外链接: select table1.column, table2.column
from table1 right join table2 -------table2为主表
on table1.column = table2.column;
5.非法使用组合数:
例如:查询各个部门的平均工资大于5000以上的部门
错法:select department_id,avg(salary) from emp
Where avg(salary)>5000-----------------不能在 where子句中使用组函数
Group by department_id;
正法:select department_id,avg(salary) from emp
Group by department_id
Having avg(salary)>5000;

6.求平均值值问题,忽略空值
例子:查询各个部门的奖金系数的平均值
错法(avg组函数忽略空值):select department_id ,avg(commission_pct) from emp
Group by department_id;
正法:select department_id avg(nvl(commission_pct,0))from emp
Group by department_id;
注意:count(expr)组合数返回expr不为空的记录总数

7.关于drop,truncate,delete 可不可以回滚?
drop和truncate (删除不可以回滚),delete(删除可以回滚)

8.约束问题注意两点:
8.1只有not null 只能使用列级约束。其他的约束两种方式皆可,
对于not null来讲,不用add,需要使用modify;
8.2添加和删除表的约束–在创建表以后,只能添加和删除,不能修改

  1. 对 rownum(伪列) 只能使用 < 或 <=, 而用 =, >, >= 都将不能返回任何数据,

    例一:查询工资前20名的员工姓名,工资 ,工资由高到低
    Select rownum,first_name,salary from
    (Select first_name, salary from employees
    Group by salary desc)
    Where rownum=<20
    如果直接按如下查询得图(三),显然不正确的,rownum(伪列)
    是按查询顺序来排列的,应当先查询筛选排好列,再查询例如例一
    查询法;
    Select rownum,first_name,salary from employees
    Where rownum<20 order by salary desc

猜你喜欢

转载自blog.csdn.net/weixin_44544678/article/details/89080414