1.Oracle

Oracle  -----------------------------------------------------

1.常用的数据库:
    大型项目:Oracle、DB2
    中小型项目:MySQL、SqlServer、Access

2.常用的数据库终端工具:PLsql developer(Oracle)、Navicat、Datagrip

3.以管理员身份登录:sqlplus / as sysdba

    alter user scott identified by admin123 account unlock;
    把用户scott密码修改为admin123并解锁用户

4.以普通用户登录: sqlplus scott/admin123

5.连接Oracle的终端工具:Plsql developer

    以scott/admin123登录

6. DBA (DataBase Administrator)数据库管理员
    Oracle 11g 安装文件夹...\11.2.0\dbhome_1\NETWORK\ADMIN下面有
    一个文件listener.ora,打开后看到如下代码
        LISTENER =
      (DESCRIPTION_LIST =
        (DESCRIPTION =
          (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
          (ADDRESS = (PROTOCOL = TCP)(HOST = 主机ip)(PORT = 默认端口))
        )
      )

  另有文件tnsnames.ora同样需要配置主机IP、端口信息,之后将该文件复制到
  客户端client 11g安装文件夹的NETWORK\ADMIN下面

7.默认端口: Oracle 1521,  MySQL 3306

8.建表语法:
    create table 表名 (
    字段1 数据类型 约束条件,
    字段2 数据类型 约束条件,
    .....
    );

9.数据类型:
    9-1.字符串(单引号)
            定长char(3)          '123' , 'tom' , 'ha'
                变长 varchar2(5)      '123',  '12345',  'hello'
    9-2.数字   整型  number(5)       123,    12345
            浮点型  number(5,2)   3.14,   123.14
    9-3.日期   date     2021-09-06,   2021-09-06 11:36:00 

10.约束条件:
    1.唯一unique:不允许重复,可以为空。  如手机号码
    2.非空not null:不允许为空,可以重复。 如姓名
    3.主键primary key:唯一,非空。 如身份证号
    4.外键foreign key:一般作为另一张表的主键,允许重复,通常不为空,references 表名(列名)
    5.默认值default:没有值的时候,取默认值,default 值
    6.检查check:检查给的值是否满足指定条件,check (sex = '男' or sex = '女')

    注:多个约束条件时,默认值在前面
        一张表最多只能有一个主键
        如果插入的记录外键为非空,则这个外键必须是在关联的表中存在的,
        如果插入的记录外键为空,则直接插入。


11.注释: --单行注释    /*多行注释*/
    表名、列名命名规则:字母、数字、下划线组成,不能以数字开头


12.表字段相关(了解):
    12-1.新增表字段语法:
        alter table 表名 add(
        字段1 数据类型 约束条件,
        字段2 数据类型 约束条件,
        .....);

    12-2.修改表字段语法:
        alter table 表名 modify (
        字段1 数据类型 约束条件,
        字段2 数据类型 约束条件,
        .....);

     12-3.删除表字段语法:
         alter table 表名 drop column 字段名;

13.删除整张表(包括表结构):
     drop table 表名;

14.查看表结构  命令窗口下 SQL>  desc 表名;


15.表数据相关:
    15-1.插入数据: 
        insert into 表名(字段1,字段2...) values(值1,值2...);
        insert into 表名 values(值1,值2...);

        插入多条数据(了解)
         Oracle(可一次性插入多个表):
          insert all 
          into 表名(字段1,字段2...) values(值1,值2...)
          into 表名(字段1,字段2...) values(值1,值2...)
          select * from dual;

         MySQL:
          insert into 表名 values(值1,值2...),(值1,值2...),(值1,值2...);

    15-2.修改数据:
        update 表名 set 要修改的字段=要修改的值 where条件;

    15-3.删除数据:
        delete from 表名 where条件;    -- from可以省略
        delete from 表名;    --删除整张表

    15-4.删除整张表的数据:
        truncate table 表名;

16.提交事务:commit;
    回滚事务:rollback;

    注:提交、回滚只适用于insert、update、delete操作

17.几种删除的区别:
    delete:删除表中数据,可跟where条件,可以回滚,删除效率较低,适用少量删除
    truncate:删除表中全部数据(保留表结构),不可跟where,不可回滚,适用大量删除
    drop:删除整张表(不保留表结构),不可跟where条件,不可以回滚

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

以下为查询部分内容(非常重要!!!)

dept --> 部门表:
    deptno  部门号
    dname   部门名称
    loc     部门地址

emp --> 员工信息表:
    empno    员工的工号
    ename    员工的姓名
    job      工种
    mgr      员工上司的工号
    hiredate 入职日期
    sal      工资
    comm     奖金
    deptno   部门号


1.全表查询(无条件查询) select...from...

    select * from 表名;   --查询表中全部内容
    select 字段1,字段2...from 表名;    -- 查询表中指定字段内容

2.条件查询  where 

    select 列1,列2... from 表名 where条件;

       常用的比较运算符: =    >   <   >=    <=    != 或 <>  

    例:查询20号部门中全体员工的姓名、工资、和工种的有关信息。
        select ename, sal, job from emp
               where deptno = 20;

    练习:查找出奖金超过其工资的雇员的姓名、工资、奖金和工种的信息。
        select ename, sal, comm, job from emp
               where comm > sal;

3.去重 distinct

    例:显示出20号部门中不同的工种。
        select distinct job from emp where deptno = 20;

4.多条件查询 优先级 and > or

    例:查找出全部经理 和 第10号部门秘书的有关信息
        select * from emp
               where job = 'MANAGER' 
               or  deptno = 10 and job = 'CLERK'; 
        select * from emp
               where job = 'MANAGER' 
               or  (deptno = 10 and job = 'CLERK'); 

    练习:查找出不是30号部门中的所有经理的所有信息。
        select * from emp
               where deptno <> 30
               and job = 'MANAGER';
        select * from emp
               where deptno != 30
               and job = 'MANAGER';

5. 多值比较运算符:between...and...    in (...)    否定前面加not

    例:查找出工资在2000到3000之间的职工姓名、工种和工资  
        select ename, job, sal from emp
               where sal >= 2000 and sal <= 3000;
        select ename, job, sal from emp
               where  sal between 2000 and 3000;

    练习:查找出工资在2500到3500之外的职工姓名、工种和工资
        select ename, job, sal from emp
               where sal <= 2500 or sal >= 3500;
        select ename, job, sal from emp
               where sal not between 2500 and 3500;

    例:查询出全部秘书、分析员或推销员的姓名、工种、工资和所在部门号
        select ename, job, sal, deptno from emp
               where job = 'CLERK' 
               or job = 'ANALYST' 
               or job = 'SALESMAN';
        select ename, job, sal, deptno from emp
               where job in ('CLERK','ANALYST' ,'SALESMAN');

    练习:查询出工资分别是1500,2500,3000的分析员或推销员的姓名、工种、工资和所在部门号  
        select ename,job, sal, deptno from emp
               where sal in (1500,2500,3000)
               and job in ('ANALYST','SALESMAN');
        select ename,job, sal, deptno from emp
               where (sal = 1500 or sal = 2500 or sal = 3000)
               and (job = 'ANALYST' or job = 'SALESMAN');


6.模糊查询: like
    %表示匹配任意长度的字符串
    %test%:表示匹配中间字符串为test的任意长度的字符串

    _表示仅能匹配一个字符
    o_a:表示匹配长度为3,以字母o开头,且以字母a结尾的字符串,中间一个字符为任意字符。

    例:查询出名字以"MA"开头的全部职工的姓名、工种、工资和部门号
        select ename, job, sal, deptno from emp
               where ename like 'MA%';

    例:查询出名字以"SCO"开头的,长度为5位的全部职工的姓名、工种、工资和部门号
        select ename, job, sal, deptno from emp
               where ename like 'SCO__';

    练习:
    1、查找出所有工种以'MANAG'开头的职工姓名、工种和工资
        select ename ,job ,sal from emp 
               where job like 'MANAG%';

    2、查找出所有姓名以'ALLE'开头,且长度为5的职工姓名、工种和工资
        select ename, job, sal from emp
               where ename like 'ALLE_';

    upper() 把字母转换成大写字母
    lower() 把字母转换成小写字母

7.空值 is null、非空值 is not null , 不能用"=NULL"或"!=NULL"表示。

    例:查找出emp表中经理号(MGR)为空的职工的有关信息
        select * from emp where mgr is null;

    练习:查找出没有奖金的员工的姓名、工种、工资和部门号
        select ename, job ,sal , deptno 
               from emp 
               where comm is null;


8.排序 ORDER BY ... ASC/DESC;  根据..正序/倒序排列,正序时asc可以省略

    例:计算每个销售人员的年度总报酬,并按总报酬由高到低顺序显示
        select (sal + nvl(comm,0))*12  sumsal from emp 
               where job = 'SALESMAN' 
               order by sumsal desc;

-- nvl(a,b)  如果a为空,处理成b

    练习:
        1、查找出工资高于1000元的职工的姓名、工种、工资和部门号,并按部门号由小到大排序显示
        select job , sal , ename, deptno from emp
               where sal >1000
               order by deptno ;

        2、查找出工资高于1000元的职工的姓名、工种、工资和部门号,并按部门号由小到大排序,
            同一部门的按工资倒序
        select job , sal , ename, deptno from emp
               where sal >1000
               order by deptno , sal desc ;
       
        3、查找出奖金超过本人基本工资3%的职工的姓名,工资,奖金,奖金与工资的比例,
            并按其比例由高到低显示
        select  ename ,sal, comm, comm/sal from emp
                where comm > sal * 0.03
                order by comm/sal desc;

    4、按工种升序,而同工种按工资降序排列显示全部职工的姓名,工种,工资。
        select ename, job ,sal from emp 
               order by job, sal desc;

9.日期处理函数 to_date(),如to_date('2021-09-07 14:48:50','yyyy-mm-dd hh24:mi:ss')

    create table student (
    id char(3) primary key,
    name varchar2(30) not null,
    sex char(3) check(sex = '男' or sex = '女'),
    birthday date default sysdate);


    insert into student(id,name,sex) values('001','王林','男');
    insert into student 
        values('002','Tom','男',to_date('2021/9/7 14:48:50','yyyy/mm/dd hh24:mi:ss'));
    insert into student 
        values('003','lan','男',to_date('2021-09-07 14:48:50','yyyy-mm-dd hh24:mi:ss'));


    练习:在emp表中,查找1981-05-01之前入职的员工信息
        select * from emp WHERE hiredate < to_date('1981-05-01','yyyy-mm-dd');

10.单表复杂查询

10-1.分组函数(聚合函数)

      count() 统计个数
      sum()   求和
      max()   最大值
      min()   最小值
      avg()   平均值

    例:统计emp表有多少人?
        select count(*) from emp ;  -- count(*) 统计所有行(包括空行)

    练习1:统计有奖金的有多少人?
        select count(*) from emp  where comm is not null;
           
        select count(comm) from emp;   -- count(列名)  统计有内容的行(不包括空行)

    练习2:统计有上级领导的人数
        select count(mgr) from emp;

    例1:计算emp表中公司职工的总人数及工种数
        select count(ename), count(distinct job) from emp;

    例2:每个月发出去奖金为多少?
        select sum(comm) from emp;

    例3:工资最多、最少、平均分别是多少?
        select max(sal), min(sal), round(avg(sal),2) from emp;

    round(m,n) 对m保留n位小数

    练习:计算全部销售员的年平均报酬。
        select avg(sal + nvl(comm,0))*12 from emp where job = 'SALESMAN';
        select avg((sal + nvl(comm,0))*12)  from emp where job = 'SALESMAN';


10-2.分组查询:

        select 列a, 分组函数 from 表
               where条件
               group by 分组列a, 分组列b
           
    --完整语法(分组条件查询):
        select 列a, 列b, 分组函数 from 表
               where 条件
               group by 分组列a, 分组列b, 分组列c
               having 分组条件(分组函数需要满足的条件)
               order by 排序


    例1:计算出公司支付给每个工种的总工资
        select job, sum(sal) from emp
               group by job;
           
    例2:查找出公司支付给每个工种的总工资大于5000的工种及总工资
        select job, sum(sal) from emp
               group by job
               having sum(sal) > 5000;

    例3:计算每个部门中每种工种各有多少职工数。
        select deptno, job, count(*) from emp
               group by deptno, job;


    练习:统计各部门的人数。
        select deptno , count(empno) from emp
               group by deptno;
           
    总结:
        1、分组函数只能出现在select、having、order by 子句中
        2、如果在select语句同时含有group by、having、order by,
            他们的顺序是group by、having、order by
        3、select后面的所有字段,必须出现在group by后面,否则有语法错误(重要)
        4、有having 一定有group by


    例:查询各工种组的年平均工资,要求每个工种组至少在2人以上
        select job, avg(sal)*12 from emp
               group by job 
               having count(job) >=2;
       
    练习1:查询出至少有两名秘书的所有部门的部门号,并按人数降序排序。
        select deptno , count(job) from emp
               where job = 'CLERK'
               group by deptno 
               having count(job) >= 2 
               order by count(job) desc;
               
        select deptno , count(job) from emp
               group by deptno, job   
               having job = 'CLERK'
               and count(job) >= 2 
               order by count(job) desc;       
           
    练习2:查询出所有经理和销售人员的年平均工资,并按年平均工资降序排序。
        select job , avg(sal)*12 from emp
               where job in ('MANAGER','SALESMAN') 
               group by job
               order by avg(sal)*12 desc;

        select job , avg(sal)*12 from emp
               group by job 
               having job in ('MANAGER','SALESMAN') 
               order by avg(sal)*12 desc;

    总结:什么时候需要用到 group by 进行分组:
     1)、需要对多组数据进行统计;
     2)、当select字句后面,同时出现字段和分组函数,就必须要用到group by进行分组,
         且select语句后面的所有字段都要包含在group by后面。

11.多表查询

11-1.等值连接  

    select a.列1, b.列2  from 表1 a, 表2 b  
           where a.id = b.id;

    例:查找名字为"ALLEN"的职工所在的部门号、部门名和部门所在地
        select e.deptno, d.dname, d.loc 
               from emp e, dept d
               where e.deptno = d.deptno
               and e.ename = 'ALLEN';
       
        select e.deptno, d.dname, d.loc 
               from emp e join dept d
               on e.deptno = d.deptno
               and e.ename = 'ALLEN';
    练习:
    1.查询部门号是20,30,40的职工的员工编号,姓名,工资,部门所在位置。
        select e.empno, e.ename, e.sal, d.loc from emp e, dept d
               where e.deptno = d.deptno
               and d.deptno in (20,30,40);
       
        select e.empno, e.ename, e.sal, d.loc from emp e join dept d
               on e.deptno = d.deptno
               and d.deptno in (20,30,40);       

    2.显示部门号为10的部门名、员工号和工资
        select d.dname, e.empno, e.sal from emp e, dept d
               where e.deptno = d.deptno
               and d.deptno = 10;
           
        select d.dname, e.empno, e.sal from emp e join dept d
               on e.deptno = d.deptno
               and d.deptno = 10;       

    3.显示雇员名,雇员工资及所在的部门的名字,并按部门名排序
        select d.dname, e.ename , e.sal from emp e , dept d
               where e.deptno = d.deptno 
               order by d.dname;
               
        select d.dname, e.ename , e.sal from emp e join dept d
               on e.deptno = d.deptno 
               order by d.dname;    
                
    4.显示平均工资高于2000的部门号和它的平均工资
        select deptno , round(avg(sal),2) avgsal from emp
               group by deptno 
               having avg(sal) >= 2000;


11-2.左右连接(外连接)

    select a.列1, b.列2  from 表1 a, 表2 b  
           where a.id = b.id(+);   -- 左连接

    select a.列1, b.列2  from 表1 a, 表2 b  
           where b.id(+) = a.id;   -- 右连接
           
    select a.列1, b.列2 from 表1 a left join 表2 b 
           on a.id = b.id;        -- 左连接

    select a.列1, b.列2 from 表2 b right join 表1 a
           on a.id = b.id;        -- 右连接
 

    左右连接的理解:
        主从表分别为t1、t2,假如id既是t1主键,又是t2外键(即id作为两表共有字段),
        当从表中没有数据与主表匹配时,Oracle为从表产生空值,
        所以操作符(+)放到从表的外键后面,表示从表加上空值以后的数据才和主表一样多。

    主键、外键相关:
        1.主键所在的表是主表,外键所在的表是从表
        2.当从表没有数据和主表连接的时候,就会产生空行与主表连接。

        create table student7 (
        id char(2) primary key,
        name varchar2(30) not null );
         
        create table score (
        id char(2) references student7(id),
        subject varchar2(30) not null,
        score number(3) default null); 

        insert into student7 values('01','张三');
        insert into student7 values('02','李四');
        insert into student7 values('03','王五');

        insert into score values('01','语文',90);
        insert into score values('01','数学',90);
        insert into score(subject,score) values('数学2',90);  -- 废数据
        insert into score values('02','英语',90);

        select s.id, s.name, c.subject, c.score from student7 s, score c
               where s.id = c.id(+);  -- 查询所有学生成绩(包含缺考的)
               

    例:显示出所有部门的编号、名称和其职工的姓名与工种。
        select d.deptno, d.dname, e.ename, e.job from emp e, dept d
               where e.deptno(+) = d.deptno;
               
        select d.deptno, d.dname, e.ename, e.job from emp e right join dept d
               on e.deptno = d.deptno;

    练习:列出无雇员的部门的情况。
        select d.* from emp e , dept d
               where  e.deptno(+) = d.deptno
               and e.empno is null;
     

11-3.自连接:把一张表构造成两张表处理

    例:查询员工姓名、工号及上级领导(mgr)姓名、工号
        select e.ename, e.empno, m.ename, m.empno from emp e, emp m
               where e.mgr = m.empno(+);
               
        select e.ename, e.empno, m.ename, m.empno from emp e left join emp m
               on e.mgr = m.empno;       


    多表查询思路:
        1.确定查询的字段来自于哪些表;
        2.找到相同字段,用等号连接;
        3.若需对单条数据过滤,将过滤条件加到where后面,多个条件用and连接;
        4.如果对多组数据统计,用group by子句;
        5.如果对分组后的数据过滤,使用having子句;
        6.如果排序,使用order by 子句。


11-4.子查询

11-4-1.单行子查询 >  <  =  !=  >=  <=

            select 列1, 列2...  from 表名
                   where 指定字段 比较运算符 (SELECT语句);
       
        例:查找出与“SMITH”在同一个部门工作的所有职工姓名及工资
            select ename, sal from emp 
                   where deptno = (SMITH的部门); -- ①搭出框架
                   
            select deptno from emp where ename = 'SMITH'; --②找出SMITH的部门
                   
            select ename, sal from emp          --③合体
              where deptno = (select deptno from emp where ename = 'SMITH');
       

        练习:
        (1)、查找出工资比"SCOTT"工资高的职工的名字,工种,工资和所在的部门号,
                并按工资升序排序。
            select ename, job ,sal, deptno from emp
                   where sal > (select sal from emp where ename = 'SCOTT')
                   order by sal;

        (2)、查找出工资比"SCOTT"高,并且在"NEW YORK"工作的职工的有关情况。
            select emp.* from emp , dept 
                   where emp.deptno = dept.deptno
                   and sal > (select sal from emp where ename = 'SCOTT') 
                   and loc = 'NEW YORK';
               
            select * from emp
                   where sal > (select sal from emp where ename = 'SCOTT')     
                   and deptno = (select deptno from dept where loc = 'NEW YORK');  

            select e.*  from emp e 
                   right join dept d 
                   on e.deptno = d.deptno 
                   where sal > (select sal from emp where ename = 'SCOTT')
                   and  d.loc = 'NEW YORK';

        (3)、查找出具有最高月工资的雇员的姓名、工种和工资。
            select ename, job, sal from emp
                   where sal = (select max(sal) from emp);

        (4)、查找出在"CHICAGO"工作的职工的姓名、工种和工资。
            select e.ename, e.job, e.sal from emp e, dept d
                   where e.deptno = d.deptno
                   and d.loc = 'CHICAGO';

11-4-2.多行子查询  in 、 not in

        select 列1, 列2...  from 表名
               where 指定字段 多值比较运算符 (SELECT语句);

        例:查找出在部门10号中没有的工种,这些工种在其它部门职工的姓名,工种和工资的信息。
            select deptno, ename, job, sal from emp
                   where job not in (select job from emp where deptno = 10);
             
        例:查找出部门10   与部门30中工种相同的职工的姓名和工种。
            select ename, job from emp
                   where deptno = 10
                   and job in (select job from emp where deptno = 30);

        练习:
        1.查找出工资在1000到3500元之间的职工  所在部门的所有人员的有关信息。
        select * from emp where deptno in (
               select deptno from emp where sal between 1000 and 3500);

        2.查找出工种在部门10中  没有其他部门的   职工的姓名、工种和工资信息。
       2 查找出工种不在部门10中的 其他部门  职工的姓名、工种和工资信息。
        select * from emp where job not in (
               select job from emp where deptno = 10);
           
        3.查找出部门20中的工种相同的职工的姓名与工种。
        select ename, job from emp 
               where job in (
                   select job from emp where deptno=20 group by job having count(*)>=2)
               and deptno = 20;

        查找出与部门20中的工种相同的职工的姓名与工种。
        select * from emp where job in (select job from emp where deptno =20);


11-4-3. from子句中使用子查询(把子查询当做一张表)

    例:显示高于自己部门平均工资的员工的信息
        select e.* 
            from emp e, (select deptno, avg(sal) avgsal from emp group by deptno) a
           where e.deptno = a.deptno and sal > a.avgsal;

    练习1、列出工资大于本部门平均工资的员工姓名和部门号及统计此部门的人数
        select e.ename, e.deptno, n.cot 
          from emp e, (
                  select deptno, avg(sal) avgsal, count(*) cot from emp group by deptno) n
          where e.deptno = n.deptno
          and e.sal > n.avgsal;
          
        select m.ename, m.deptno, n.num 
            from (select e.ename, e.deptno 
                       from emp e, 
                           (select deptno, avg(sal) avgsal from emp group by deptno) m
                    where e.deptno = m.deptno
                    and e.sal >m.avgsal) m,
               (select deptno , count(*) num from emp group by deptno) n
           where m.deptno = n.deptno;

        结果显示如下:
        EMPNAME   DEPID   部门人数
        ZHANGSAN  1       15
        LISI      2       10

    总结:
        1、数据库的插入insert,删除delete,修改update,都不可以使用多表连接;
            如果删除、修改的数据涉及多张表,必须使用子查询。
        2、删除的数据存在于多张表,必须先删从表的数据,再删主表的数据。

12.分页查询-伪列(了解内容)

    例:查2到6行
        select * from (select rownum rm,emp.* from emp) a where rm >= 2 and rm <= 6;

    例:mysql用法:从第3行开始,查6行(MySQL的limit从0行开始算)
        select * from emp limit 2, 6; 

    例:MySQL查询前6行
        select * from emp limit 6; 


13.数据备份 ---------

    --*********** 导出表数据 ***********
    步骤:
        1、选择PL/SQL菜单栏的Tools下面的Export Tables
        2、切换到"SQL Inserts"窗口
        3、选择"Create tables"
        4、在Output file处,选择输出的地址,并自定义好以 .sql 结尾的文件名字,如 emp.sql
        5、点击Output file最右边的Export按钮即可。
     
     
    --*********** 导入表数据 ***********
    步骤:
        1、选择PL/SQL菜单栏的Tools下面的Import Tables
        2、切换到"SQL Inserts"
        3、选择“Use Command Window”
        4、在Import file中选择要导入的sql文件
        5、点击Import file最右边的Import按钮即可。
    


扩展1:
    找出语文,数学,英语三科成绩都大于或等于90分的学生姓名。
    姓名          课程           成绩
    张三           语文          94
    张三           数学          95
    李四           英语          88
    李四           语文          99 
    李四           数学          96
    。。。。。。。
    。。。。。。。
    。。。。。。。

     
    -- 找出90以上,再找3门的
    -- 找出3门的,再找90以上  
    -- 先找3门的,再找出有90分以上的,再出都是90分以上     
    -- 先找出语文90以上,再找出数学90以上,英语90以上的
    -- 先找出3门的,最少分在90以上  
    -- 最少分90以上,再找出3门
    -- 分数少于90,再找出不是这个名字的  (有瑕疵)

    create table student6 (
    name varchar2(30) not null,
    course varchar2(30) not null,
    score number(3) not null);
    insert into student6 values('张三','语文',94);
    insert into student6 values('张三','数学',95);
    insert into student6 values('李四','英语',88);
    insert into student6 values('李四','语文',99);
    insert into student6 values('李四','数学',96);
    insert into student6 values('王五','语文',94);
    insert into student6 values('王五','数学',99);
    insert into student6 values('王五','英语',92);
    insert into student6 values('小明','语文',94);


    select * from student6 where course in ('语文','数学','英语') and score >90; --(×)

1.
select sname from student6
where score > 90
group by sname
having count(*) = 3;

2.
select c.sname from 
    (select sname,score from student6 where course='语文') c,
    (select sname,score from student6 where course='数学') m,
    (select sname,score from student6 where course='英语') e 
  where c.sname=m.sname 
  and m.sname=e.sname 
  and c.score>=90 
  and m.score>=90 
  and e.score>=90;

3.
select distinct sname from student6 
      where sname in (select sname from student6 where course = '语文' and score > 90) 
      and sname in (select sname from student6 where course = '英语' and score > 90)
      and sname in (select sname from student6 where course = '数学' and score > 90);

4.无法保证考了三门
select * from student6 
       where sname not in (select sname from student6 where score <=90);

5. 无法保证考了三门
select sname, min(score) from student6
       group by sname
       having min(score) >90;

6.
select sname from student6 
       where course = '英语' and score >=90 
       and sname in (
           select sname from student6 
           where course = '数学' and score >=90 
           and sname in (
                     select sname from student6 
                                  where course = '语文' and score >= 90
                                        )
                                  );
7.
select name from s 
       group by name 
       having min(score)>=90
       and count(name) >=3;  
       
             
8.先找出考了三门的,再找出有90的,再找出三门都考90的
select s.name from s,(
     select name,count(name) from s
       group by name
       having count(name) = 3    ) n       
       where s.name = n.name 
       and score >= 90
       group by s.name
       having count(s.name) > 2;
 

扩展2: case...when.. 适用于字段值比较多的情况,如订单状态
 
select job, case job 
       when 'CLERK' then '职员'
       when 'SALESMAN' then '销售'
       when 'MANAGER' then '经理'
       when 'ANALYST' then '统计'
       else '总经理'  end  "中文职业"
       from emp;

补:oracle启动的三个过程:startup nomount、mount、open
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值