11g approach
From Oracle 11g and above, you can do this in one step using VIRTUAL columns .
Test case
SQL> CREATE TABLE tab_default ( 2 ID NUMBER, 3 flag varchar2(1) GENERATED ALWAYS AS (decode(id, 3, 'Y', 'N')) VIRTUAL 4 ); Table created. SQL> SQL> INSERT INTO tab_default (ID) VALUES (1); 1 row created. SQL> INSERT INTO tab_default (ID) VALUES (3); 1 row created. SQL> INSERT INTO tab_default (ID) VALUES (10); 1 row created. SQL> SELECT * FROM tab_default; ID F ---------- - 1 N 3 Y 10 N SQL>
So, the DECODE function in the VIRTUAL column declaration handles the requirement for you.
10g approach
You can fulfill the requirement using -
DEFAULT ValueAFTER INSERT TRIGGER whenever id = 3
Create a table to have a DEFAULT value of "N". Let the trigger only start when a new row is inserted with the value in id column = 3, so that the trigger updates the value to 'Y'. Else for all other cases, the default value will be "N".
source share