触发器问题 向各位求助!

chekey 2010-05-09 06:27:18
完整问题如下:
创建两个表
表一、Grade( gid number primary key,
stuid number,--学号
cno number)—课程号

表二、course(CNO number,--课程号
cname varchar2(20))--课程名

问题:
为grade表创建触发器S_insert,当向grade表中插入数据时,要求学号必须以“97”开头,且课程号CNO必须在COURSE表中,否则取消插入操作。
为grade表创建delete触发器,当删除记录大于1行时取消此操作,并且抛出程序异常提示错误
为course表创建update触发器,当用户试图更新cno时,提示不能更新此列,取消操作
...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangwonderful 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 adebayor 的回复:]

create or replace trigger S_insert
before insert on grade
for each row
declare
a number;
begin
if substr(:new.id,1,2) != '97' then
raise no_data_found;
elsif
select count(*)……
[/Quote]
支持,优化一条语句更好
select count(*) into a from course where cno=:new.CNO ;
Adebayor 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 adebayor 的回复:]

引用楼主 chekey 的回复:
为grade表创建delete触发器,当删除记录大于1行时取消此操作,并且抛出程序异常提示错误

这个有难度 一时还没想出来 似乎要创建临时表了
[/Quote]
哎 今天有时间 问题终于解决了
create global temporary table temp_grade
(
gid number,
stuid number,
cno number
)on commit delete rows;


create or replace trigger tri_grade
after delete on grade
for each row
declare
a number;
e exception;
begin
dbms_output.put_line(:old.gid || :old.stuid || :old.cno);
insert into temp_grade values(:old.gid,:old.stuid,:old.cno);
select count(*) into a from temp_grade;
if a > 1 then
raise e;
end if;
exception
when e then
raise_application_error(-20001,'删除记录大于1行');
end;

xuelianlee 2010-05-10
  • 打赏
  • 举报
回复
1,2可以的,if inserting then ...eleif deleting then ...
3的话都不是同一个表,肯定不行;
不过你的条件中有的可以用约束完成,这样速度还会快!
luoyoumou 2010-05-09
  • 打赏
  • 举报
回复
-- 三个功能: 整成一个触发器呢?
Adebayor 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 chekey 的回复:]
为grade表创建delete触发器,当删除记录大于1行时取消此操作,并且抛出程序异常提示错误
[/Quote]
这个有难度 一时还没想出来 似乎要创建临时表了
Adebayor 2010-05-09
  • 打赏
  • 举报
回复
create or replace trigger S_update
before update on course
for each row
declare
e exception;
a number;
begin
if :new.cno != :old.cno then
raise e;
end if;
exception
when e then
raise_application_error(-20001,'不能更新此列');
end ;
Adebayor 2010-05-09
  • 打赏
  • 举报
回复
create or replace trigger S_insert
before insert on grade
for each row
declare
a number;
begin
if substr(:new.id,1,2) != '97' then
raise no_data_found;
elsif
select count(*) into a from course where :new.CNO in (select cno from course);
if a = 0 then
raise no_data_found;
end if;
end if;
end ;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧