首页 > 其他 > 详细

索引,视图,存储过程和存储函数

时间:2020-03-15 00:33:55      阅读:83      评论:0      收藏:0      [点我收藏+]
select * from emp;
-- 1.自动创建索引:Oracle 会自动为主键和唯一键创建索引
--- 自动创建的索引无法手动删除,只有在删除主键或唯一键时,对应的索引一并删除
alter table emp 
      add constraint enam_uiq unique (ename);
      
alter table emp
      drop constraint enam_uiq
      
-- 2.手动创建索引:对于查询条件中经常使用到的查询字段可以添加索引
create index index_name on emp(ename) -- index_name:索引名称,emp:表名, ename:索引字段
--删除索引:只能删除手动添加的索引
drop index index_name

-- ===========================视图=========================================
--视图,就是一个虚表,可以用这个表查询数据
--视图,就是一个命名的查询语句。可以对表字段数据分权
-- 视图主要是用来做查询的,不能做DML操作,对视图的DML操作会影响原表数据
-- 1.创建一个名为hr_emp的视图
create or replace view hr_emp
as
select * from emp;

-- 2.创建项目mgr_emp
create or replace view mgr_emp
as
select empno,ename,job,mgr,deptno
from emp

-- s删除视图,不会影响原来表数据
drop view mgr_emp;

存储函数:

create or replace function get_sal(dpno number)
 return number
 is
   v_sumSal number(10):=0;
   Cursor sal_cursor is select sal from emp where deptno=dpno;
  begin
    for c in sal_cursor loop
      v_sumSal:=v_sumSal+c.sal;
    end loop;
    return v_sumSal;
  end;

 

 

存储过程:

/**
  对给定部门(作为参数)的员工进行加薪,在职时间为(?,95)期间,加薪 5%
                                                   (95,98)期间,加薪 3%
                                                   (98,?)期间,加薪 1%           
 得到以下返回结果:此次加薪,公司需要额外付出的成本 ,定义一个out类型的输出变量
*/

create or replace procedure add_sal(dept_id number,tem_sal out number)
is
       v_i number(4,2):=0;
       cursor sal_cursor is select empno,sal,hiredate  from emp where deptno = dept_id;
begin
    tem_sal:=0;
    for c in sal_cursor loop
         if to_char(c.hiredate,yyyy) <1995  then v_i:=0.05;
         elsif to_char(c.hiredate,yyyy)<1998 then v_i:=0.03;
         else v_i:=0.01;
         end if;
           --1.更新工资
         update emp set sal = sal*(1+v_i) where empno = c.empno;
           --2.付出成本
         tem_sal:=tem_sal + v_i * c.sal;
    end loop;
    dbms_output.put_line(tem_sal);
end;

调用以上的存储过程,在plsql命名中输入ed,
技术分享图片

 

 进入Text editor界面:

技术分享图片

在sql命令中输入 /,得到存储过程结果

技术分享图片

索引,视图,存储过程和存储函数

原文:https://www.cnblogs.com/wsy0202/p/12495156.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!