Oracle---day03

2023-11-17

一、视图

  对一个查询结果的封装(视图里面所有的数据都是来自于它查询的那张表,视图里面不存储任何数据)

   视图的好处:1.能够封装复杂的查询结果
         2.屏蔽表中的细节

   语法:create [or replace] view 视图名字 as 查询语句 [with read only]
        or replace--如果存在就覆盖
        with read only--只允许读,不允许修改,一般都会加

  同义词--就是给表、视图等取另外一个名字

--创建一个视图
create or replace view view_test1 as select ename,job,deptno from emp;

--通过视图修改数据
update view_test1 set ename='SIMITH2' where ename = 'smith2';

--创建一个只读视图
create or replace view view_test2 as select ename,job,deptno from emp with read only;

--同义词概念
create synonym yuangong for view_test2;

select * from yuangong

select * from view_test1;
select * from emp;

二、序列

  序列:生成类似于auto_increament(mysql中的id自增长)的ID

   语法:create sequence 序列名称
      start with 起始值
      increament by 增长量
      maxvalue 最大值 | nomaxvalue
      minvalue  最小值 | nominvalue
      cycle  |  nocycle  是否在max和min之间循环
      cache 缓存数量 |  nocache
   用法:sequencename.currval  获得当前值(至少在使用一次nextval之后才能使用)
      sequencename.nextval  获得下一个值

   最常用的写法:careate sequence seq_test;

--创建一个1,3,5....30  切循环的序列
create sequence seq_test1
start with 1
increment by 2
maxvalue 30
cycle
cache 3;
--最常用的序列写法
create sequence seq_test2;   --默认从1开始,每次增长1,无最大值,不循环,不缓存--1,2,3,4,5,6....


select seq_test1.nextval from dual;
select seq_test1.currval from dual;

三、索引

  索引:相当于一本书的目录,能够提高我们的查询效率。如果某一列经常作为查询条件,就有必要创建索引(数据量大的情况下)

  语法:create index 索引名称 on 表名(列名)

  原理:btree balanceTree   平衡二叉树

  注意:主键约束自带主键索引,唯一约束自带唯一索引。如果某列作为查询条件的时候创建索引,查询会变快,但修改,插入的时候会变慢索引创建好之后,每过一段时间,DBA都回去重构索引

--五百万条数据测试
create table wubaiwu(
       name varchar(20),
       address varchar(20)
);

declare
begin
     for i in 1..5000000 loop
         insert into wubaiwu values('姓名'||i,'地址'||i);
     end loop;
     commit;
end;

--在没有索引的情况下查询姓名为 "姓名3000000" 的数据
select * from wubaiwu where name='姓名3000000';

--创建索引,再查询
create index idx_wubaiwu on wubaiwu(name);
select * from wubaiwu where name='姓名3000000';

select * from wubaiwu where name='姓名3000000';

四、PLSQL编程

  PLSQL编程:procedure Languege——过程语言,Oracle对SQL的一个扩展

   语法:declare  --声明变量
        变量名 变量类型;
        变量名 变量类型 := 初始值
        vsal emp.sal%type;  --引用表字段类型%%%%
        vrow emp%rowtype;    --引用表中一条记录的类型
      begin
        业务逻辑
      end;

declare
   i varchar(10) := '张三';
begin
   dbms_output.put_line(i);   --类型java中的System.out.print
end;

--查询7369的工资,并打印
declare
   vsal emp.sal%type;    --引用emp表中sal字段的类型
begin
   select sal into vsal from emp where empno=7369;  --将查询结果赋值给vsal
   dbms_output.put_line(vsal);
end;

--查询7369员工信息并打印
declare
   vrow emp%rowtype;   --引用emp表一条记录的类型
begin
   select * into vrow from emp where empno=7369;
   dbms_output.put_line('姓名:'||vrow.ename||'  工资:'||vrow.sal);
end;

  条件判断语句

/*
    条件判断语句:
    if ** then
    elsif ** then
    else
    end if
*/
declare 
   age number := &aa;
begin
   if age < 18 then
      dbms_output.put_line('未成年');
   elsif age >= 18 and age <= 24 then
      dbms_output.put_line('青年');
   elsif age > 24 and age <= 40 then
      dbms_output.put_line('中年');
   else
      dbms_output.put_line('老年');
   end if;
end;

  循环语句

/*
    循环语句
    for循环:
        for x in [reverse] 起始值..终止值 loop
           循环体
        end loop
    
    while循环:
        while 条件 loop
           循环体
        end loop
        
    loop循环:
       loop
          循环体
          exit when 条件     --满足条件时退出    
       end loop
*/

--for循环输出1-10
declare

begin
   for i in reverse 1..10 loop
       dbms_output.put_line('未成年'||i);
   end loop;
end;

--while循环输出1-10
declare
   val number := 1;
begin
   while val<=10 loop
      dbms_output.put_line(val);
      val := val+1;
   end loop;
end;

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

--输出菱形
declare

begin
   for i in 1..5 loop
      for j in 1..5 loop
         if(i+j>=4 and j<=i+2 and i+j<=8 and j>=i-2) then
            dbms_output.put('*');
         else
            dbms_output.put(' ');
         end if;
      end loop;
      dbms_output.put_line(' ');
   end loop;
end;


select sal from emp where empno=7369

五、游标

  游标:数据的缓冲区,从表中查询出结构集,指定访问一条记录,类似于数组。游标还可以用于抛出自定义异常

   语法:cursor 游标名 [(参数名, 参数类型)] is 查询结果集      --声明游标

   使用方式:1.声明游标
        2.打开游标  --open 游标名
        3.从游标中获取数据:fetch 游标名 into 变量
                  游标名%found --找到数据
                  游标名%nofound --没有找到数据
        4.关闭游标  --close 游标名

--输出所有员工姓名(不带参数游标)
declare
    cursor vrows is select * from emp;
    vrow emp%rowtype;
begin
    open vrows    --打开游标
    loop
        fetch vrows into vrow;
        exit when vrows%ontfound    --没有找到的时候退出
        dbms_output.put_line(vrow.ename || ' ---' || vrow.sal);
    end loop;
    close vrows;
end;

--输出指定部门下的员工姓名和工资(带参数的游标)
declare
    --声明游标
    cursor vrows(dno number) is select * from emp where deptno=dno;
    --声明变量记录每一行数据
    vrow emp%rowtype
begin
    open vrows(10)    --查询10号部门
    loop
        fetch vrows into row
        exit when vrows%notfound
        dbms_output.put_line('姓名:'||vrow.ename||'   工资:'||vrow.sal);
    end loop;
    close vrows;
end;

--for循环遍历游标,不需要打开关闭游标
declare
   cursor vrows is select * from emp;
begin
   for vrow in vrows loop
       dbms_output.put_line('姓名:'||vrow.ename||'   工资:'||vrow.sal);
   end loop;
end;
--涨工资,总裁涨1000,经理800,其他400
declare
   cursor vrows is select * from emp;
   vrow emp%rowtype;
begin
   open vrows;
   loop
       fetch vrows into vrow;
       exit when vrows%notfound;
       if vrow.job='PRESIDENT' then
           update emp set sal=sal+1000 where empno=vrow.empno;
       elsif vrow.job='MANAGER' then
           update emp set sal=sal+800 where empno=vrow.empno;
       else
           update emp set sal=sal+400 where empno=vrow.empno;
       end if;
   end loop;
   close vrows;
   commit;
end;


/*
    异常
*/
--捕获异常
declare
   val emp%rowtype;
begin
   select * into val from emp where empno=123;
     
exception
   when others then
       dbms_output.put_line('捕获到异常');
end;

--自定义异常
declare
   cursor vrows(dno number) is select * from emp where deptno=dno;
   val emp%rowtype;
   no_emp exception;
begin
   open vrows(123);
   fetch vrows into val;
   if vrows%notfound then   --判断是否有值
       raise no_emp;
   end if;
   close vrows;
exception
   when no_emp then
       dbms_output.put_line('捕获到自定义异常');
end;

 

六、存储过程和存储函数

  存储过程:实际上是封装在服务器上的一段PLSQL代码,已经编译好了的代码。客户端调用存储过程,执行效率就会非常高兴。

   语法:create [or replace] procedure 存储过程名称 (参数名 in|out 参数类型,参数名 in|out 参数类型)  --in 代表输入参数,out 代表输出参数。
      as | is
      --声明部分
      begin
      --业务逻辑
      end;

  存储函数:和存储过程差不多,只是多了一个返回值。但有了out参数,好像存储函数就没啥用了。

   语法:create [or replace] procedure 存储过程名称 (参数名 in|out 参数类型) return 返回值类型
      .....其他和储存过程一样

--创建一个存储过程,给指定员工涨工资,并输出涨工资前后的工资,,,输入参数in--vempno,vsal
create or replace procedure proc_updateSal(vempno in number, vsal in number)
is
    currentSal number;
begin
    select sal into currentSal from emp where empno=vempno;
    dbms_output.put_line('涨薪前的工资:'||currentSal);
    update emp set sal = sal + vsal where empno=vempno;   --更新工资
    dbms_output.put_line('涨薪前的工资:'||(currentSal+vsal));
    commit;
end;

--调用存储过程
--方式一
call proc_updateSal(7788, 10);

--方式二,也是最长用的方式
declare
begin
   proc_updateSal(7788,-100);
end;

--查询员工年薪
select sal*12+nvl(comm, 0) from emp where empno=7788;
create or replace procedure get_YearSal(vempno in number, yearSal out number)
is

begin
    select sal*12+nvl(comm, 0) into yearSal from emp where empno=vempno;
end;
--测试存储过程
declare 
   yearSal number;
begin
   get_yearSal(7788, yearSal);
   dbms_output.put_line(yearSal);
end;


--查询员工年薪---使用存储函数
create or replace function fun_YearSal(vempno in number) return number
is
   yearSal number;
begin
    select sal*12+nvl(comm, 0) into yearSal from emp where empno=vempno;
    return yearSal;
end;
--测试存储函数
declare 
   yearSal number;
begin
   yearSal:=fun_yearSal(7788);
   dbms_output.put_line(yearSal);
end;

七、触发器

  触发器:当用户执行了insert,update,delete等操作后,可以触发一系列其他动作

   语法:create [or replace] trigger 触发器名字
      before | after
      insert | update | delete
      on 表名
      [for each row]  --是否应用到每一行
      declare
      ...
      begin
      ...
      end;

   触发器分类:
        语句级触发器:  不管影响多少行,都只触发一次,也就是不加 for each row
        行级触发器:      影响了多少行就触发多少行。
   其他: :old  更新前的记录
         :new   更新或的记录

--新员工入职后,输出一句话:欢迎来到德莱联盟
create or replace trigger tri_test1
before
insert
on emp
declare
begin
     dbms_output.put_line('欢迎来到德莱联盟');
end;

--数据校验,周二老板不在,不能办理入职手续
create or replace trigger tri_test2
before
insert
on emp
declare
   vday varchar2(10);
begin
   select trim(to_char(sysdate,'day')) into vday from dual;
   if vday='星期二' then
      dbms_output.put_line('周二老板不在,不能办理入职手续');
      --抛出系统异常
      raise_application_error(-20001,'周二老板不在,不能办理入职手续');
   end if;
end;


insert into emp(empno,ename) values(1188,'德莱厄斯')

--更新所有员工的工资,并输出一句话
create or replace trigger tri_test3
after
update
on emp
for each row
declare

begin
   dbms_output.put_line('涨钱啦!');
end;


update emp set sal = sal+10;

--判断员工涨工资后的工资一定要大于更新后的工资
create or replace trigger tri_updateSal
before
update
on emp
for each row
declare

begin
   if :old.sal>:new.sal then
      raise_application_error(-20002,'这不是涨工资是扣工资');
   end if;
end;

update emp set sal = sal-1;

 模拟mysql中的id只增长属性  auto_increment

--创建测试表
create table person(
       pid number primary key,
       pname varchar2(20)
);

--创建自增长序列
create sequence seq_person_pid;

--创建触发器
create or replace trigger tri_add_pid
before
insert
on person
for each row
declare

begin
   dbms_output.put_line('嘻嘻'||:new.pname);
   select seq_person_pid.nextval into :new.pid from dual;
end;
;


insert into person values(null,'张三');

select seq_person_pid.nextval from dual;

select * from person;

select * from emp;

转载于:https://www.cnblogs.com/zy-Luo/p/11552849.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Oracle---day03 的相关文章

随机推荐

  • 基于Pytorch的神经网络部分自定义设计

    一 基础概念 学习笔记 1 训练误差和泛化误差 1 本质上 优化和深度学习的目标是根本不同的 前者主要关注的是最小化目标 后者则关注在给定有限数据量的情况下寻找合适的模型 训练误差和泛化误差通常不同 由于优化算法的目标函数通常是基于训练数据
  • 世界上最难的视觉图_世界上最长的蛇有多长?四川惊现55米洪荒巨蟒(图)

    蛇 一直是站在食物链顶端的致命生物 蟒蛇更是能够吞食人类的恐怖巨兽 网传世界上最长的蛇有500米之长 名为 红海巨蛇 已被证实为虚假传言 以地球现在的环境是不可能出现如此之大的蛇的 据说四川发现了罕见的55米长的巨蟒 堪称世界上最长的蛇 是
  • QT QWebView/QWebEngineView使用

    一 简介 使用QT程序可以访问web页面 但在不同QT版本中使用的类和方法不同 1 Qt4中使用webkit模块 2 Qt5 Qt5 5使用webkitwidgets模块 3 Qt5 6以上版本使用webenginewidgets模块 所以
  • 【Jmeter】生成html格式接口自动化测试报告

    jmeter自带执行结果查看的插件 但是需要在jmeter工具中才能查看 如果要向领导提交测试结果 不够方便直观 笔者刚做了这方面的尝试 总结出来分享给大家 这里需要用到ant来执行测试用例并生成HTML格式测试报告 一 ant下载安装 1
  • Error failed to bundle project: error running light.exe报错解决方法

    Error failed to bundle project error running light exe error running light exe failed to run C Users admin AppData Local
  • 利用VNC远程连接Ubuntu server(图形界面)

    有时会需要远程连上ubuntu server图形界面控制调试 这时VNC就是一个挺好的工具 windows 上安装 vnc viewer ubuntu server 上安装vnc server 进行连接即可 一 ubuntu 安装vnc s
  • 如何在CMD下运用管理员权限

    方法一 鼠标右键 这个方法比较比较普通 点开开始找到cmd 右击鼠标 以管理员身份运行 A 这样调用就是管理员的权限 方法二 快捷模式 在点开win R后 选择 以管理员身份运行 然后确定 可以这还是会调出UAC的提示界面 选择 是 即可
  • 【动态系统的建模与分析】二阶系统的单位阶跃响应-笔记

    1 数学推导 上节说到 定义系统输入输出 方程变为 考虑 从传递函数入手分析 求解它的传递函数 即laplace两边 即 得 传递函数为 框图为 极点 所以 则 因为B和C共轭 因此 Laplace逆变换 得 其中 则 其他情况可以自行分析
  • Ubuntu16.04主机安装基于ZYNQ的QT交叉编译库

    Ubuntu16 04主机尝试安装基于ZYNQ的QT交叉编译库 前言 基于vivado2016 4 SDK的交叉编译环境 主机Linux上安装vivado2016 4版本 qtcreator上添加基于ZYNQ的交叉编译工具 基本环境 参考资
  • 网络IO并发的底层分析

    一 IO 1 IO 类型 大部分的socket接口都是阻塞型的 所谓阻塞型接口是指系统调用 一般是IO接口 不返回调用结果并让当前线程一直阻塞 只有当该系统调用获得结果或者超时出错时才返回 select系统调用是用来让我们的程序监视多个文件
  • java集合框架中contains(),containsKey()和containsValue()的用法:

    java集合框架中contains containsKey 和containsValue 的用法 List集合的contains 方法用于判断集合中包不包含某个元素 返回值是boolean Map集合的containsKey 和contai
  • python如何输出两行_python pandas dataframe拆分行(某些行一行拆成多行)

    简单循环 最简单的方式就是循环拆分一下呗 先上最简单方法 import pandas as pd df pd DataFrame A 1 2 3 B 1 2 3 4 5 6 C 3 3 3 result pd DataFrame colum
  • 使用Matlab生成随机数数组的方法

    使用Matlab生成随机数数组的方法 在Matlab中 我们可以使用rand randn和randi函数来生成随机数数组 下面 我们将分别介绍这些函数的使用方法 rand函数 rand函数用于生成一个由0到1之间的随机数构成的矩阵 可以指定
  • Mybatis 延迟加载使用

    在一对一映射中使用 首先配置延迟加载 需要在mybatis xml中配置
  • Ubuntu下安装GMP

    记得安装m4 GMP下载地址 https gmplib org 1 进入官网 下载最新安装包 2 解压得到gmp xxx 3 进入文件夹 cd gmp 6 2 1 4 安装m4 如果没有这步 第六步将报错 sudo apt get inst
  • 【开发工具】【Dropbear】Dropbear编译安装

    简介 dropbear 小型的嵌入式系统专用的SSH服务端和客户端工具 服务器端 dropbear dropbearkey 客户端 dbclient dropbear默认使用nsswitch实现名称解析 账户名称 dropbear会在用户登
  • Flutter倒计时定时器

    Timer void countdown Timer timer new Timer new Duration seconds 10 只在倒计时结束时回调
  • 二叉树各种函数的实现

    如果你觉得迷茫 那就尽可能选择比较困难的路 目录 前言 一 通过前序遍历创建二叉树 二 二叉树的四种遍历 1 二叉树的前序遍历 2 二叉树的中序遍历 3 二叉树的后序遍历 4 二叉树的层序遍历 三 二叉树的结点个数 四 二叉树的叶子结点的个
  • Linux查看当前目录及子目录大小

    du h max depth 1 var lib docker 查看 var lib docker 目录及该目录直接子目录各文件及文件夹大小 查看效果如下图 改变 max depth 1 中的数值大小就可以指定查看该目录下多少级子目录 例如
  • Oracle---day03

    一 视图 对一个查询结果的封装 视图里面所有的数据都是来自于它查询的那张表 视图里面不存储任何数据 视图的好处 1 能够封装复杂的查询结果 2 屏蔽表中的细节 语法 create or replace view 视图名字 as 查询语句 w