关于oracle:Oracle基础之一Oracle-11安装与介绍

一、数据库介绍

1、关系型数据库

(1)Mysql(用的最多)
(2)Oracle(最平安)
(3)Sql server(.net)
(4)Db2(金融、银行)

2、非关系型数据库

(1)Hbase
(2)Redis
(3)mongodb

3、Oracle介绍

1、数据库版本

(1)Oracle 8及8i:示意的Internet,向网络倒退,适度版本,只有一张vcd,8i是适度性产品
(2)Oracle9i:是之前应用最宽泛的版本,8i的升级版,1CD
(3)Oracle 10g:700M过渡性产品,其中g示意的是网格计算,以立体网格,以核心查找
(4)Oracle 11g:完整性产品,最新版本2G
(5)Oracle 12c:the world’s first database designed for the cloud

2、用户

(1)sys 超级管理员
(2)system 一般管理员
(3)scott 一般的用户

3、示例–>数据库

(1)数据库实例名 对立应用orcl

二、Oracle装置

1、装置文档

链接:https://pan.baidu.com/s/1OXZt…
提取码:6666

2、装置实现后会有如下服务

3.1 Oracle服务的作用

装置oracle 11g R2中的办法胜利装置Orcle 11g后,共有7个服务,这七个服务的含意别离为:

(1)Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service)可能让存储根底设施(比方磁盘,阵列等)创立高保真的工夫点映像,即映射拷贝(shadow copy),它能够在多个卷或者单个卷上创立映射拷贝,同时捕捉影响到零碎的零碎性能。(非必须启动)
(2)OracleDBConsoleorcl:Oracle数据库控制台服务,orcl是Oracle的实例标识,默认的实例为orcl。在运行Enterprise Manager(企业管理器OEM)的时候,须要启动这个服务。(非必须启动)
(3)OracleJobSchedulerORCL:Oracle作业调度(定时器)服务,ORCL是Oracle实例标识。(非必须启动)
(4)OracleMTSRecoveryService:服务端管制,该服务容许数据库充当一个微软事务服务器MTS、COM/COM+对象和分布式环境下的事务的资源管理器。(非必须启动)
(5)OracleOraDb11g_home1ClrAgent:Oracle数据库.NET扩大服务的一部分。(非必须启动)
(6)OracleOraDb11g_home1TNSListener:监听器服务,服务只有在数据库须要近程拜访的时候才须要。(必须启动,上面会有具体解释)
(7)OracleServiceORCL:数据库服务(数据实例),是Oracle外围服务,该服务是数据库启动的根底,只有该服务启动,Oracle数据库能力失常启动(必须启动)

3.2开发的时候须要启动哪些服务

对老手来说,要是只有Oracle自导的sql*plus的话,只有启动OracleServiceORCL即可,要是应用PL/SQL Developer等第三方工具的话,OracleOraDb11g_home1TNSListener服务也要开启,OracleDBConsoleorcl是进入基于web的EM必须开启的,其余服务很少用
注:ORCL是数据库实例名,默认的数据库是ORCL,你能够创立其余的,即OrcleService+数据库名

3、卸载文档

链接:https://pan.baidu.com/s/1OXZt…
提取码:6666

三、账号治理


四、SQL语言

1、结构化查询语言(Structured Query Language)

具备定义,查问,更新和管制等多种性能,是关系数据库的规范语言

2、sql分类

2.1 DML:数据操纵语言 Data Manipulation Language:

SELECT INSERT UPDATE DELETE

2.2 DDL:数据定义语言 Data definition language

CREATE ALTER DROP RENAME TRUNCATE

2.3 DCL:数据管制语言 Data Control Language

GRANT REVOKE

2.4 Transaction:

commit rollback savepoint

3、示例











comment on column emp.hiredate is '入职日期';
comment on column emp.hiredate is '入职日期';
/*
sql语句学习
SELECT [DISTINCT] {*,column alias,...}
FROM table alias
Where 条件表达式
*/
--查问雇员表中部门编号是10的员工
select empno,ename, job,deptno from emp where deptno=10;
--去除反复数据
select distinct deptno from emp;
--去除反复数据也能够针对多个字段,多个字段值只有有一个不匹配就算是不同的记录
select distinct deptno,sal from emp;
--在查问的过程中能够给列增加别名,同时也能够给表增加别名
select e.empno 雇员编号,e.ename 雇员名称,e.job 雇员工作 from emp e where e.deptno=10;
select e.empno as "雇员 编号",e.ename as 雇员名称,e.job as 雇员工作 from emp e where e.deptno=10;
--查问表中的所有字段,能够应用*,然而在我的项目中不要轻易应用
select * from emp;

/*
=, !=, <>, <, > <=, >=, any, some, all
is null, is not null
between x and y
in(list), not in (list)
exists (sub-query)
like _, %, escape `\` _ \% escape `\` 
*/
-- =
select * from emp where deptno=20;
-- !=
select * from emp where deptno != 20;
-- <> 不等于
select * from emp where deptno <> 20;
-- <, > <=, >=
select sal from emp where sal > 1500;
select sal from emp where sal < 1500;
select sal from emp where sal >= 1500;
select sal from emp where sal <= 1500;
--any 只有sal大于any外面的任意一个值都符合条件
select sal from emp where sal > any(1000,1500,3000)
--some 跟any是同一个成果,只有大于其中某一个值都符合条件
select sal from emp where sal > some(1000,1500,3000)
--all 大于所有的值才会成立
select sal from emp where sal > all(1000,1500,3000)

-- is null, is not null 在sql语法中null示意一个非凡含意, null != null
-- 所以要应用is null 或者is not null 来判断
select * from emp where comm is null;
select * from emp where comm is not null;

-- between x and y 蕴含x 和 y的值
select sal from emp where sal between 1500 and 3000;
select sal from emp where sal >= 1500 and sal <= 3000;

--in(list) 
select * from emp where deptno in(10,20);
select * from emp where deptno =10 or deptno =20;
--not in (list)
select * from emp where deptno not in (10,20);
select * from emp where deptno !=10 and deptno !=20;

-- exists (sub-query) 当exists中的子查问语句能查到后果的时候,意味着条件满足
-- 相当于双重for循环
select *
  from emp e
 where exists (select deptno
          from dept d
         where (deptno = 10 or deptno = 20)
           and e.deptno = d.deptno);
-- like _, %, escape `\` _ \% escape `\`
-- 含糊查问 查问名字以S结尾 应用like的时候要谨慎,因为like效率比拟低
-- 应用like能够参考索引,然而不能以%结尾
select * from emp where ename like ('S%');
select * from emp where ename like ('%S%');
-- 含糊查问 查问名字以S结尾 且倒数第二个字符为T的用户
select * from emp where ename like ('S%T_');
select * from emp where ename like ('S%T%');
select * from emp where ename like ('JO%');
select * from emp where ename like ('_O%'); --第二个字母为O

--查问名字中带%的用户
--escape 应用转义字符,能够本人规定转义字符
select * from emp where ename like ('%\%%') escape('\');
select * from emp where ename like ('%a%%') escape('a');

--order by
--默认按升序排序 ASC
--desc 降序
-- 排序是依照天然程序排序的 数值从小到大,
--字符串依照字典序排序
--在进行排序的时候能够指定多个字段,而且多个字段能够应用不同的排序形式
--每次在执行order by的时候相当于是做了全排序,思考全排序的效率,
-- 比拟消耗系统资源,因而个别统计的事件放在业务不太忙碌的时候进行
select * from emp order by sal;
select * from emp order by sal desc;
select * from emp order by sal desc,ename asc;

-- 创立计算字段
--字符串连贯
select 'my name is '|| ename from emp;
select concat('my name is',ename) from emp;
--计算所有员工的年薪
-- null是比拟非凡的存在,null做任何计算都还是为null,因而要将null进行转换
--nvl(arg1,arg2),如果arg1是空,那么返回arg2,如果不是空,则返回原来的值
select ename,(sal+comm) * 12 from emp;
select ename,(sal+nvl(comm,0))*12 from emp;
--并集 将两个汇合中的所有数据都进行显示,然而不蕴含反复数据
select * from emp where deptno != 30;
select * from emp where sal > 1000;

select * from emp where deptno != 30 union
select * from emp where sal > 1000;
--选集 将两个汇合中的所有数据都进行显示,蕴含反复数据
select * from emp where deptno != 30 union all
select * from emp where sal > 1000;
--交加 两个汇合中穿插的数据集,只显示一次
select * from emp where deptno != 30 intersect
select * from emp where sal > 1000;
--差集 蕴含在A汇合而不蕴含在B汇合中的数据,跟A B汇合的程序无关
select * from emp where deptno != 30 minus
select * from emp where sal > 1000;

五、Oracle函数






















--组函数:又称为聚合函数,输出多个值,最终只会返回一个值,组函数仅可用于抉择列表或查问的having子句
--单行函数:输出一个值,输入一个值
--函数的测试
--查问所有员工的薪水总和
select sum(sal) from emp;
-- 查看表中有多少条记录
select deptno,count(*) from emp group by deptno having count(*) >3;

--字符函数
--concat:示意字符串连贯 等同于||
select concat('my name is ',ename)from emp;
-- inicap 将字符串首字母大写
select initcap(ename) from emp;
-- 将字符串全副转换为大写
select upper(ename) from emp;
-- 将字符串全副转换为小写
select lower(ename) from emp;
-- 填充字符串
select lpad(ename,10,'*') from emp;
select rpad(ename,10,'*') from emp;
-- 去除空格
select trim(ename) from emp;
select ltrim(ename) from emp;
select rtrim(ename) from emp;
--查找指定字符串的地位
select instr('ABABCDEF','A') FROM emp;
--查看字符串的长度
select length(ename) from emp;
--截取字符串
select substr(ename,0,2) from emp;
--替换操作
select replace(ename,'S','hehe') from emp;

-- 数值函数
-- 四舍五入 能够指定小数局部的位数
select round(123.126,2) from dual;
-- 截断数据 依照位数进行截取,然而不会进行四舍五入的操作
select trunc(123.128,2) from dual;
-- 取模操作
select mod(10,4) from dual;
-- 向上取整
select ceil(12.12) from dual;
-- 向下取整
select floor(12.13) from dual;
-- 取绝对值
select abs(-100) from dual;
--获取正负值
select sign(-100) from dual;
--获取x的y次幂
select power(2,3) from dual;

--日期函数
select  sysdate from dual;
select current_date from dual;
--add_months,增加指定的月份
select add_months(hiredate,2),hiredate from emp;
-- 返回输出日期月份的最初一天
select last_day(sysdate) from dual;
-- 两个日期距离的月份
select months_between(sysdate,hiredate) from emp;
-- round 返回四舍五入的第一天
select sysdate 过后日期,
round(sysdate) 最近0点日期,
round(sysdate,'day') 最近星期日,
round(sysdate,'month') 最近月初,
round(sysdate,'q') 最近季初日期, 
round(sysdate,'year') 最近年初日期 from dual;

-- 返回下周的星期几
select sysdate 过后日期,
next_day(sysdate,'星期一') 下周星期一,
next_day(sysdate,'星期二') 下周星期二,
next_day(sysdate,'星期三') 下周星期三,
next_day(sysdate,'星期四') 下周星期四,
next_day(sysdate,'星期五') 下周星期五,
next_day(sysdate,'星期六') 下周星期六,
next_day(sysdate,'星期日') 下周星期日 from dual;

--提取日期中的工夫
select 
extract(hour from timestamp '2001-2-16 2:38:40 ' ) 小时,
extract(minute from timestamp '2001-2-16 2:38:40 ' ) 分钟,
extract(second from timestamp '2001-2-16 2:38:40 ' ) 秒,
extract(DAY from timestamp '2001-2-16 2:38:40 ' ) 日,
extract(MONTH from timestamp '2001-2-16 2:38:40 ' ) 月,
extract(YEAR from timestamp '2001-2-16 2:38:40 ' ) 年
 from dual;

-- 返回日期的工夫戳
select localtimestamp from dual;
select current_date from dual;
select current_timestamp from dual;
-- 给指定的工夫单位减少数值
select
trunc(sysdate)+(interval '1' second), --加1秒(1/24/60/60)
trunc(sysdate)+(interval '1' minute), --加1分钟(1/24/60)
trunc(sysdate)+(interval '1' hour), --加1小时(1/24)
trunc(sysdate)+(INTERVAL '1' DAY),  --加1天(1)
trunc(sysdate)+(INTERVAL '1' MONTH), --加1月
trunc(sysdate)+(INTERVAL '1' YEAR), --加1年
trunc(sysdate)+(interval '01:02:03' hour to second), --加指定小时到秒
trunc(sysdate)+(interval '01:02' minute to second), --加指定分钟到秒
trunc(sysdate)+(interval '01:02' hour to minute), --加指定小时到分钟
trunc(sysdate)+(interval '2 01:02' day to minute) --加指定天数到分钟
from dual;

--转换函数:在oracle存在数值的隐式转换和显示转换
--隐式转换:是指字符串能够转换为数值或者日期
--显示转换:
--   to_char:当由数值或者日期转换成字符串的时候,必须要规定格局
select '999'+10 from dual;
-- date to_char
select to_char(sysdate,'YYYY-MM-dd HH24:MI:SS') from dual;
--number to_char
select to_char(123.456789,'99999999') from dual;
select to_char(123.456789,'0000.00') from dual;
select to_char(123.456789,'$0000.00') from dual;
select to_char(123.456789,'L0000.00') from dual;
select to_char(123456789,'999,999,999,999') from dual;
--to_date 转换之后都是固定格局
select to_date('2019-10-10 10:10:10','YYYY-MM-DD HH24:MI:SS') from dual;
--to_number:转成数字
select to_number('123,456,789','999,999,999')from dual;

--显示没有下级治理的公司首脑
select ename,nvl(to_char(mgr),'no manager') from emp where MGR is null;
--显示员工雇佣期满6个月后下一个星期五的日期
select hiredate,next_day(add_months(hiredate,6),'星期五') from emp;


--其余函数
-- 条件函数
-- decode, case when
-- 给不同部门的人员涨薪,10部门涨10%,20部门涨20%,30部门涨30%
select ename,sal,deptno,decode(deptno,10,sal*1.1,20,sal*1.2,30,sal*1.3) from emp;
select * from emp;

select ename,
       sal,
       deptno,
       case deptno
         when 10 then
          sal * 1.1
         when 20 then
          sal * 1.2
         when 30 then
          sal * 1.3
       end
  from emp;




-- 组函数,个别状况下,组函数都要和group by组合应用
-- 组函数个别用于抉择列表(select)和having子句
-- 罕用的组函数
--avg() 平均值 实用于数值类型
--min() 最小值 实用于任何类型
--max() 最大值 实用于任何类型
-- count() 记录数 个别用来获取表中的记录数,倡议应用count(1),count(id)这种形式,不倡议应用count(*)
--sum()求和 实用于数值类型
select avg(sal) from emp;
select min(sal) from emp;
select max(sal) from emp;
select count(sal) from emp;
select sum(sal) from emp;

--group by 依照某些雷同的值进行分组操作,能够指定一个列或者多个列,
-- 然而应用了group by之后,抉择列表中只能蕴含组函数的值或者group by的一般字段
--例如:select deptno, avg(sal),deptno,ename from emp group by deptno having avg(sal)>2000;
--这个sql语句中的ename会报错
--求每个部门的均匀薪水
select deptno, avg(sal) from emp group by deptno;
--求均匀薪水大于2000的部门
select deptno, avg(sal),deptno from emp group by deptno having avg(sal)>2000;

六、关联查问








--关联查问
--查问雇员名称和部门名称
select ename,dname from emp,dept where dept.deptno = emp.deptno;--等值连贯
--查问雇员名称以及本人的薪水等级
select e.ename, e.sal, sg.grade
  from emp e, salgrade sg
 where e.sal between sg.losal and sg.hisal; --非等值连贯
--外连贯 左外连贯(把右边表的数据全副显示), 右外连贯(把左边表的数据全副显示)
select * from emp, dept where emp.deptno(+) = dept.deptno;--右外连贯
select * from emp, dept where emp.deptno = dept.deptno(+);--左外连贯
select * from emp, dept where emp.deptno = dept.deptno;
--自连贯 将一张表当成不同的表来对待,本人关联本人
--将雇员和他经理的名称查出来
select e.ename,m.ename from emp e,emp m where e.mgr = m.empno;
--笛卡尔积 当关联多张表,然而不指定连贯条件的时候,会进行笛卡尔积
select * from emp e,dept d;

-- cross join
select * from emp cross join dept;
--natural join 相当于等值连贯,不须要写连贯条件
--当两张表中不具备雷同的列名的时候,会进行笛卡尔积
select * from emp e natural join dept d;
select * from emp e natural join salgrade d;
--on子句,增加连贯条件
select * from emp e join dept d on e.deptno= d.deptno; --等值连贯
select * from emp e join salgrade sg on e.sal between sg.losal and sg.hisal; --非等值连贯
--left outer join 左外连贯
select * from emp e left outer join dept d on e.deptno = d.deptno; 
select * from emp e left join dept d on e.deptno = d.deptno; 
--right outer join右外连贯
select * from emp e right outer join dept d on e.deptno = d.deptno; 
--full outer join 全连贯
select * from emp e full outer join dept d on e.deptno = d.deptno; 
--inner join 两张表的连贯查问,只会查问出有匹配记录的数据
select * from emp e inner join dept d on e.deptno = d.deptno;
--using 除了能够应用on示意连贯条件之外,也能够应用using作为连贯条件,此时连贯条件的列不再归属于任何一张表
select deptno,e.ename from emp e join dept d using(deptno);
select e.deptno from emp e join dept d on e.deptno = d.deptno;
--检索雇员名称,所在单位,薪水等级
select e.ename, d.loc, sg.grade
  from emp e
  join dept d
    on e.deptno = d.deptno
  join salgrade sg
    on e.sal between sg.losal and sg.hisal;

七、子查问








--子查问:嵌套在其余sql语句中的残缺的sql语句,能够称之为子查问
--分类 单行子查问  多行子查问
--有哪些人的薪水是在整个雇员的均匀薪水之上的
select avg(sal) from emp;
select * from emp where sal >(select avg(sal) from emp);
--查问雇员中有哪些人是经理人
select * from emp where empno in (select distinct(t.mgr) from emp t );
--找出部门编号为20的所有员工中支出最高的职员
select sal from emp where deptno=20;
select * from emp where sal >= all(select sal from emp where deptno=20) and emp.deptno = 20;
--求每个部门均匀薪水的等级
select avg(sal) from emp group by deptno;
select t.deptno,t.vsal,sg.grade from salgrade sg join (select deptno,avg(sal) vsal from emp group by deptno) t
on t.vsal between sg.losal and sg.hisal;
--求均匀薪水最高的部门的编号
select t.deptno,avg(sal) vsal from emp t group by t.deptno;
select max(t.vsal) from (select deptno,avg(sal) vsal from emp group by deptno)t;
select e.deptno from (select t.deptno, avg(sal) vsal from emp t group by t.deptno) e
 where e.vsal =
       (select max(t.vsal)
          from (select deptno, avg(sal) vsal from emp group by deptno) t);
--求部门均匀薪水的等级
select avg(sal) from emp group by deptno;
select sg.grade from salgrade sg join (select avg(sal) vsal from emp group by deptno)t
on t.vsal between sg.losal and sg.hisal;

--求部门均匀的薪水等级
select e.deptno,sg.grade from salgrade sg,emp e where e.sal between sg.losal and sg.hisal ;
select t.deptno, avg(t.grade)
  from (select e.deptno, sg.grade
          from salgrade sg, emp e
         where e.sal between sg.losal and sg.hisal) t group by t.deptno;
--求薪水最高的前5名雇员
--限度输入:在oracle中,如果须要应用限度输入和分页的性能的话,必须要应用rownum,
--然而rownum不能间接应用,必须要嵌套
select * from emp order by sal desc;
select * from (select * from emp where sal is not null order by sal desc) where rownum <=5;

--求薪水最高的第6到10名雇员
--应用rownum的时候必须要在外层增加嵌套,此时能力将rownum作为其中的一个列,而后进行限度输入
select t1.*,rownum from (
select * from emp e order by e.sal desc)t1 where rownum <= 10;

select *
  from (select t1.*, rownum rn
          from (select * from emp e order by e.sal desc) t1
         where rownum <= 10) t
 where t.rn > 5
   and t.rn <= 10
   
select * from (select t1.*,rownum rn from
 (select * from emp e order by e.sal desc) t1) t where t.rn >5 and t.rn <= 10;

/*
表内容:
2005-05-09  胜
2005-05-09  胜
2005-05-09  负
2005-05-09  负
2005-05-10  胜
2005-05-10  负
2005-05-10  负

生成以下后果
     日期  胜 负
2005-05-10    1    2
2005-05-09    2    2

*/
select t.rq,
       count(decode(shengfu, '胜', 1)) 胜,
       count(decode(shengfu, '负', 2)) 负
  from tmp t
 group by rq;

/*
student_score 表转成以下格局
姓名    语文    数学    英语
王五     89      56      89
*/
select * from student_score;
--decode
select ss.name, 
max(decode(subject,'语文',ss.score)) 语文,
max(decode(subject,'数学',ss.score)) 数学,
max(decode(subject,'英语',ss.score)) 英语
from student_score ss group by ss.name
--case when
select ss.name,
max(case ss.subject when '语文' then ss.score end) 语文,
max(case ss.subject when '数学' then ss.score end) 数学,
max(case ss.subject when '英语' then ss.score end) 英语
from student_score ss group by ss.name
--join
select ss.name, ss.score from student_score ss where ss.subject='语文';
select ss.name, ss.score from student_score ss where ss.subject='数学';
select ss.name, ss.score from student_score ss where ss.subject='英语';

select ss01.name,ss01.score 语文,ss02.score 数学,ss03.score 英语  
from (select ss.name, ss.score from student_score ss where ss.subject='语文') ss01
join (select ss.name, ss.score from student_score ss where ss.subject='数学') ss02
on ss01.name = ss02.name
join (select ss.name, ss.score from student_score ss where ss.subject='英语') ss03
on ss01.name = ss03.name;

--union all
select t.name,max(t.score01) 语文,max(t.score02) 数学,max(t.score03) 英语 from (
select ss01.name, ss01.score score01, 0 score02,0 score03 from student_score ss01 where ss01.subject='语文'  union all
select ss02.name, 0 score01,ss02.score score02, 0 score03 from student_score ss02 where ss02.subject='数学' union all
select ss03.name, 0 score01, 0 score02,ss03.score score03 from student_score ss03 where ss03.subject='英语') t
 group by t.name;

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理