zoukankan      html  css  js  c++  java
  • oracle学习笔记第一天

    oracle学习笔记第一天
    --oracle学习的第一天
    --一、几个基础的关键字
     
    1、select
    select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条语句的最后执行
    select * from emp;
    相关:1)列可起别名,有三种方式:<1>直接列名后下个单词;
    select job j from emp;
    <2>列名后双引号添加别名
    select job "j" from emp;
    <3>列名后加as,再加有有双引号的别名
    select job as "j" from emp;
    select job as "j" from emp;
    2)distinct关键字(有区别的)  去重,可有多列(多列为整体,都一样时才去重)
    select distinct job from emp;
    select distinct job,comm from emp;
     
    2、from关键字     检索表,紧跟后面可直接写表名
    select * from emp e; 
     
    3、where关键字     过滤行记录  
    用法:
      1).=,!=,<>,<,>,<=,>=,any,some,all
    select * from emp where sal>2000;
    select * from emp where sal = any(5000,1100);
      2). is null,is not null
    select * from emp where comm is not null;
      3).between x and y
      select * from emp where sal between 2000 and 4000;
    4).and 、 or 、 not
    select * from emp where deptno = 10 and sal>2000;
    select * from emp where deptno = 10 or sal>2000;
    select * from emp where comm is not null;
      5).in(list),not in(list)注:定值并非定范围
    select * from emp where sal in(1100,5000);
      6).exists(子查询)、not exists(子查询)
      select * from emp where exists (select deptno from emp where  deptno = 20 and sal >2000 );
      7).like模糊查询(用单引号)
        “%”:匹配零个或若干个字符
        “_”:匹配一个字符
        在模糊查询中,如果查询的数据中有“%”,“_”时,可以使用escape自定义转
       select * from emp where ename like '%A%' ;  
       select * from emp where ename like '_A%';    
       
    4、order by关键字  排序
    注:asc 升序(可省略),desc 降序
        可加多列,有优先级,第一列先排序,后第一列值相同,按第二列排序,类推
       
    5、集合操作:
    1).union 关键字   并集(去重)
      select ename from emp where sal > 2000 union select ename from emp where deptno=10
    2).union all 关键字   全集(不去重)
       select sal from emp where sal > 2000 union all select sal from emp where sal < 5000 
    3).intersect 关键字  交集
       select sal from emp where sal > 2000 intersect select sal from emp where sal < 5000 
    4).minus 关键字   差集
       select sal from emp where sal > 2000 minus select sal from emp where sal < 5000 
    注:1)能用and,or,解决的不要用集合操作,效率低
        2)两集合选择的列建议保持一致,不一致有什么意义呢
       
    6、group by关键字      分组
            注: 1.分组之后,不能将除分组字段之外的字段放在select后面
                2.group by 后面可以跟多个字段,则这多个字段值都相同时,才分为一组
                3.分组之后,可以使用组函数对每个组进行数据处理
    select deptno from emp group by deptno,comm;
     
    7、having关键字  分组后过滤行记录
          注:与where基本一样      
    select deptno from emp group by deptno having deptno =10;   
     
    二、函数
    单行函数
    dual 虚表,一般用于测试
     
    1.字符函数
      1)concat(s1,s2) 拼接a,b两个字符串数据
      select concat('aa','ccc') from dual;
      2)initcap(s) 将每个单词x首字母大写
      select initcap ('li jia sheng') from emp ;
      3)lower(s) / upper(s)  将字符串小写/将字符串大写
       select lower ('LI JIA SHENG') from emp ;
       select upper ('li jia sheng') from emp ;
      4)length(s) 获取字符串的长度
       select length('aaaccc') from dual;
      5)lpad(s,len,c) /rpad() 将s字符串左边填充至len长度,用c字符填充
      select lpad('abcde',10,'-') from dual;
      select rpad('abcde',10,'-') from dual;
      6)ltrim(s,c)  / rtrim()  去除s字符串左边的c字符,如果c不传参,默认去除空格
      select ltrim('abcde','a') from  dual;
      7)replace(s,s1,s2)   将a中的b字符串替换为c
       select replace('a bcde','a','c') from dual;
      8)substr(s,index,len) 将s的字符串,从index位置开始截取,截len个长度
       select substr('abcde',2,2) from dual;
      9)trim( c from s) 将b左右两边的a字符去除掉
       select trim('a' from 'aaddddaa') from dual;
     
    2.数字函数
      abs() 求取绝对值
      select abs(-3) from dual;
      ceil() 向上取整
      select ceil(5.2) from dual;
      floor() 向下取整
        select floor(5.2) from dual;
      round() 四舍五入
        select round(5.2) from dual;
      power(x,y)  x的y次幂
        select power(5,2) from dual;
     
    3.日期函数
      sysdate 返回系统当前日期,注意没有括号
      select sysdate from dual;
      add_months(d1,d2) 在d1日期上,增加d2个月份
      select add_months(sysdate,4) from dual;
      months_between(d1,d2) 返回d1和d2之间的相隔月份
      select months_between(sysdate,hiredate) from emp;
      last_day(d) 返回d日期所在月份最后一天的日期
      select last_day(sysdate) from dual;
      next_day(d,X) 返回下一个星期X的日期
      select next_day(sysdate,'星期日') from dual;
     
    4.转换函数
      to_char()  将数字、或日期转化为字符串
      select to_char(sysdate,'yyyy-mm-dd hh-mi-ss') from dual ;
      select to_char(1234,'9,9,9,9') from dual;
      to_date() 将字符串转化为日期
      select to_date('1996/06/25','yyyy-mm-dd ')  from dual;
      to_number()  将字符串转化为数字
      select to_number('23333') from dual;
      select to_number(64,'XX') from dual;
    5.其他函数
      nvl(x,y) 如果x为null,则显示为y,x和y的类型保持一致
      select nvl(comm,0) from emp;
      sys_guid() 生成一个的32位随机字符串
      select sys_guid() from emp;
      decode()  条件取值,类同java的switch
      select decode(sal,1000,'****',5000,'######','!!!!!') from emp;
      case when then else end  条件取值,类同java的if-else if-else
      select  case when sal<1000 then '@@@@@@@'
                      when sal<5000 then '^^^^^^^'
                        else '!!!!!'  end from emp;
     
    组函数
     
    组函数又被称作聚合函数,用于对多行数据进行操作,并返回一个单一的结果
     
    avg()求平均值,只能对数字类型进行处理,不处理空字段
    select avg(sal) from emp;
    sum()求和,只能对数字类型进行处理
    select sum(sal) from emp;
    max() 求最大值,对任何类型生效
    select max(sal) from emp;
    min() 求最小值,对任何类型生效
    select min(sal) from emp;
     
    三、sql顺序
    sql顺序分为两类:
     
    1.sql的书写顺序
    select   from    where    group by   having   order by [asc/desc]
     
    2.sql的执行顺序
    from   where   group by   having    select   order by [asc/desc]
     
  • 相关阅读:
    ORA-1034 ORACLE not available (转)
    关于命名管道
    Linux12.3 exportfs命令
    Linux12.2 NFS配置
    Linux12.1 NFS介绍
    Linux11.4 常用SQL语句、数据库备份恢复
    Linux11.3 MySQL用户管理
    ssh远程连接错误
    Internal Server Error: /api/course/coursesection/
    vue.esm.js?efeb:591 [Vue warn]: Invalid prop: type check failed for prop "data". Expected Array, got String.
  • 原文地址:https://www.cnblogs.com/heviny/p/10722580.html
Copyright © 2011-2022 走看看