oracle存储过程与触发器实验总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzq__janeGreen_/article/details/72630320

(1) 在JXGL 数据库中创建一存储过程并运行,从S 表中查询所有女生的信息。

1、创建存储过程

create or replace procedure prFemale(
pSno out S_RZ0119.Sno%type,
pSname out S_RZ0119.Sname%type,
pSage out S_RZ0119.Sage%type,
pSsex out S_RZ0119.Ssex%type,
pSdept out S_RZ0119.Sdept%type,
out_var out sys_refcursor
)
is
begin
open out_var for select Sno,Sname,Sage,Ssex,Sdept into pSno,pSname,pSage,pSsex,pSdept
from S_RZ0119
where Ssex=‘女‘;
end;
/

2、执行存储过程

var abc refcursor
declare
pSno varchar2(11);
pSname varchar2(20);
pSage number(2);
pSsex varchar2(9);
pSdept varchar2(20);
begin
prFemale(pSno,pSname,pSage,pSsex,pSdept,:abc);
end;
/

//打印

print :abc;

(2) 在JXGL 数据库中创建一存储过程并运行,从S 表中根据学号查询并返回该学
生的姓名和年龄。

1、创建根据学号查询学生姓名和年龄的过程

create or replace procedure searchS(
pSno in S_RZ0119.Sno%type,
pSname out S_RZ0119.Sname%type,
pSage out S_RZ0119.Sage%type
)
is
begin
select Sname,Sage into pSname,pSage 
from S_RZ0119
where Sno=pSno;
end;
/

2、执行存储过程

declare
pSname varchar2(20);
pSage number(20);
begin
searchS(‘131102‘,pSname,pSage);
dbms_output.put_line(pSname);
dbms_output.put_line(pSage);
end;
/

(3) 在JXGL 数据库中创建一触发器,保证S 表中学生的年龄在8-45 岁之间。

1、创建年龄触发器

create or replace trigger tri_SageWrong
before insert
on S_RZ0119
for each row
begin
if((:new.Sage<8) or (:new.Sage>45)) then
RAISE_APPLICATION_ERROR(-20600,‘学生表中的学生年龄必须在8-45之间!‘);
end if;
end;
/

2、测试

insert into S_RZ0119 values (‘130105‘,‘李得胜‘,‘男‘,6,‘软件工程系‘);

(4) 在JXGL 数据库中创建一触发器,当有学生选了某门课时,不能从C 表中删除
该课程。

1、创建课程触发器

create or replace trigger tri_CourseDelet
before delete
on C_RZ0119
for each row
declare CCount number;
begin
select count(*) into CCount from SC_RZ0119 where Cno=:old.Cno;
if CCount>0 then
RAISE_APPLICATION_ERROR(-20601,‘该课程已被学生选定,不能删除!‘);
end if;
end;
/

2、测试

delete from C_RZ0119 where Cno=‘C001‘;

猜你喜欢

转载自blog.csdn.net/wzq__janeGreen_/article/details/72630320