Search code examples
oracleplsqloracle11gdatabase-trigger

how do I create a condition in oracledb?


How do i create a condition on oracle db? I'm new on this db. I already create table called vehicle_parked, but i want to trigger vehicle number as NULL if parkedOnSite value is 'F' and the structure like this

vehicle_parked

 - parked_id number(4) PK
 - arrivalTime date
 - parkedOnSite varchar(1) // value will be T/F
 - vehicle_number varchar(8)

Thanks.


Solution

  • you can easily manipulate your data by creating a DML trigger that fires before your data is inserted into table, and changes data.

    CREATE OR REPLACE TRIGGER trg_vehicle_parked
    BEFORE INSERT OR UPDATE
       ON vehicle_parked
       FOR EACH ROW    
    DECLARE
    
    BEGIN
      if :new.parkedOnSite = 'F' then
       :new.vehicle_number := null;
      end if;
    END;
    /
    

    In triggers, besides :new, we can use :old pseudo codes. They stand for the values of the columns before(:old) or after(:new) DML statements issued. Especially for an update or delete trigger you may compare your column's old and new values in a trigger as in the example :

    if ( nvl(:old.vehicle_number,0) != nvl(:new.vehicle_number,0) ) then
      go_on_with_statement ....