Jump to content

Mysql trigger - simple question


miklas

Recommended Posts

 

 

Hi,

 

I have one table (TABLE A) in my mysql database which has the listed fields:

 

    * row1_id - PK

    * row2_id - PK

    * row3_data

 

What I would like to do is, every time a record is inserted or updated in table A, I would like to call a trigger to write me the new information of table A in another table, TABLE A_LOG, which has the following fields:

 

    * row0-modification-id - PK

    * row1_id (from table A) - PK

    * row2_id (from table A) - PK

    * row3_data (from table A)

 

the Table A_LOG should then have 3 PKeys, two from the new inserted/updated record of table A and other that indicates me the number of the modification (1, 2, 3..)

 

Thank you very much for your help.

 

Link to comment
Share on other sites

What MySQL version you have? Triggers are available since 5.0.2

 

DELIMITER ||
CREATE TRIGGER `A_afterInsert` AFTER INSERT ON `A`
  FOR EACH ROW BEGIN
    INSERT INTO `A_LOG` (row1_id,row2_id,row3_data) VALUES (NEW.row1_id,NEW.row2_id,NEW.row3_data);
  END;
||

CREATE TRIGGER `A_afterUpdate` AFTER UPDATE ON `A`
  FOR EACH ROW BEGIN
    INSERT INTO `A_LOG` (row1_id,row2_id,row3_data) VALUES (NEW.row1_id,NEW.row2_id,NEW.row3_data);
  END;
||

DELIMITER ;

 

 

Link to comment
Share on other sites

Hi,

 

thank you very much for your quick reply.

My sql version is fine, triggers work fine ;)

 

It seems like this is it. But I didn't see any reference to the row0_id there. this will work anyway?

 

thks

 

What MySQL version you have? Triggers are available since 5.0.2

 

DELIMITER ||
CREATE TRIGGER `A_afterInsert` AFTER INSERT ON `A'
  FOR EACH ROW BEGIN
    INSERT INTO `A_LOG` (row1_id,row2_id,row3_data) VALUES (NEW.row1_id,NEW.row2_id,NEW.row3_data);
  END;
||

CREATE TRIGGER `A_afterUpdate` AFTER UPDATE ON `A`
  FOR EACH ROW BEGIN
    INSERT INTO `A_LOG` (row1_id,row2_id,row3_data) VALUES (NEW.row1_id,NEW.row2_id,NEW.row3_data);
  END;
||

DELIMITER ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.