触发检查外键在PLSQL中不起作用

时间:2016-11-29 07:36:11

标签: sql database oracle plsql

以下是我正在使用的tables

create table EMPLOYEE (
                    Eid number(5) primary key,
                    Ename char(30),
                    Designation char(30));

create table Attendance (
                        Eid number(5) references EMPLOYEE(Eid),
                        tdate date,
                        attendance char(2) check(attendance in('P','A')));

以下是我用来参加的Procedure

create or replace PROCEDURE take_attendance(
                                            id IN number,
                                            attendance IN char) IS
begin 
    insert into ATTENDANCE Values(id, sysdate, attendance);
end;

以下是我正在使用的Trigger

create or replace TRIGGER to_take_attendance
before insert or update of eid on ATTENDANCE
for each row
declare
    CURSOR c(n number) is select * from EMPLOYEE where eid = :new.eid;
    e c%rowtype;
    invalid_id EXCEPTION;
begin
open c(:new.eid);
    fetch c into e;
    if c%NOTFOUND then
        raise invalid_id;
    else
        NULL;
    end if;

EXCEPTION   
when invalid_id then
    close c;
    raise_application_error(-20001,'Invalid ID');

close c;
end;

以下是Errors我正在

begin
*
ERROR at line 1: 
ORA-20001: Invalid ID 
ORA-06512: at "101503028.TO_TAKE_ATTENDANCE", line 17 
ORA-04088: error during execution of trigger '101503028.TO_TAKE_ATTENDANCE' 
ORA-06512: at "101503028.TAKE_ATTENDANCE", line 5 
ORA-06512: at line 2

我不确定为什么我会收到这些错误。我是PL / SQL的新手是我的问题,我非常愚蠢。

谢谢!

编辑 - 删除了其他例外。

1 个答案:

答案 0 :(得分:2)

您收到错误是因为您的触发按预期工作 您正在尝试插入EMPLOYEE中不存在的ATTENDANCE ID。

您的代码中定义的异常:
raise_application_error( -20001 ,' 无效ID ');

收到错误:
ORA- 20001 ID无效