I have a table with a DEC field named product_price
and I wanted to add a field called price_updated_date
. Is there a way to set the table to automatically insert the current time-stamp whenever the product_price
field has been updated?
If not, is there a way to set it to insert the current time-stamp any time the entry is updated at all?
It seems like using a trigger is the best option here. I am new to triggers and having some trouble creating it. Here is my code:
CREATE TRIGGER price_update
AFTER UPDATE ON cart_product
FOR EACH ROW
IF(OLD.product_price != NEW.product_price)
THEN
UPDATE cart_product
SET price_updated_date = CURDATE()
WHERE product_id = NEW.product_id
This is giving me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8
Why not set properties for that field as:
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With