Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note:</strong> This answer is similar to the other two, but I'll post it anyway because it defines the trigger differently in a way that may be useful. <em>Please note that I've up-voted the other two answers as they'll do the job nicely.</em></p> <p>First, if you're only interested in changes to the <code>DEPARTMENT_ID</code> column, you can specify that in your trigger definition:</p> <pre><code>CREATE OR REPLACE TRIGGER employee_dept_trigger BEFORE INSERT OR UPDATE OF department_id ON employee FOR EACH ROW .... </code></pre> <p>A trigger can use the "automatic" logical values <code>INSERTING</code> and <code>UPDATING</code> to determine if an insert or an update called it. You can use the <code>INSERTING</code> value to indicate that this is a new employee:</p> <pre><code>IF INSERTING THEN -- set old department name = 'N/A' ELSE -- set old department name = whatever END IF; </code></pre> <p>Your question doesn't describe where the department <em>name</em> comes from, so this example descends into pseudocode when it comes to the department names:</p> <pre><code>CREATE OR REPLACE TRIGGER employee_dept_trigger BEFORE INSERT OR UPDATE OF department_id ON employee FOR EACH ROW BEGIN IF INSERTING THEN -- New employee INSERT INTO employee_dept_history VALUES ( :NEW.employee_id, :NEW.employee_name, 'N/A', (SELECT department_name from ...), &lt;-- based on :NEW.department_id SYSDATE); ELSE -- Existing employee INSERT INTO employee_dept_history VALUES ( :NEW.employee_id, :NEW.employee_name, (SELECT department_name from ...), &lt;-- based on :OLD.department_id (SELECT department_name from ...), &lt;-- based on :NEW.department_id SYSDATE); END IF; END employee_dept_trigger; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload