Create trigger for auto incerment id and default unix datetime

爱⌒轻易说出口 提交于 2020-01-06 08:18:27

问题


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:


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;


来源:https://stackoverflow.com/questions/13656520/create-trigger-for-auto-incerment-id-and-default-unix-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!