0

I am trying to load a table where i have a PK column called E_id. I want to generate a unique ID for this column every time a row is loaded into this table at informatica level. But i want this e_id column to generate a unique value at table level. Can i achieve this by writing a query to this column at table create script itself? Currently i tried this method of defaulting the value with this

e_id NUMBER DEFAULT TO_NUMBER(TO_CHAR(SYSTIMESTAMP, 'YYYYMMDDHH24MISSFF9')) NOT NULL

Although at times i am getting the same value for two records. Can someone help how to go about this?

TIA.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
gbppa
  • 35
  • 1
  • 2
  • 10

3 Answers3

1

If you are using 12c the easiest way is to use an IDENTITY column. Oracle will automatically generate a unique value for the column whenever you insert a record.

In prior versions you can define a sequence and generate it via a trigger or reference it directly in the insert statement.

You mention Informatica. Its a long time since I used it but I seem to remember there being a simple way to generate a unique ID using Informatica too.

BriteSponge
  • 1,034
  • 9
  • 15
  • Yeah but the constraint here is i can not use informatica to load unique values for this columns else i would hav easily used the sequence generator transformation. Thats my constraint here. I need to find a way to populate this column at table script level. – gbppa Jan 17 '17 at 11:12
  • If it has to be purely at table script level then you will need to be on 12c, otherwise it sounds like the trigger is the way to go. – BriteSponge Jan 17 '17 at 12:17
1

one of the most common way to do this is using sequences

1 create sequence

2 before insert row trigger that will populate value into the pk column

create sequence GSEQUENCE
minvalue 1000
maxvalue 99999999999999999
start with 93581
increment by 1
cache 20;


CREATE OR REPLACE TRIGGER BI_DOCUMENTS
  BEFORE INSERT
  on DOCUMENTS

  for each row
declare numrows INTEGER;
begin


select gsequence.nextval
into :new.id_DOCUMENT
from dual;

end;
/
are
  • 2,535
  • 2
  • 22
  • 27
0

You can use Globally Unique ID (GUID) which is unique for all practice purpose. Lot of systems use GUID to generate unique id.

select sys_guid() from dual

Mote into here

While GUID gives truly random unique ids, you can always use Oracle sequence if you need sequential unique values.

Oracle doesn't have "auto increment" columns, but you can achive similar results by a trigger.

EDIT: You can refer this link too How to generate a GUID in Oracle?

Community
  • 1
  • 1
Vijayakumar Udupa
  • 1,115
  • 1
  • 6
  • 15