Search code examples
oracle-databaseuserid

Oracle : auto add the current user id in a new entry


I have a table to create and i need to get the current user id. I wonder if a solution like for the current date :

is_date_enr     date        default sysdate not null,

is possible.


Solution

  • You can use UID and USER pseudocolumns to do that:

    create table test (
      tst_date date default sysdate not null,
      tst_user varchar2(20) default user not null,
      tst_uid number default uid not null,
      field1 number,
      field2 varchar2(10));
    

    Test:

    insert into test (field1, field2) values (1, 'ABC');
    insert into test (field1, field2) values (2, 'DEF');
    insert into test (field1, field2) values (3, 'XYZ');
    
    select * from test;
    
    TST_DATE             TST_USER  TST_UID FIELD1    FIELD2
    -------------------  --------  -------  --------  --------
    2015-03-23 17:41:28  SCOTT         236         1  ABC
    2015-03-23 17:41:28  SCOTT         236         2  DEF
    2015-03-23 17:41:28  SCOTT         236         3  XYZ
    

    Columns TST_DATE, TST_USER and TST_UID were filled automatically.