MySQL Forums
Forum List  »  Triggers

After Insert Copy Data to Another Table
Posted by: Elizabeth LaRose
Date: June 27, 2011 06:55AM

I had a nice trigger working and managed to eliminate it by accident. I'm having a hard time recreating it. I was going to post in Newbie, but I see that all Trigger Qs are getting moved here, so that is why I'm trying this forum.

What I want is to insert a new row in table PubData whenever table Purchases has a new row, and to copy the data in the ID field from table Purchases to the PubID field in table PubData.

The table structures look like this:

CREATE TABLE `Purchases` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Date` datetime NOT NULL,
  `Price` decimal(10,2) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=409 DEFAULT CHARSET=utf8 COLLATE=utf8_bin$$

CREATE TABLE `PubData` (
  `PubID` int(255) NOT NULL,
  `PubDate` datetime DEFAULT NULL,
  `Key` tinyint(4) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`Key`)
) ENGINE=InnoDB AUTO_INCREMENT=109 DEFAULT CHARSET=utf8$$

I need to do something like this:

CREATE TRIGGER `create_pubdata` AFTER INSERT ON `Purchases` FOR EACH ROW BEGIN
insert into PubData
SET PubID = Purchases.ID, PubDate = Now();
END;

All of the beginner-comprehensible AFTER INSERT examples I am finding use static values for the second table, whereas I am trying to get the values from another table.

Thanks in advance for any help. If any further info is needed, I can furnish it.

Elizabeth

Options: Reply• Quote


Subject
Views
Written By
Posted
After Insert Copy Data to Another Table
32110
June 27, 2011 06:55AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.