It is possible to automatically generate a primary key for Oracle Database tables when they are created. The steps below will show how to configure database tables so that primary keys are automatically generated:

The following example would create an automatic primary key generation with the following table:

  1. Create Table with primary key and any additional attributes.
    create table test (id number, testdata varchar2(255));
  2. Create Sequence
    create sequence test_seq
    start with 1
    increment by 1
    nomaxvalue;
  3. Create Trigger
    create trigger test_trigger
    before insert on test
    for each row
    begin
    select test_seq.nextval into :new.id from dual;
    end;
    /
  4. Test
    insert into test values(‘hello world!’);
    commit;
    select * from test;