This is a post about SYSDATE() and NOW() and CURRENT_TIMESTAMP() functions in MySQL.
Firstly, note is that of these three, only CURRENT_TIMESTAMP() is part of the SQL Standard. NOW() happens to be an alias for CURRENT_TIMESTAMP() in MySQL.
Secondly, note that replication does not work well with non-deterministic functions. And “hey, what time is it?” is non-deterministic. Ask it twice, with a second apart between asking, and both times you get different results (with at least second precision).
You can start to see the problem here….but there’s more…. (more…)
MySQL
replication
now
datetime
timestamp
deterministic
sysdate
Have you ever needed a random timestamp in MySQL? For example to create demo data programmatically? Here’s my solution:
SELECT FROM_UNIXTIME(
FLOOR(
UNIX_TIMESTAMP('2007-01-01') +
RAND() *
(UNIX_TIMESTAMP('2007-01-03')-UNIX_TIMESTAMP('2007-01-01'))
)
) as random_timestamp;
Or if you prefer a function:
CREATE FUNCTION random_timestamp (start TIMESTAMP, end TIMESTAMP)
RETURNS TIMESTAMP NOT DETERMINISTIC
RETURN FROM_UNIXTIME(
FLOOR(
UNIX_TIMESTAMP(start) +
RAND() *
(UNIX_TIMESTAMP(end)-UNIX_TIMESTAMP(start))
)
);
mysql> select random_timestamp('2007-10-01', NOW());
+---------------------------------------+
| random_timestamp('2007-10-01', NOW()) |
+---------------------------------------+
| 2007-10-05 23:07:11 |
+---------------------------------------+
1 row in set (0.00 sec)