Alexander Lomov
Alexander Lomov

Reputation: 119

MySQL triggers data copy

I have a trigger which looks like this:

delimiter //
 create trigger tracker after insert on Items for each row
      begin

      end; //
delimiter ;

I need to insert id, type, and date_created of the inserted column to another table as

INSERT INTO Tracker (table_id, type, date) VALUES ( ??? );

I'm not sure how to get those data fields within a trigger. Could someone give me a tip

Upvotes: 1

Views: 386

Answers (2)

lopushen
lopushen

Reputation: 1137

Use the NEW alias as it is shown in

MySQL trigger On Insert/Update events

As an example CREATE TRIGGER ins_sum BEFORE INSERT ON account FOR EACH ROW SET @sum = @sum + NEW.amount;

Upvotes: 1

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You can use NEW.column_name to get the inserted value within a trigger

INSERT INTO Tracker (table_id, type, date)
VALUES ( NEW.id_col_traker,NEW.col,NEW.col );

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.

Trigger Syntax and Examples

Upvotes: 2

Related Questions