Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an automatic modification time stamp type for Oracle columns?

Tags:

Is there a way to create a timestamp column in Oracle that automatically stores a timestamp of when the record has changed ?

like image 540
Ayrad Avatar asked Oct 23 '09 15:10

Ayrad


People also ask

Does Oracle have TIMESTAMP data type?

Introduction to Oracle TIMESTAMP data typeThe TIMESTAMP data type allows you to store date and time data including year, month, day, hour, minute and second. In addition, it stores the fractional seconds, which is not stored by the DATE data type.

What is the default TIMESTAMP format in Oracle?

Oracle introduced TIMESTAMP data type in 9i version. It allows to store date time with fraction of seconds. By default format of TIMESTAMP is 'YYYY-MM-DD HH24:MI:SS. FF'.

How do I add a TIMESTAMP to a column in Oracle?

Insert the same date and time as a TIMESTAMP literal. SQL> INSERT INTO table_ts VALUES(2, TIMESTAMP '2003-01-01 2:00:00'); Insert the same date and time as a TIMESTAMP WITH TIME ZONE literal. Oracle converts it to a TIMESTAMP value, which means that the time zone information is dropped.

How can I change the date format of a column in Oracle?

Finally, you can change the default DATE format of Oracle from "DD-MON-YY" to something you like by issuing the following command in sqlplus: alter session set NLS_DATE_FORMAT='<my_format>'; The change is only valid for the current sqlplus session.


1 Answers

Pretty sure you have to do this with a trigger in Oracle:

create or replace TRIGGER parkedorder_tbiur    BEFORE INSERT OR UPDATE    ON parkedorder    REFERENCING OLD AS old_row NEW AS new_row    FOR EACH ROW BEGIN    IF INSERTING    THEN       IF :new_row.ID IS NULL       THEN          SELECT parkedorder_seq.NEXTVAL            INTO :new_row.ID            FROM DUAL;       END IF;    END IF;     IF    :new_row.lastupdated <> SYSDATE       OR :new_row.lastupdated IS NULL    THEN       SELECT sysdate         INTO :new_row.lastupdated         FROM DUAL;    END IF;     SELECT SYS_CONTEXT ( 'USERENV', 'OS_USER' )      INTO :new_row.lastupdatedby      FROM DUAL; END; 
like image 93
dkackman Avatar answered Sep 27 '22 19:09

dkackman