oracle11g - Before update trigger with referential integrity in oracle 11g -


i want understand before update in trigger means.

i have table called dept_mst dept_id primary key. has 2 rows dept_id 1 , 2.

another table emp has columns emp_id primary key , emp_dept_id foreign key referencing dept_id of dept table.

now if add before update trigger on emp tables emp_dept_id column check if new value emp_dept_id present in master table dept if insert new row new dept_id dept table.

now if update emp_dept_id 3 emp_dept_id 2 in emp table giving integrity constraint violation error parent not found.

so,

  1. does mean oracle checks integrity constraints first , calls "before update" trigger?
  2. then how can bypass check , call before update trigger?
  3. what "before update" mean here?
  4. how can achieve above result using triggers , not using explicit pl sql block?

thank you

non-deferred foreign key constraints evaluated before triggers called, yes.

if can declare foreign key constraint deferrable (which require dropping , re-creating if existing constraint not deferrable)

alter table emp   add constraint fk_emp_dept (emp_dept_id) references dept( dept_id )   deferred deferrable; 

in application, can set constraint deferrable, run insert statement causing trigger fire , insert parent row. foreign key constraint validated when transaction commits.

an alternative defining constraint deferrable rename emp table to, say, emp_base, create view named emp , create instead of insert trigger on emp implements logic of first inserting dept , inserting emp_base.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -