Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL set current date in a DATETIME field on insert

Tags:

mysql

triggers

I have a 'created_date' DATETIME field which I would like to be populated with the current date upon insert. What syntax should I use for the trigger for this? This is what I have so far but it is incorrect.

CREATE TRIGGER set_created_date
BEFORE INSERT ON product
FOR EACH ROW BEGIN
SET NEW.created_date = now();
like image 514
William Avatar asked Sep 05 '25 11:09

William


1 Answers

DELIMITER ;;
CREATE TRIGGER `my_table_bi` BEFORE INSERT ON `my_table` FOR EACH ROW
BEGIN
    SET NEW.created_date = NOW();
END;;
DELIMITER ;
like image 151
pevik Avatar answered Sep 08 '25 02:09

pevik