Create trigger for auto incerment id and default u

2019-08-28 05:05发布

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..

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-28 06:04

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;
查看更多
登录 后发表回答