Solved

MySQL Update Trigger question

Posted on 2013-11-12
5
583 Views
Last Modified: 2013-11-12
I want to track latest Price change for an item.
The table name is res, field names are price, prevprice, priceupdatedate

I need help with writing a trigger that updates the field "prevprice" with the old price when the "price" field is updated and then writes the current time stamp to the priceupdatedate field.

code example appreciated! Thanks!
0
Comment
Question by:Bang-O-Matic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:alpmoon
ID: 39642549
Actaully there are useful sample trigger code on the web:

http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39642571
This is the trigger you need:

DELIMITER $$
CREATE TRIGGER updatePrevPrice BEFORE UPDATE ON res
   FOR EACH ROW BEGIN
      SET NEW.prevprice = OLD.price;
   END;
$$

Open in new window

Make sure your priceupdatedate column is set to a timestamp datatype and is the only one in there, and it'll update automatically as the rows changes.
0
 

Author Comment

by:Bang-O-Matic
ID: 39642737
@ChrisStanyon
Thanks for the help!

Running the trigger did work for the setting the NEW.prevprice = OLD.price

The priceupdateddate column I changed to timestamp but it did not update the field when updating the price.

I tried:

SET NEW.prevprice = OLD.price, priceupdatedate = NOW();

but that didn't work either says my version of MySQL doesnt support multiple triggers with same action time and event for one table.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39642767
if you edit the trigger using the code I provided it will try and create the same trigger again so you need to delete it first. the timestamp column should update automatically but only if it's the only timestamp column in your table. that may depend on your version of mysql
0
 

Author Closing Comment

by:Bang-O-Matic
ID: 39642828
Works perfect!
Thanks so much!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question