DML(Data Manipulation Language)语句 on Oracle

DML语句(select + inser update delete)

--insert

insert into tablename values(col1value,col2value···);
--可以插入特定字段
insert into dept2 (deptno,dname) values(60,'game2')
--可以插入整张表
insert into dept1 (select * from dept2)

--update

update tablename set col1name=value1,col2name=value2··· where···

--delete

delete from tablename
delete from tablename where···

--select

select * from tablename;
select col1name,col2name from tablename;
select col1name,col2name*12 from tablename;
select col1name,col2name*12 "another name" from tablename;--anothername里有特殊字符,用双引号括起来,双引号可以保持别名的格式
desc dual; --空表,用来算数学表达式
select sysdate from dual;
select colmame1||colname2 --|| 字符串连接符
--''单引号括起来的是字符串,若字符串内有单引号则用两个单引号代替一个单引号
--去除重复项,distinct修饰多个列时保证查询结果对这几个列的组合是不重复的
select distinct colname from tablename;
--value 为字符串时加''
--比较运算符:<,>,不等于<>,between···and···,<=,>=;
--逻辑运算符:and,or,not
--其他判断    :is null,is not null,in(value1,value2,value3)等于其中之一,not in()
--比较日期时日期字符串要按标准格式写
select * from tablename where colname = value;
--模糊查询 like 通配符 %零个或多个 _代表一个字母 \转义字符
···like '%$%%' escape '$' --自己指定转义字符为$
···order by colname1 desc,colname2 asc;--desc降序的 默认升序asc;先按colmname1降序,相同的再用colmname2升序排

常用sql函数:

单行函数
lower(colname) 
upper(cloname)
substr(colname,NUM1,NUM2) --从第NUM1位开始取 NUM2位
chr(ASCII)
ascii('CHARACTER') 
ROUND(DECIMALS)-- ROUND(小数,四舍五入到第几位)
to_char(colname,'$99,999.9999') --9代表一位数字,但是这位上没有数字时不显示;0也代表一位数字,该位没数字时用0补; L代表本地货币
to_char(colname,'YYYY-MM-DD HH:MI:SS')
to_char(colname,'YYYY-MM-DD HH24:MI:SS')
to_date('1988-10-24 12:34:56','YYYY-MM-DD HH24:MI:SS')
to_number('$1,250.00',"$9,999.99")
nvl(colname,0) --若conname为null,则变为0,mysql中是ifnull(cloname,0)
组函数:
--多行函数!!!!!!!!!!!!!!!!!!!!!!!!
max(colname) 
min(colname)
avg(colname)
sum(colname) 
count(*)--一共多少条记录
count(colname)--只计算不是空值的
count(distinct colname)--只计算不重复的
group by colname
group by colname1,colname2-- 按这俩的组合分组!!!!!!!
出现在select 列表中的字段如果不是出现在组函数里,那么必须存在于group by子句里!!!!!!!
having  where对行操作,having对组操作,分组后再having
select···from···where···group by··having··order by···
--where后为筛选元组的条件,group by为分组条件,having后为筛选分组的条件,order by为排序条件

子查询

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

--自连接(把一张表当两张表用)

select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr = e2.empno;--92版
select e1.ename,e2.ename from emp e1 join emp e2 on (e1.mgr = e2.empno);--99版

--交叉连接

select ename,dname from emp,dept;--92版
select ename,dname from emp cross join dept;--99版

--等值连接

select ename,dname from emp,dept where emp.deptno = dept.deptno;--92版
select ename,dname from emp join dept on (emp.deptno = dept.deptno);--99版
select ename,dname from emp join dept using (deptno);--不推荐用

--非等值连接

select ename,sal,grade--99版
  from emp e join salgrade s
             on (e.sal between s.losal and s.hisal);
select ename,dname,grade
  from emp e join dept d 
               on (e.deptno=d.deptno)
             join salgrade s
               on (e.sal between s.losal and s.hisal)
  where ename not like '_A%';

--外连接
--左外连接:

select e1.ename,e2.ename from emp e1 left join emp e2 on (e1.mgr=e2.empno);

--右外连接:

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

--全外连接

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

--基于Oracle示例表的子查询练习
--求部门中那些人薪水最高

select ename,sal,emp.deptno
  from emp join (select max(sal) max_sal,deptno from emp group by deptno) t
             on (t.max_sal = emp.sal and emp.deptno = t.deptno);

--部门平均薪水的等级

select deptno,avg_sal,grade
  from salgrade join (select avg(sal) avg_sal,deptno from emp group by deptno) t
                  on (t.avg_sal>salgrade.losal and t.avg_sal<salgrade.hisal);

--部门平均的薪水等级

select deptno,avg(grade)
  from emp e join salgrade s
               on (e.sal between s.losal and s.hisal)
  group by deptno;

select deptno,avg(grade)from (
  select deptno,ename,sal,grade
    from emp e join salgrade s
                 on (e.sal between s.losal and s.hisal)
)group by deptno;

--雇员中有哪些人是经理人

select distinct e2.ename
  from emp e1 join emp e2
    on (e1.mgr=e2.empno);

--求普通员工时要注意了,not in不行,子查询里有空值,空值代表任意值

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

--不准用组函数,求薪水的最高值

select distinct e1sal from(
  select distinct e1.sal e1sal,e2.sal e2sal
    from emp e1 left join emp e2
                       on (e1.sal<e2.sal)
)where e2sal is null;

select distinct e1.sal from 
  emp e1 left join emp e2 on (e1.sal<e2.sal) 
where e2.sal is null;

select distinct sal from emp
where sal not in (
  select distinct e1.sal e1sal
    from emp e1 join emp e2
                  on (e1.sal<e2.sal)
);

--求平均薪水最高的部门的部门编号

select deptno,avgsal from 
  (select deptno,avg(sal) avgsal from emp group by deptno)
where avgsal=
  (select max(avgsal) from 
    (select deptno,avg(sal) avgsal from emp group by deptno)
  );

--组函数嵌套,最多两层

select deptno,avgsal from 
  (select deptno,avg(sal) avgsal from emp group by deptno)
where avgsal=
  (select max(avg(sal)) from emp group by deptno);

--求平均薪水最高的部门的部门名称

select dname from dept where deptno =
(
  select deptno from 
    (select deptno,avg(sal) avgsal from emp group by deptno)
  where avgsal=
    (select max(avgsal) from 
      (select deptno,avg(sal) avgsal from emp group by deptno)
    )
)

--求平均薪水的等级最低的部门的部门名称

select dname from dept where deptno =(
  select deptno from 
  ( select deptno,grade from
     (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
    on (t.avg_sal between s.losal and hisal)
  )where grade =(
    select min(grade) from(
      select deptno,grade from
       (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
      on (t.avg_sal between s.losal and hisal)
    )
  ) 
)

select d.deptno,dname,avg_sal,grade from 
  (
    select deptno,avg_sal,grade from
     (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
    on (t.avg_sal between s.losal and hisal)
  ) t 
  join dept d on (t.deptno=d.deptno)
  where grade =(
    select min(grade) from(
      select deptno,grade from
       (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
      on (t.avg_sal between s.losal and hisal)
    )
  )
select dname from(
  select t.deptno,dname,grade from 
    (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
      on (t.avg_sal between s.losal and s.hisal)
    join dept d on (t.deptno=d.deptno)
)where grade=(
  select min(grade) from(
    select t.deptno,dname,grade from 
  (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
    on (t.avg_sal between s.losal and s.hisal)
    join dept d on (t.deptno=d.deptno)
  )
)

--创建视图

grant create table,create view to scott;
create view V$_dept_sal_info as(
select t.deptno,dname,grade from( 
  (select deptno,avg(sal) avg_sal from emp group by deptno) t join salgrade s
    on (t.avg_sal between s.losal and s.hisal)
    join dept d on (t.deptno=d.deptno)
  )
)
select dname from V$_dept_sal_info 
where grade=(select min(grade) from V$_dept_sal_info2)

--求部门经理人中平均薪水最低的部门名称(思考题)

select dname from dept where deptno=(
  select deptno from (
    select deptno,avg(sal) avg_sal from 
    (
      select empno,ename,deptno,sal from emp 
      where empno in (select e2.empno from emp e1 join emp e2 on e1.mgr=e2.empno)
    )group by deptno
  )where avg_sal=(
    select min(avg(sal)) from 
    (
      select empno,ename,deptno,sal from emp 
      where empno in (select e2.empno from emp e1 join emp e2 on e1.mgr=e2.empno)
    )group by deptno
  )
)

--求比普通员工的最高薪水还要高的经理人名称

select ename from emp where
  empno in(select distinct mgr from emp where mgr is not null)
  and sal > (
    select max(sal) from emp where empno not in
     (select distinct mgr from emp where mgr is not null)
  )

--求薪水最高的前五名雇员
--结果集中存在伪字段rownum,行号,只能和< <=一块用

select ename from
  (select ename,sal from emp order by sal desc)
where rownum < 6

--求薪水最高的第六到第十名雇员

select ename,sal,r from
  (
  select ename,sal,rownum r from
    (select ename,sal from emp order by sal desc)
  )
where r>=6 and r<=10

--练习:求最后入职的五名员工

select ename,to_char(hiredate,'YYYY_MM_DD HH24:MI:SS') from
 (select ename,hiredate from emp order by hiredate desc)
where rownum<=5

--<的话直接用就行了
--面试题:比较效率

  select * from emp where deptno = 10 and ename like '%A%';
  select * from emp where ename like '%A%' and deptno = 10;

--有3个表S,C,SC S(SNO,SNAME) C(CNO,CNAME,CTEACHER) SC(SNO,CNO,SCGRADE)
--找出没选过“黎明”老师的所有学生姓名

select disctinct s.sno,s.sname from s
  join sc on (s.sno=sc.sno)
  join c on(c.cno=sc.cno)
  where cteacher<>'黎明'

--列出两门以上(含两门)不及格学生姓名及平均成绩

select sname,avg_g from s join(
  select sno,avg(scgrade) avg_g from sc where scgrade<60 group by sno having count(cno)>=2
) t on (s.sno=t.sno)

--既学过1号课程又学过2号课程的所有学生的姓名

select sname from s where sno in
(
  select sno from sc where cno=2 and sno in (select distinct sno from sc where cno=1)
)

你可能感兴趣的:(JOIN,oracle,null,delete,insert,character)