0

Any one help me to create a trigger for auto increment fld_id and Unix datetime. My table field is fld_id(int),fld_date(number),fld_value(varchar2).

My insert query is

insert into table (fld_value)values('xxx');
insert into table (fld_value)values('yyy');

I need the table record like this

fld_id    fld_date     fld_value
1         1354357476    xxx
2         1354357478    yyy

Please help me to create this.I can't able to do this..

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
user1804985
  • 297
  • 1
  • 7
  • 18
  • 1
    check this out: [auto increment insert trigger in oracle](http://stackoverflow.com/questions/317001/autoincrement-in-oracle) – bonCodigo Dec 01 '12 at 05:05

1 Answers1

1

If you want fld_id to be a sequential value, you'll need to create a sequence and a trigger

CREATE SEQUENCE sequence_name
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

CREATE OR REPLACE TRIGGER trigger_name
  BEFORE INSERT ON table_name
  FOR EACH ROW
BEGIN
  SELECT sequence_name.nextval
    INTO :new.fld_id
    FROM dual;
  :new.fld_date := (SYSDATE - date '1970-01-01') * 60 * 60 * 24;
END;
Justin Cave
  • 227,342
  • 24
  • 367
  • 384