Oracle第一天笔记.sql

select * from tab;

/*
数据库 —> 数据库实例 —> 表空间(逻辑单位)(用户) —> 数据文件(物理单位)

地球 —> 一个国家 —> 省份(逻辑单位)(公民) —> 山川河流(物理单位)

通常情况下,ORacle数据库只会有一个实例ORCL,

新建一个项目:
MYSQL : 创建一个数据库,创建相应的表
Oracle: 创建一个表空间,创建用户,用户去创建表

Oracle和MYSQL的差别

Oracle是多用户的, MYSQL是多数据库的

  1. 遵循SQL标准
  2. 不同厂商,不同的数据库产品,但是有自己的方言
  3. 使用自己的方言,也能够完成相同的功能
  4. Oracle安全级别要高,MYSQL开源免费

/
/

基本查询:
SQL : 结构化查询语言

请听题: 请说一下SQL的分类以及每类常见的操作符都有哪些
四类:
DDL : 数据定义语言 create alter drop truncate
DML : 数据操纵语言 insert update delete
DCL : 数据控制语言 安全 授权 grant revoke
DQL : 数据查询语言 select from子句 where子句

查询语句的结构:

select [列名] [*] from 表名 [where 条件] [group by 分组条件] [having 过滤] [order by 排序]
*/
select * from emp;

select 1+1; --在Oracle等于报错 ,在MYSQL中输出结果是2

/*
dual : oracle中的虚表 ,伪表, 主要是用来补齐语法结构

*/
select 1+1 from dual;

select * from dual;

select 1 from emp;
–直接写一个常量比写 * 要高效
select count(1) from emp;
select count(*) from emp;

/*
别名查询: 使用as 关键字, 可以省略
别名中不能有特殊字符或者关键字, 如果有就加双引号

*/
select ename 姓名, sal 工资 from emp;

select ename “姓 名”, sal 工资 from emp;

/*
去除重复数据 distinct
多列去除重复: 每一列都一样才能够算作是重复
*/
–单列去除重复
select distinct job from emp;

–多列去除重复的
select distinct job,deptno from emp;

–查询中四则运算
select 1+1 from dual;

–查询员工年薪 = 月薪* 12
select sal*12 from emp;

–查询员工年薪+奖金
select sal12 + comm from emp;
–nvl 函数 : 如果参数1为null 就返回参数2
select sal
12 + nvl(comm,0) from emp;

/*
注意: null值 , 代表不确定的 不可预知的内容 , 不可以做四则运算
*/

/*
字符串拼接:
java : + 号拼接
Oracle 特有的连接符: || 拼接

在Oracle 中 ,双引号主要是别名的时候使用, 单引号是使用的值, 是字符

concat(str1,str2) 函数, 在mysql和Oracle中都有

*/
–查询员工姓名 : 姓名:SCOTT
select ename from emp;
–使用拼接符
select ‘姓名:’ || ename from emp;

–使用函数拼接
select concat(‘姓名:’,ename) from emp;

/*
条件查询 : [where后面的写法]
关系运算符: > >= = < <= != <>
逻辑运算符: and or not
其它运算符:
like 模糊查询
in(set) 在某个集合内
between…and… 在某个区间内
is null 判断为空
is not null 判断不为空
*/
–查询每月能得到奖金的员工信息
select * from emp where comm is not null;

–查询工资在1500–3000之间的员工信息
select * from emp where sal between 1500 and 3000;

select * from emp where sal >= 1500 and sal <= 3000;

–查询名字在某个范围的员工信息 (‘JONES’,‘SCOTT’,‘FORD’) in
select * from emp where ename in (‘JONES’,‘SCOTT’,‘FORD’);

/*
模糊查询: like
% 匹配多个字符
_ 匹配单个字符

    如果有特殊字符, 需要使用escape转义

*/
–查询员工姓名第三个字符是O的员工信息
select * from emp where ename like ‘__O%’;

–查询员工姓名中,包含%的员工信息
select * from emp where ename like ‘%%%’ escape ‘’;

select * from emp where ename like ‘%#%%’ escape ‘#’;

/*
排序 : order by
升序: asc ascend
降序: desc descend

      排序注意null问题 : nulls first | last
      
      同时排列多列, 用逗号隔开

*/
–查询员工信息,按照奖金由高到低排序
select * from emp order by comm desc nulls last;

–查询部门编号和按照工资 按照部门升序排序, 工资降序排序
select deptno, sal from emp order by deptno asc, sal desc;

/*
函数: 必须要有返回值

 单行函数: 对某一行中的某个值进行处理
     数值函数
     字符函数
     日期函数
     转换函数
     通用函数
 
 多行函数: 对某一列的所有行进行处理
       max()  min count sum avg
       
       1.直接忽略空值 

*/
–统计员工工资总和
select sum(sal) from emp;

–统计员工奖金总和 2200
select sum(comm) from emp;

–统计员工人数 14
select count(1) from emp;

–统计员工的平均奖金 550 错误 2200/14 =
select avg(comm) from emp;

–统计员工的平均奖金 157.
select sum(comm)/count(1) from emp;
select ceil(sum(comm)/count(1)) from emp;

update emp set ename = ‘TUR%NER’ where ename = ‘TURNER’;

select * from emp;

–数值函数
select ceil(45.926) from dual; --46
select floor(45.926) from dual; --45
–四舍五入
select round(45.926,2) from dual; --45.93
select round(45.926,1) from dual; – 45.9
select round(45.926,0) from dual; --46
select round(45.926,-1) from dual; --50
select round(45.926,-2) from dual; --0
select round(65.926,-2) from dual; --100

–截断
select trunc(45.926,2) from dual; --45.92
select trunc(45.926,1) from dual; – 45.9
select trunc(45.926,0) from dual; --45
select trunc(45.926,-1) from dual; --40
select trunc(45.926,-2) from dual; --0
select trunc(65.926,-2) from dual; --0

–求余
select mod(9,3) from dual; --0
select mod(9,4) from dual; --1

–字符函数
– substr(str1,起始索引,长度)
–注意: 起始索引不管写 0 还是 1 都是从第一个字符开始截取
select substr(‘abcdefg’,0,3) from dual; --abc
select substr(‘abcdefg’,1,3) from dual; --abc

select substr(‘abcdefg’,2,3) from dual; --bcd

1、substr函数格式   (俗称:字符截取函数)

  格式1: substr(string string, int a, int b);

  格式2:substr(string string, int a) ;

解释:

    格式11、string 需要截取的字符串
        2、a 截取字符串的开始位置(注:当a等于01时,都是从第一位开始截取)
        3、b 要截取的字符串的长度

    格式21、string 需要截取的字符串
        2、a 可以理解为从第a个字符开始截取后面所有的字符串。
2、实例解析
1select substr('HelloWorld',0,3) value from dual; //返回结果:Hel,截取从“H”开始3个字符
 2select substr('HelloWorld',1,3) value from dual; //返回结果:Hel,截取从“H”开始3个字符
 3select substr('HelloWorld',2,3) value from dual; //返回结果:ell,截取从“e”开始3个字符
 4select substr('HelloWorld',0,100) value from dual; //返回结果:HelloWorld,100虽然超出预处理的字符串最长度,但不会影响返回结果,系统按预处理字符串最大数量返回。
 5select substr('HelloWorld',5,3) value from dual; //返回结果:oWo
 6select substr('Hello World',5,3) value from dual; //返回结果:o W (中间的空格也算一个字符串,结果是:o空格W)
 7select substr('HelloWorld',-1,3) value from dual; //返回结果:d (从后面倒数第一位开始往后取1个字符,而不是3个。原因:下面红色 第三个注解)
 8select substr('HelloWorld',-2,3) value from dual; //返回结果:ld (从后面倒数第二位开始往后取2个字符,而不是3个。原因:下面红色 第三个注解)
 9select substr('HelloWorld',-3,3) value from dual; //返回结果:rld (从后面倒数第三位开始往后取3个字符)
10select substr('HelloWorld',-4,3) value from dual; //返回结果:orl (从后面倒数第四位开始往后取3个字符)
注:当a等于01时,都是从第一位开始截取(如:12))
    (注:假如HelloWorld之间有空格,那么空格也将算在里面(如:56))
    (注:虽然78910截取的都是3个字符,结果却不是3 个字符; 只要 |a| ≤ b,取a的个数(如:789);当 |a| ≥ b时,才取b的个数,由a决定截取位置(如:910)11select substr('HelloWorld',0) value from dual;  //返回结果:HelloWorld,截取所有字符
12select substr('HelloWorld',1) value from dual;  //返回结果:HelloWorld,截取所有字符
13select substr('HelloWorld',2) value from dual;  //返回结果:elloWorld,截取从“e”开始之后所有字符
14select substr('HelloWorld',3) value from dual;  //返回结果:lloWorld,截取从“l”开始之后所有字符
15select substr('HelloWorld',-1) value from dual;  //返回结果:d,从最后一个“d”开始 往回截取1个字符
16select substr('HelloWorld',-2) value from dual;  //返回结果:ld,从最后一个“d”开始 往回截取2个字符
17select substr('HelloWorld',-3) value from dual;  //返回结果:rld,从最后一个“d”开始 往回截取3个字符

–获取字符串长度 24 28
select length(‘abcdefg’) from dual;

–去除字符左右两边的空格
select trim(’ hello ') from dual;

–替换字符串
Select replace(‘hello’,‘l’,‘a’) from dual;

–日期函数
–查询今天的日期
select sysdate from dual;
–查询3个月后的今天的日期
select add_months(sysdate,3) from dual;
–查询3天后的日期
select sysdate + 3 from dual;

–查询员工入职的天数
select sysdate - hiredate from emp;

select ceil(sysdate - hiredate) from emp;

–查询员工入职的周数
select (sysdate - hiredate)/7 from emp;

–查询员工入职的月数
select months_between(sysdate,hiredate) from emp;

–查询员工入职的年份
select months_between(sysdate,hiredate)/12 from emp;

–转换函数 数值转字符 字符转数值 日期
–字符转数值 to_number(str) 鸡肋
select 100+‘10’ from dual; --110 默认已经帮我们转换
select 100 + to_number(‘10’) from dual; --110

–数值转字符
select to_char(sal,’$9,999.99’) from emp;

select to_char(sal,‘L9,999.99’) from emp;
/*
to_char(1210.73, ‘9999.9’) 返回 ‘1210.7’
to_char(1210.73, ‘9,999.99’) 返回 ‘1,210.73’
to_char(1210.73, ‘$9,999.00’) 返回 ‘$1,210.73’
to_char(21, ‘000099’) 返回 ‘000021’
to_char(852,‘xxxx’) 返回’ 354’

*/

–日期转字符 to_char()
select to_char(sysdate,‘yyyy-mm-dd hh:mi:ss’) from dual;
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) from dual;
–只想要年
select to_char(sysdate,‘yyyy’) from dual; --2017

–只想要日
select to_char(sysdate,‘d’) from dual; --2 代表一个星期中第几天
select to_char(sysdate,‘dd’) from dual; --10 代表一个月中的第几天
select to_char(sysdate,‘ddd’) from dual; --100 代表一年中的第几天

select to_char(sysdate,‘day’) from dual; --monday
select to_char(sysdate,‘dy’) from dual; --mon 星期的简写

–字符转日期
select to_date(‘2017-04-10’,‘yyyy-mm-dd’) from dual;

–查询1981年 – 1985年入职的员工信息
select * from emp where hiredate between to_date(‘1981’,‘yyyy’) and to_date(‘1985’,‘yyyy’);

/*
通用函数:
nvl(参数1,参数2) 如果参数1 = null 就返回参数2
nvl2(参数1,参数2,参数3) 如果参数1 = null ,就返回参数3, 否则返回参数2

   nullif(参数1,参数2) 如果参数1 = 参数2 那么就返回 null , 否则返回参数1
   
   coalesce: 返回第一个不为null的值

*/
select nvl2(null,5,6) from dual; --6;

select nvl2(1,5,6) from dual; --5;

select nullif(5,6) from dual; --5
select nullif(6,6) from dual; --null

select coalesce(null,null,3,5,6) from dual; --3

select ceil(-12.5) from dual; --12
select floor(12.5) from dual; --12

select ’ hello ’ from dual;
select * from emp;

发布了23 篇原创文章 · 获赞 0 · 访问量 215

猜你喜欢

转载自blog.csdn.net/qq_41518179/article/details/104665462
今日推荐