Oracle笔记


Posted in Oracle onApril 05, 2021

一、创建表空间及用户授权

-- 创建表空间

create tablespace zhous --文件的名字
datafile 'f:\zhou.dbf' --文件的储存地址
size 100m --文件大小
autoextend on next 10m; --每次扩容大小 


--删除表空间
drop tablespace zhous;


--创建用户
create user zhouxs --用户名字
identified by 123123 --用户密码
default tablespace zhous --默认表空间

--授权
connect --连接角色
resource --开发者角色
dba --超级管理员

grant dba to zhouxs; --给用户赋予超级管理员权限

二、对表的基本操作

--创建数据表
create table person(
       pid number(21),
       pname varchar(25)
)

--修改表结构
alter table person add(sex number(2)); --添加一列

alter table person modify sex varchar(2); --修改表字段类型

alter table person rename column sex to sexs; --修改列名称

alter table person drop column sexs; --删除一列

insert into person values('1','张三'); --插入数据

delete from person; --删除整个表
drop from person; --删除表结构

truncate table person; --先删除表再创建表

 
update person set pname='李四' where pid=1 --修改

select * from person; --查询



--创建序列 每次自增1
create sequence s_person;

insert into person values(s_person.nextval,'王五')

三、函数

3.1、单行函数

--scott用户,密码tiger
--解锁scott用户
alter user scott account unlock;

--解锁用户密码(也可以设置密码)
alter user scott identified by tiger;

select * from emp e


--单行函数:作用于一行,返回一个值
--字符函数
select upper('hello') from dual --小写转大写
select lower('HELLO') from dual --小写转大写

--数值函数
select round(45.6,-1) from dual --四舍五入,后面显示保留的小数
select trunc(46.7,-1) from dual --直接截取。不看后面的数字

--日期函数
--查出所偶有员工距离现在有多少天
select sysdate-e.hiredate from emp e 

--算出明天此刻
select sysdate+1 from dual

--算出所有员工入职距离现在有多少个月
select months_between(sysdate,e.hiredate) from emp e;

--算出所有员工入职距离现在有多少年
select months_between(sysdate,e.hiredate)/12 from emp e;

--算出所有员工入职距离现在有多少周
select round((sysdate-e.hiredate)/7) from emp e;

--日期转换函数,日期转字符串
select to_char(sysdate,'fm yyyy-mm-dd hh24:mi:ss') from dual;

--字符串转日期
select to_date('2021-4-2 20:58:56','fm yyyy-mm-dd hh24:mi:ss') from dual;

--通用函数
--null值运算结果都为null
select e.sal*12+nvl(e.comm,0) from emp e;

3.1、多行函数

-多行函数 作用域多行函数,返回一个值

select count(1) from emp; --查询总数量

select sum(e.sal) from emp e; --求和

select min(e.sal) from emp e; --求最大值

select avg(e.sal) from emp e; --求平均值

select max(e.sal) from emp e; --求最大值

四、条件表达式

--条件表达式

--相当于switch

select e.ename,
       case e.ename
         when 'SMITH' then '史密斯'
           when 'ALLEN' then '艾伦'
             else '无名'
               end
 from emp e;
 
 
 --判断工资范围
 select e.sal,
          case
       when e.sal>3000 then '高收入'
         when e.sal>1500 then '中等收入'
           else '低收入'
             end
from emp e;


--oracle专用表达式
select e.ename,
 decode(e.ename,
 'SMITH','史密斯',
 'ALLEN','艾伦',
 '无名') "中文名" 
from emp e;

五、复杂查询和分页查询

--分组查询,group by后面的列才能出现在select 后面,想要出现在group by后面加聚合函数
select e.deptno,avg(e.sal) from
emp e group by e.deptno;



--查询部门平均工资大于2000的人
--所有条件不能使用别名来判断
select e.deptno,avg(e.sal) from
emp e group by e.deptno having avg(e.sal)>2000;

--where过滤分组前的数据,having过滤分组之后的数据
select e.deptno,avg(e.sal) from
emp e 
where e.sal>800
group by e.deptno;


--查询部门大于800的员工工资,再查询除部门平均工资大于2000的人
select e.deptno,avg(e.sal) 
from emp e 
where e.sal>800
group by e.deptno having avg(e.sal)>2000;


--笛卡尔积
select * from emp e,dept d;

--等值连接
select * from emp e,dept d where e.deptno=d.deptno;

--内连接
select * from emp e inner join dept d on e.deptno=d.deptno;

--外连接
--右连接
select * from emp e right join dept d on e.deptno=d.deptno;


--左连接
select * from emp e left join dept d on e.deptno=d.deptno;


--oracle专用的外连接
select * from emp e,dept d where e.deptno(+)=d.deptno;


--自连接
select e1.ename,e2.mgr
from emp e1,emp e2
where e1.mgr=e2.empno


--子查询
select * from emp where sal in
(select sal from emp where ename='scott')

--查询工资和10号部门任意工资一样的员工
select * from emp where sal in
(select sal from emp where deptno=10)


--查询每个部门的最低工资,最低工资员工姓名,员工所在部门
select t.deptno,t.msal,e.ename,d.dname 
from (
select deptno,min(sal) msal from emp group by deptno
)t,emp e,dept d
where t.deptno =e.deptno and t.msal=e.sal
and e.deptno =d.deptno


--分页查询
--rownum行号
--查询一行记录,就会在该行加上一个行号,从一开始,依次递增,不能跳着走
select rownum,e.* from emp e order by e.sal desc

select rownum, t.* from (
select rownum,e.* from emp e order by e.sal desc
) t;

select * from (
select rownum rn,tt.* from(
select * from emp  order by sal desc
) tt where rownum<11 
)  where rn>5

六、视图和索引

--创建视图,必须有管理员权限
create table emp as select * from scott.emp;

select * from emp;
--创建视图
create view v_emp as select ename,job from emp;

--查询视图
select * from v_emp;

--修改视图[不推荐]
update v_emp set job='222' where ename='smith';

--创建只读视图
create view v_emp1 as  select ename,job from emp with read only;

--视图的作用
--①屏蔽敏感字段
--②保证数据的及时统一



--索引
--普通索引
create index idx_name on emp(ename)

select * from emp where ename='KING' --触发单列索引

--复合索引
create index idx_name on emp(ename,job)

select * from emp where ename='KING' and job='xx'; --触发索引

select * from emp where ename='KING' or job='xx'; --不触发索引

七、PLSQL编程

plsql是对sql语言的扩展,使得sql具有过程化编程的特性,灵活高效,主要用来编写过程函数和存储函数。

7.1、变量定义和赋值

--声明方法
--赋值操作  := 也可以 into查询语句赋值
declare
   i number(3):=1; --赋值
   name varchar(10):='张三'; --引用型变量
   ena emp.ename%type;
   emprow emp%rowtype; --记录型变量
begin
  dbms_output.put_line(i);
  dbms_output.put_line(name);
  select ename into ena from emp where empno=7788;
  select * into emprow from emp where empno=7788;
  dbms_output.put_line(ena);
  dbms_output.put_line(emprow.ename ||'的工作为:'||emprow.job);
end;

7.2、PLSQL条件判断和循环

--PLSQL--IF判断 dbms_output.put_line--输出语句
declare
     i number(3):=&ii;
begin
  if i<18 
    then dbms_output.put_line('未成年');
    elsif i<40 
      then dbms_output.put_line('中年');
      else
        dbms_output.put_line('老年');
  end if;
end;


--PLSQL--循环
--三种循环输出1-10
declare
  i number(2):=1;
begin
  while i<11 loop --i<10为退出条件
       dbms_output.put_line(i);
       i:=i+1;
  end loop;
end;

--exit循环
declare
  i number(2):=1;
begin
  loop
    exit when i>10;
    dbms_output.put_line(i);
       i:=i+1;
  end loop;
end;

--for循环
declare
begin
  for i in 1..10 loop
     dbms_output.put_line(i);
    end loop;
  end;

八、游标

--游标:可以存放多个对象,多行记录。
--输出emp表中的所有数据
declare
  cursor cl is select * from emp; --创建游标,游标的数据来自emp表
  emprow emp%rowtype; --用来保存一行数据
begin --开始
  open cl; --打开游标
       loop --开始循环
         fetch cl into emprow; --将遍历结果放到变量
         exit when cl%notfound; --没找到退出
         dbms_output.put_line(emprow.ename); --输出信息
         end loop; --结束循环
         close cl; --关闭游标
  end; --退出
--游标:可以存放多个对象,多行记录。
--输出emp表中的所有数据
declare
  cursor cl is select * from emp; --创建游标,游标的数据来自emp表
  emprow emp%rowtype; --用来保存一行数据
begin --开始
  open cl; --打开游标
       loop --开始循环
         fetch cl into emprow; --将遍历结果放到变量
         exit when cl%notfound; --没找到退出
         dbms_output.put_line(emprow.ename); --输出信息
         end loop; --结束循环
         close cl; --关闭游标
  end; --退出
  
--员工涨工资方法
declare
  cursor c2(eno emp.deptno%type) 
  is select empno from emp where deptno =eno;
  en emp.empno%type;
begin
  open c2(10);
  loop
    fetch c2 into en;
    exit when c2% notfound;
         update emp set sal=sal+100 where empno=en;
         commit;
    end loop; 
  close c2;
  end;
  
select * from emp where deptno=10;

九、存储过程、存储函数

9.1、存储过程

存储过程就是提交编译好的plsql放到数据库端可以直接被调用。

--存储过程
create or replace procedure pl (eno emp.empno%type)
is
begin
  update emp set sal=sal+1000 where empno=eno;
  commit;
  end;

declare
begin
 pl(7788);
  end;
--存储过程输出参数
create or replace procedure p_yearsal(eno emp.empno%type,yearsal out number)
is
       s number(10);
       c emp.comm%type;
begin
  select sal*12,nvl(comm,0) into s,c from emp where empno=eno;
  yearsal :=s+c;
end;

--测试
declare
  yearsal number(10);
begin
  p_yearsal(7788,yearsal);
  dbms_output.put_line(yearsal);
end;


--in 和out的区别
--涉及到into查询语句或者:=赋值的参数,都需要out来修饰

9.2、存储函数

--存储函数
--计算员工年薪
create or replace function f_years_sals (eno emp.empno%type) return number --返回类型为number
is
       s number(10); --定义变量用来接收返回结果
begin
  select sal*12+nvl(comm,0) into s from emp where empno=eno;
  return s;
end;

--测试
declare
  s number(10);
begin
  s :=f_years_sals(7788);
   dbms_output.put_line(s);
  end;

存储过程和存储函数的区别

语法区别:关键字不一样。

本质区别:存储函数有返回值(比存储过程多了两个返回值),存储过程没有返回值。

十、触发器

10.1、触发器的概念

当我们做增删改的时候如果需要做出什么操作,可以使用触发器。

10.2、触发器分类

语句级触发器:不包含for each row 的触发器。

行级触发器:包含or each row 的触发器,加触发器是为了old或者new对象。

--语句级触发器
create or replace trigger t1
after
insert
on person
declare
begin
   dbms_output.put_line('新员工入职');
  end;
  
insert into person values(4,'小黑');

--行级触发器
create or replace trigger t2
before
update
on emp
for each row
  declare
  
  begin
    if :old.sal>:new.sal then
      raise_application_error(-20001,'不能降薪');
    end if;
   end;

select * from emp where empno=7788;

update emp set sal=sal-1 where empno=7788;
commit;


--触发器实现主键自增
create or replace trigger auid
before
insert
on person
for each row
  declare
  
  begin
    select s_person.nextval into :new.pid from dual;
end;
    
select * from person;


insert into person(pname) values('星星')
Oracle 相关文章推荐
mybatis使用oracle进行添加数据的方法
Apr 27 Oracle
oracle通过存储过程上传list保存功能
May 12 Oracle
DBCA命令行搭建Oracle ADG的流程
Jun 11 Oracle
ORACLE查看当前账号的相关信息
Jun 18 Oracle
使用Oracle跟踪文件的问题详解
Jun 28 Oracle
C#连接ORACLE出现乱码问题的解决方法
Oct 05 Oracle
Oracle中update和select 关联操作
Jan 18 Oracle
分析SQL窗口函数之聚合窗口函数
Apr 21 Oracle
解决Oracle数据库用户密码过期
May 11 Oracle
Oracle删除归档日志及添加定时任务
Jun 28 Oracle
oracle设置密码复杂度及设置超时退出的功能
Jun 28 Oracle
Oracle中日期的使用方法实例
Jul 07 Oracle
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
Apr 06 #Oracle
ORACLE数据库对long类型字段进行模糊匹配的解决思路
Oracle 数据仓库ETL技术之多表插入语句的示例详解
oracle表分区的概念及操作
Apr 24 #Oracle
Oracle设置DB、监听和EM开机启动的方法
mybatis使用oracle进行添加数据的方法
Apr 27 #Oracle
使用springboot暴露oracle数据接口的问题
You might like
main.php
2006/12/09 PHP
PHP多个版本的分析解释
2011/07/21 PHP
Yii把CGridView文本框换成下拉框的方法
2014/12/03 PHP
新浪微博OAuth认证和储存的主要过程详解
2015/03/27 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
2015/04/21 PHP
PHP测试成功的邮件发送案例
2015/10/26 PHP
php用户名的密码加密更安全的方法
2019/06/21 PHP
Javascript的闭包
2009/12/31 Javascript
Js 刷新框架页的代码
2010/04/13 Javascript
JS控制输入框内字符串长度
2014/05/21 Javascript
JavaScript引用类型和基本类型详解
2016/01/06 Javascript
EasyUI在表单提交之前进行验证的实例代码
2016/06/24 Javascript
学习vue.js计算属性
2016/12/03 Javascript
AnglarJs中的上拉加载实现代码
2018/02/08 Javascript
nuxt框架中路由鉴权之Koa和Session的用法
2018/05/09 Javascript
微信小程序实现登录注册tab切换效果
2020/12/29 Javascript
[02:47]DOTA2英雄基础教程 野性怒吼兽王
2013/12/05 DOTA
用Python写一个无界面的2048小游戏
2016/05/24 Python
python3 pandas 读取MySQL数据和插入的实例
2018/04/20 Python
使用实现XlsxWriter创建Excel文件并编辑
2018/05/04 Python
python+splinter自动刷新抢票功能
2018/09/25 Python
Python简单过滤字母和数字的方法小结
2019/01/09 Python
Pandas实现dataframe和np.array的相互转换
2019/11/30 Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
2019/12/02 Python
python脚本后台执行方式
2019/12/21 Python
Python headers请求头如何实现快速添加
2020/11/03 Python
python用opencv 图像傅里叶变换
2021/01/04 Python
css3如何绘制一个圆圆的loading转圈动画
2018/01/09 HTML / CSS
字中字效果的实现【html5实例】
2016/05/03 HTML / CSS
自荐书封面下载
2013/11/29 职场文书
大学生自我鉴定范文
2013/12/28 职场文书
护理中职生求职信范文
2014/02/24 职场文书
安全教育月活动总结
2014/05/05 职场文书
音乐教师个人工作总结
2015/02/06 职场文书
Python爬虫入门案例之爬取去哪儿旅游景点攻略以及可视化分析
2021/10/16 Python
mysql 获取时间方式
2022/03/20 MySQL