-------------------------------Oracle体系结构------------------
--数据库:database
Oracle数据库只有一个database,orcl-全局数据库
--实例
是由一系列的进程和内存结构组成,Oracle中可以有多个实例,但是99.9%只用一个实例
--数据文件
是存放数据的文件 扩展名.dbf
--表空间
是数据文件的一个逻辑映射
--用户
管理数据
select * from scott.emp
------------------重置scott用户的密码--------
用户名:scott
密码:tiger
重置scott用户的密码
alter user scott identified by tiger
-----------------一下内容用scott用户----------------
--scott用户的三张表介绍
select * from dept;--部门
deptno:部门编号
dname:部门名称
loc:位置
select * from emp;--雇员、员工
empno:员工编号
ename:员工名称
job:职位
mgr:上级
hiredate:雇佣日期 入职时间
sal:工资
comm:奖金
deptno:部门编号
select * from salgrade;--工资等级
grade:等级号
losal:最低工资
hisal:最高工资
------------基本查询------
select * from emp;
--别名
select empno as "员工编号",job as 职位,hiredate "入职时间" ,sal 工资 from emp;
/*
中文乱码问题解决
1.查看服务器端编码
select userenv('language') from dual;
我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK
2.执行语句 select * from V$NLS_PARAMETERS
查看第一行中PARAMETER项中为NLS_LANGUAGE 对应的VALUE项中是否和第一步得到的值一样。
如果不是,需要设置环境变量.
否则PLSQL客户端使用的编码和服务器端编码不一致,插入中文时就会出现乱码.
3.设置环境变量
计算机->属性->高级系统设置->环境变量->新建
设置变量名:NLS_LANG,变量值:第1步查到的值, 我的是 AMERICAN_AMERICA.ZHS16GBK
4.重新启动PLSQL,插入数据正常
*/
--去重
select distinct job from emp;
--四则运算:加减乘除
select sal*12 from emp
--字符串的连接 || concat
员工编号是XXX,姓名是XXX,职位是XXX
select '员工编号是'||empno||',姓名是'||ename||',职位是'||job info from emp
---------------条件查询--------
--等值
=
>
>=
<
<=
<> !=
smith
select * from emp where ename='SMITH'--oracle查询 大小写敏感
--其他查询
--in
7788 7369 7566
select * from emp where empno in(7788,7369,7566);
--between ... and .. 包含临界值
查询工资大于1500并且小于3000的员工
select * from emp where sal>=1500 and sal<=3000;
select * from emp where sal between 1500 and 3000;
--is null
查询奖金为空的员工
select * from emp where comm is null
--逻辑查询
--or
查询职位是CLERK或者工资大于1500
select * from emp where job='CLERK' or sal>1500
--and
查询职位是CLERK并且工资大于1500
select * from emp where job='CLERK' and sal>1500
--not
查询工资不大于1500
select * from emp where sal<=1500;
select * from emp where not (sal>1500);
select * from emp where empno not in(7788,7369,7566);
------------------模糊查询-------
like _ %
--查询员工姓名中第二个字母带M的
select * from emp where ename like '_M%'
select * from emp where ename like '%M%'
--查询员工姓名中带_的
select * from emp where ename like '%m_%' escape 'm'
select * from emp where ename like '%n_%' escape 'n'
select * from emp where ename like '%2_%' escape '2'
select * from emp where ename like '%8_%' escape '8'
select * from emp where ename like '%@_%' escape '@'
select * from emp where ename like '%!_%' escape '!'
select * from emp where ename like '%__%' escape '_'
select * from emp where ename like '%%_%' escape '%'
select * from emp where ename like '%&_%' escape '&'
-------------排序-------
order by 放在查询语句的最后面
order by 列名 asc desc
--按照奖金倒序排序
--nulls last 把null值放到最后
select * from emp order by comm desc nulls last
--nulls first 把null值放到最前
select * from emp order by comm nulls first
-----------------------单行函数-----------------------
--字符函数
select * from dual
--伪表dual 为了配合查询语句
--lower 转小写
select lower('ABC') from dual;
select 'abc' from dual
select 123 from dual
select lower(ename) from emp;
--concat 连接函数 ||
select concat('aaa','bbb') from dual
--replace 替换
select replace('aabbdd','dd','cc') from dual
--substr 截取 起始位置0和1是一样
select substr('qwertyui',3) from dual;--ertyui
select substr('qwertyui',0,3) from dual;--qwe
select substr('qwertyui',1,3) from dual;--qwe
select substr('qwertyui',2,3) from dual;--wer
--length 长度
select length('qwertyui') from dual;--8
--数值函数
--round 四舍五入
select round (12.34) from dual--12
select round (12.34789) from dual--12
select round (12.34789,2) from dual--12.35
select round (12.34489,2) from dual--12.34
select round (12.34489,-1) from dual--10
select round (15.34489,-1) from dual--20
select round (15.34489,-2) from dual--0
--trunc 截断
select trunc (12.34) from dual--12
select trunc (12.34789) from dual--12
select trunc (12.34789,2) from dual--12.34
select trunc (12.34489,2) from dual--12.34
select trunc (12.34489,-1) from dual--10
select trunc (15.34489,-1) from dual--10
select trunc (15.34489,-2) from dual--0
--mod 取模 取余
select mod(10,3) from dual
select mod(10,2) from dual
--日期函数
日期-日期=数值 单位:天
日期+数值=日期
每个员工截止到现在入职的天数
内置对象 sysdate 当前时间
select sysdate from dual
select ename,hiredate, round(sysdate-hiredate) from emp;
每个员工截止到现在入职的周数
select ename,hiredate, round((sysdate-hiredate)/7) from emp;
每个员工截止到现在入职的月数
--months_between两个日期的月份差
select ename,hiredate, round(months_between(sysdate,hiredate)) from emp;
获取两个月之后的日期
--add_months N月之后的日期
select add_months(sysdate,2) from dual
--转换函数
字符 数值 日期
--to_number 字符转数值
select '123', to_number('123') from dual
select '123'+'123' from dual
--to_char 可以把数值和日期转成字符串
select 123, to_char(123) from dual
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate, 'yyyy'),
to_char(sysdate, 'mm'),
to_char(sysdate, 'dd'),
to_char(sysdate, 'hh24'),
to_char(sysdate, 'day')
from dual;
--to_date 把字符串转成日期
查询入职日期是1980-12-17的员工
select * from emp where hiredate =to_date ( '1980-12-17','yyyy-mm-dd')
--通用函数
--nvl
计算年薪 如果null参与计算结果恒为null
select ename,sal,comm,sal*12+nvl(comm,0) from emp
--decode 是oracle独有的--用来单独指定字段名
把员工的表中的job值显示成中文
select * from emp
select ename,job,
decode(job,
'CLERK','店员',
'SALESMAN','销售员',
'MANAGER','经理',
'其他'
)
from emp
--表达式
语法:
case 字段 when then
when then
else
end
select ename,
job,
case job
when 'CLERK' then
'店员'
when 'MANAGER' then
'经理'
else
'其他'
end
from emp
------------------多行函数 聚合函数 组函数---------
-- max min avg count sum
select max(sal) from emp
select count(*) from emp
--分组 group by
每个部门的最大工资
select deptno,max(sal) from emp group by deptno
每个部门的最大工资大于3000的
select deptno,max(sal) from emp group by deptno having max(sal)>3000
where和having
where过滤的是表中的物理字段
having过滤的是组函数计算出的结果
where用在group by之前
having用在group by之后
---重点:
单行函数
replace
substr
round
to_date
to_char
decode
组函数
max min avg count sum
843

被折叠的 条评论
为什么被折叠?



