【Oracle】PL/SQL&存储过程&触发器

一、实验目的

1. 掌握PL/SQL语句操作;

2. 掌握存储过程的创建和使用;

3. 掌握触发器的创建和使用。


二、实验任务

1. 创建PL/SQL语句完成相应需求;

2.创建并调用存储过程完成相应任务;

3. 创建触发器并测试完成相应任务。


三、实验内容与要求

3.1 创建PL/SQL语句完成相应需求

写一段pl/sql程序,为部门号为10的员工涨20%工资

declare
cursor addSal is select * from emp where deptno=10;
pemp emp%rowtype;
begin
  open addSal;
  loop
    fetch addSal into pemp;
    exit when addSal%notfound;
    update emp set sal = sal +0.1*sal where empno = pemp.empno;
    commit;
  end loop;
  close addSal;
end;

写一段pl/sql程序,使用游标输出emp中员工信息

declare
cursor emplist is select * from emp;
pemp emp%rowtype;
begin
  open emplist;
  loop
    fetch emplist into pemp;
    exit when emplist%notfound;
    dbms_output.put_line(pemp.empno||'   '||pemp.ename||'   '||pemp.job||'   '||pemp.mgr||'   '||pemp.hiredate||'   '||pemp.sal||'   '||pemp.comm||'   '||pemp.deptno);
  end loop;
  close emplist;
end;

3.2 创建并调用存储过程完成相应任务

通过存储过程,向dept表中新增数据

create or replace procedure zyt_02 is
begin
  insert into dept(deptno,dname,loc) values('50','SALES','china');
  insert into dept(deptno,dname,loc) values('60','ACCOUNTING','china');
end;

3.3 创建触发器并测试完成相应任务

emp表中插入员工后打印一句话“一个新员工插入成功”

create or replace trigger t2 after insert on emp
declare
begin
  dbms_output.put_line('一个新员工插入成功');
end;

通过触发器实现某一表中主键自增长功能

create table student(
  studentid varchar2(4) primary key,
  name varchar2(20)
  );//创建表
create sequence student_seq start with 1 increment by 1;//创建序列
 create or replace trigger student_trigger before insert on student for each row
 begin
   select student_seq.nextval into :new.studentid from dual;
   end;
  /  //创建触发器

insert into student(name) values('zyt');
insert into student(name) values('zytt');
insert into student(name) values('zyttt');//插入数据


四、实验总结

通过本次实验,我进行了PL/SQL语句的相关基础操作的学习和使用,进一步了解了PL/SQL语句,并且学会了调用存储过程还有触发器的使用,在最后一题实现自增长时出现问题,在通过搜索资料之后解决了这个问题。

猜你喜欢

转载自blog.csdn.net/Luoxiaobaia/article/details/125158278
今日推荐