Search code examples
oracle-databaseoracle12cddl

On insert or update i want to update UPDATED_AT column with current timestamp


I'm getting this error missing right parenthesis near on update query in oracledb

Working fine with Mysql :

ALTER TABLE EMPLOYEE
    ADD COLUMN `updated_at` `updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CurrENT_TIMESTAMP();

Not working with Oracle:

ALTER TABLE EMPLOYEE
ADD (
  UPDATED_AT DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Solution

  • There is no data type of datetime in Oracle. There is a date which always has the day and the time to the second. Or timestamp which includes fractional seconds. Or timestamp with [local] time zone that includes a time zone. Since you are using current_timestamp, I'll guess that you intended the data type to be timestamp with time zone.

    There is also no on update property for a column. If you want the column to be updated whenever the row is updated, you'd need to write a trigger for that.

    ALTER TABLE EMPLOYEE
    ADD (
      UPDATED_AT TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
    );
    
    create trigger set_updated_at
      before update on employee
      for each row
    begin
      :new.updated_at := current_timestamp();
    end;