Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NOW() for DATETIME InnoDB Transaction guaranteed?

Does using NOW() in 2+ queries in a single InnoDB transaction guarantee that the inserted datetime value will be exact in the database?

In other words, is the NOW(), even if you have more than 20 queries in a single transaction using it always going to be the same, or will it change?

like image 493
Maverick Avatar asked Apr 11 '12 20:04

Maverick


1 Answers

Apparently it is not guaranteed across a transaction but can change from statement to statement. There is a workaround you can use as shown here:

BEGIN;
SELECT @now := NOW();
INSERT ... VALUES (..., @now, ...);
INSERT ... VALUES (..., @now, ...);
UPDATE ... @now ...;
COMMIT; 

If you want to avoid that altogether just set the current date and time into a PHP variable and use that instead.

like image 103
John Conde Avatar answered Sep 24 '22 00:09

John Conde