database - Auto increment an ID with a string prefix in oracle sql? -
i have created following table , wish have userid auto-increment whenever there row added database. id formatted 000001 example below, since there few tables , ideal give each id string prefix:
userid ---------- user000001 user000002 user000003 create table usertable ( userid varchar(20), username varchar(250) not null, firstname varchar(250) not null, lastname varchar(250) not null, constraint pkuserid primary key (userid), constraint uniusername unique (username) );
you have use combination of trigger , sequence shown in code below:
create sequence create sequence usertable_seq start 1 increment 1 nocache nocycle; / create or replace trigger usertable_trigger before insert on usertable each row begin select 'user' || to_char(usertable_seq.nextval, '000099') :new.userid dual; end; /
Comments
Post a Comment