G-7730: If using triggers, use compound triggers

Minor

Efficiency, Maintainability

Reason

A single trigger is better than several

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
create or replace trigger dept_i_trg
before insert
on dept
for each row
begin
  :new.id = dept_seq.nextval;
  :new.created_on := sysdate;
  :new.created_by := sys_context('userenv','session_user');
end;
/
create or replace trigger dept_u_trg
before update
on dept
for each row
begin
  :new.updated_on := sysdate;
  :new.updated_by := sys_context('userenv','session_user');
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
create or replace trigger dept_ui_trg
before insert or update
on dept
for each row
begin
  if inserting then
    :new.id = dept_seq.nextval;
    :new.created_on := sysdate;
    :new.created_by := sys_context('userenv','session_user');
  elsif updating then
    :new.updated_on := sysdate;
    :new.updated_by := sys_context('userenv','session_user');
  end if;
end;
/