Search code examples
sqloracleaudit

Column default value via query execution


I have an audit table which keeps track of user updating the data. There are 2 ways of updating the data, first via GUI where logged in user will be updated in audit table. Second is by executing sql queries, now in this case i want to populate logged in DB user in Audit table. I want to achieve this by setting default value for audit user column by executing the query Select user from dual

Is it possible in oracle to set default value of a column to result of a query output?


Solution

  • No need for a query, just use the keyword user

    create table audit_table
    (
      ... other columns ....
      changed_when timestamp   default current_timestamp not null, 
      changed_by   varchar(30) default user              not null
    );