Solved

MySQL Update Trigger question

Posted on 2013-11-12
5
579 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
  • 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …

830 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