sql - How to write a trigger to move data I want to delete from a table to antother before deleting it -
i wrote trigger monitor table called employees , , when deleting employee table trigger should fire , , copy employee want delete table employees , put in table called deleted_employees , when put following sql statement delete employees employee_id = 100 sql error says :
error starting @ line 17 in command: delete employees employee_id = 100 error report: sql error: ora-02292: integrity constraint (hr.dept_mgr_fk) violated - child record found 02292. 00000 - "integrity constraint (%s.%s) violated - child record found" *cause: attempted delete parent key value had foreign dependency. *action: delete dependencies first parent or disable constraint. create or replace trigger employee_deleted before delete on employees declare cursor cur_emp select employee_id , first_name , last_name , email , phone_number , hire_date , job_id , salary , commission_pct , manager_id , department_id employees; emp_rec cur_emp%rowtype; begin open cur_emp; while(cur_emp%found) loop fetch cur_emp emp_rec; insert deleted_employees (employee_id , first_name , last_name , email , phone_number , hire_date ,job_id , salary , commission_pct , manager_id , department_id) values (emp_rec.employee_id ,emp_rec.first_name ,emp_rec.last_name , emp_rec.email , emp_rec.phone_number , emp_rec.hire_date , emp_rec.job_id , emp_rec.salary , emp_rec.commission_pct , emp_rec.manager_id , emp_rec.department_id); end loop; close cur_emp; end;
i don't know how test trigger , ideas ?!
one thing observed are, mistake, inserting records employee table deleted_employees.
i think want insert employee detail deleted_employee table in delete action.
for in oracle can using :old
keyword refer current running record. if want delete related chile records .. can in trigger without problem -
create or replace trigger employee_deleted before delete on employees each row declare begin insert deleted_employees ( employee_id , first_name , last_name , email , phone_number , hire_date ,job_id , salary , commission_pct , manager_id , department_id ) values (:old.employee_id ,:old.first_name ,:old.last_name , :old.email , :old.phone_number , :old.hire_date , :old.job_id , :old.salary , :old.commission_pct , :old.manager_id , :old.department_id); delete department employee_id = :old.employee_id; -- if deleting child record not ora:02292 error end;
just edit delete
part in trigger per database.
for need first check key constraint dept_mgr_fk
. guessed key present in department table on employee_id column.
so check key , change second last line , compile.
Comments
Post a Comment