web_day01_oracle入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35537301/article/details/83628702

一、oracle介绍

1、oracle

2、oracle体系结构

二、oracle入门

1、新用户登录

1.1 解锁用户

--使用DBA(system)解锁用户的语句
alter user scott account unlock;

1.2 修改密码

alter user scott identified by tiger;

2、基本查询

2.1 简单查询

--查询一下员工信息
select * from emp ;--效率稍微高一丢丢
select empno,ename,job from emp ;

2.2 别名查询(使用双引号)

--"" 只能用来修饰别名 其他地方用单引号
select empno as eno ,ename ee,job "职位" from emp ;

2.3 四则运算

--查年薪
select sal*13 "年薪" from emp;

2.4 去重查询

--去重复
select distinct job from emp;

2.5  连接字符(||)

-----连接字符
select concat(empno,ename) from emp ;--mysql,oracle

select empno|| '====' ||ename from emp ;--oracle特有

select '员工编号是'||empno||',姓名是'||ename||',职位是'||job info from emp;

2.6 null的处理

  • 什么是null
    • 包含null的表达式都为null
    • 空值永远不等于空值
---null 很特殊   null 不等于null  ,null 跟谁运算谁就变成null
select e.sal*16+nvl(e.comm,0) "年薪",e.comm "奖金" from emp e;

3、条件查询

--条件查询

--查询奖金comm为空并且工资大于 1500
select * from emp e where e.comm is null and e.sal>1500;


--查询奖金comm为空并且工资不大于 1500
select * from emp e where e.comm is null and  e.sal <=1500;
select * from emp e where e.comm is null and  not(e.sal >1500);


--查询奖金commm为空或者工资大于 1500
select * from emp e where e.comm is null  or  e.sal >1500;


--范围查询

--查询工资大于1500并且小于3000 
select * from emp e where e.sal >1500 and e.sal<3000;
--between:包含临界点
select * from emp e where e.sal between 1500 and 3000;


--查询员工编号是,7369,7788,7654的员工
select * from emp e where e.empno=7369 or e.empno=7788 or e.empno=7654;
select * from emp e where e.empno in (7369,7788,7654);--效率一样,但是in,or查询效率不高


--查询员工姓名是SMITH,MARTIN,SCOTT
select * from emp e where e.ename  in ('SMITH','MARTIN','SCOTT');


--查询员工编号不等于  7369的员工
select * from emp e where e.empno !=7369;
select * from emp e where e.empno <>7369;--查询效率一样

4、排序(order by)

--按工资排序
---升序
select * from emp e order by e.sal;
select * from emp e order by e.sal asc;
---降序
select * from emp e order by e.sal desc;


--按奖金排序  降序
select * from emp e order by e.comm desc  nulls last;
select * from emp e order by e.comm desc  nulls first;

5、模糊查询

--模糊查询

--查询员工姓名中带M的
select * from emp e where e.ename like  '%M%';

--oracle数据区分大小写,而关键字不区分
select * from emp e where e.ename like  '%m%';
SELECT * FROM EMP E WHERE E.ENAME LIKE  '%M%';

--查询员工姓名第二个字母是M的员工
select * from emp e where e.ename like  '_M%';

--查询员工姓名第三个字母是M的员工
select * from emp e where e.ename like  '__M%';

--查询员工姓名带_的员工
select * from emp e where e.ename like '%r_%'  escape 'r';--r第一次出现的地方是普通字符串

6、单行函数(调用执行完,几行数据,还是几行数据)

1.字符函数

--字符函数
select lower(e.ename) from emp e;--小写

select upper( ename) from emp ;--大写
select upper('abcdef') from emp ;
select upper('abcdef') from dual;--伪表,虚表:用来补全语法

--字符连接
select concat( concat('abc','df'),'bvv') from dual;

--去空格:trim
select trim('  agc  vvv    ') from dual;

--替换:replace
select replace('  agc  vvv    ',' ','') from dual;

2.数值函数

--四舍五入 round
select round(56.349) from dual  --56
select round(56.549) from dual  --57
select round(56.349,2) from dual--56.35
select round(56.349,-1) from dual--60
select round(53.349,-1) from dual--50
 
--截断  trunc
select trunc(56.349) from dual  --56
select trunc(56.549) from dual  --56
select trunc(56.349,2) from dual--56.34
select trunc(56.349,-1) from dual--50
select trunc(53.349,-1) from dual--50
 
--取余  mod
select mod(10,3) from dual
select mod(10,2) from dual
select mod(10,0) from dual--10

3.转换函数

--转换函数

--字符转数字(to_number)
select '123' ,to_number('123') from dual;

--数字转字符(to_char)
select 123,to_char(123) from dual;

select * from emp e where e.empno=7369;
select * from emp e where e.empno='7369';

--日期转字符
--to_char(日期,要转换成什么样的格式)
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;--12小时
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;--24小时

--字符转日期
--to_date(字符串,按照什么形式解析)
select to_date('2017-08-25 12:06:09','yyyy-mm-dd hh24:mi:ss') from dual;

4.日期函数

--当前时间  sysdate
select sysdate from dual
 
--查询3个月以后的时间
--add_months(时间,数值)
select add_months(sysdate,3) from dual
select add_months(sysdate,-3) from dual
select add_months(sysdate,1) from dual


--计算每个员工的入职天数
--时间-时间=数值    数值的单位是:天
--时间+数值=时间
select empno,ename,hiredate , sysdate-hiredate from emp


--计算每个员工的入职月数
--months_between(当前时间,要计算的初始时间)
select empno,ename,hiredate , months_between(sysdate,hiredate) from emp

5.通用函数

--滤空函数  nvl(字段,0)
select sal*12+nvl(comm,0) from emp
 
 
--decode类似于条件表达式  只有Oracle有
--语法  decode(字段名,'值1','显示的值1','值2','显示的值2')
select decode(ename,
       'CLARK','鲁班七号',
       'KING','黄忠',
       'MILLER','安其拉'
       ) "姓名"
from emp where deptno=10;
 
 
-- case表达式
--语法:case 字段 when 值  then 显示的值.....end
select
    case ename 
        when 'CLARK' then  '鲁班七号'
        when 'KING' then  '黄忠'
        when 'MILLER' then  '安其拉'    
    end  "姓名"
from emp where deptno=10;

7、多行函数(调用完,几行数据,转换成一行)

1.聚合函数(会忽略空值)

--聚合函数
select count(*) form emp;--14
select count(empno) from emp;--14
select count(comm) from emp ;--4
select count(nvl(comm,0)) from emp;--14

--查询员工工资和
--sum
select sum(sal) from emp ;

--查询员工工资平均数
--avg
select avg(sal) from emp ;

--查询员工最高工资
--max  最大
select max(sal) from emp ;

--查询员工最低工资
--min 
select min(sal) from emp ;

2.分组

SQL中使用group by,select后只能使用聚合函数,或者分组的列名

--按部门查询员工数量
select deptno "部门",count(1) "人数" from emp group by deptno;


--按部门查询员工数量 获取数量大于5的
--having 对分组后的结果做再次过滤
select deptno "部门",count(1) "人数" from emp group by deptno having count(1)>5;

总结

SELECT select_list [INTO new_table_name] 
[FROM table_source] 
[WHERE search_condition] 
[GROUP BY group_by_expression] 
[HAVING search_condition] 
[ORDER BY order_expression [ASC | DESC]]

猜你喜欢

转载自blog.csdn.net/qq_35537301/article/details/83628702