完整问题如下:
创建两个表
表一、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时,提示不能更新此列,取消操作

解决方案 »

  1.   

    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 ;
      

  2.   

    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 ;
      

  3.   

    1,2可以的,if inserting then ...eleif deleting then ...
    3的话都不是同一个表,肯定不行;
    不过你的条件中有的可以用约束完成,这样速度还会快!
      

  4.   

    哎 今天有时间  问题终于解决了
    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;
      

  5.   

    支持,优化一条语句更好
    select count(*) into a from course where cno=:new.CNO ;