Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Trigger Updating a field on an Insert or an Update

For some reason I'm running a blank on how to go about doing something like this.

I have a table that looks like this:

UserID   |  Name   |  DateAdded   |   LastUpated
--------------------------------------------------
1        | James Q | 1/1/2009     |

If I insert or update record the lastupdated field should be updated the sysdate. How would I go about doing something like this?

like image 760
AlteredConcept Avatar asked Dec 02 '09 15:12

AlteredConcept


People also ask

Does triggers are executed once the action of an insert update or DELETE statement is done?

While a procedure is explicitly executed by a user, application, or trigger, one or more triggers are implicitly fired (executed) by Oracle when a triggering INSERT, UPDATE, or DELETE statement is issued, no matter which user is connected or which application is being used.

Can we use update in trigger?

The AFTER UPDATE trigger in MySQL is invoked automatically whenever an UPDATE event is fired on the table associated with the triggers. In this article, we are going to learn how to create an AFTER UPDATE trigger with its syntax and example.

What does insert or update or DELETE clauses do in trigger syntax?

A trigger fired by an INSERT statement has meaningful access to new column values only. Because the row is being created by the INSERT , the old values are null. A trigger fired by an UPDATE statement has access to both old and new column values for both BEFORE and AFTER row triggers.


1 Answers

CREATE OR REPLACE TRIGGER your_trigger_name
BEFORE INSERT OR UPDATE
ON your_table
FOR EACH ROW
DECLARE
BEGIN
   :new.LastUpdated := sysdate;
END;

Try that. I did not have a oracle server at hand, but I hope I got the syntax right.

like image 109
Klaus Byskov Pedersen Avatar answered Oct 05 '22 00:10

Klaus Byskov Pedersen