通过触发器删除引用完整性约束

时间:2015-09-05 10:42:41

标签: oracle11g triggers

我有三张桌子

  1. 患者
  2. 依赖
  3. Patient_Visit
  4. 患者表的主键在dependent和patient_visit表中用作外键。我需要在患者台上触发,在删除之前检查子表中的patient_id。 至于现在我已经创建了一个触发器,但它没有工作。

    create or replace trigger deletePatient 
    before delete on patient 
    for each row 
    declare     
           v_count number(5);
           v_count1 number(5); 
    begin
         select count(patient_id)   
         into v_count   
         from dependent     
         where patient_id = :old.patient_id;
    
         if v_count > 0 then 
                     raise_application_error(-20444,'Cannot Delete record');    
         else select count(patient_id)      
              into v_count1         
              from patient_visit        
              where patient_id = :old.patient_id;
    
             if v_count1 > 0 then 
                       raise_application_error(-20434,'Cannot Delete record');                  
             else  dbms_output.put_line('Deletion Successful');         
             end if;    
         end if;    
    end; 
    /
    

0 个答案:

没有答案