patternsqlMajor
In the same query INSERT, can two VALUES being NOW() return a different time?
Viewed 0 times
sametheinsertcanreturnquerytimebeingnowtwo
Problem
I met a query similar to
having column definitions
Will in this case MySQL set the NOW() value to the current time and return that same values to both calls, or will that query run the (improbable) risk of having two slightly different times for Created and Updated?
Note: I can't change the field types.
INSERT INTO mytable (id, Created, Updated) VALUES (null, NOW(), NOW())having column definitions
| Created | datetime |
| Updated | datetime |Will in this case MySQL set the NOW() value to the current time and return that same values to both calls, or will that query run the (improbable) risk of having two slightly different times for Created and Updated?
Note: I can't change the field types.
Solution
The time returned by
MySQL docs for
NOW() returns a constant time that indicates the time at which the statement began to execute. ...
You will always get the same
Worth noting, that this isn't per transaction:
NOW(), and other date time functions, is derived from the start time of the query. The THD class here is used to contain all the information for the connection. The NOW() function implementation grabs this value and returns it into the now_time structure.MySQL docs for
NOW() also state:NOW() returns a constant time that indicates the time at which the statement began to execute. ...
You will always get the same
NOW() value from anywhere in the query.MariaDB [test]> select now(),sleep(10),now();
+---------------------+-----------+---------------------+
| now() | sleep(10) | now() |
+---------------------+-----------+---------------------+
| 2021-03-22 14:17:05 | 0 | 2021-03-22 14:17:05 |
+---------------------+-----------+---------------------+Worth noting, that this isn't per transaction:
MariaDB [test]> start transaction
-> ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select now();
+---------------------+
| now() |
+---------------------+
| 2021-03-22 14:20:53 |
+---------------------+
1 row in set (0.00 sec)
MariaDB [test]> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
MariaDB [test]> select now();
+---------------------+
| now() |
+---------------------+
| 2021-03-22 14:21:00 |
+---------------------+
1 row in set (0.00 sec)Code Snippets
MariaDB [test]> select now(),sleep(10),now();
+---------------------+-----------+---------------------+
| now() | sleep(10) | now() |
+---------------------+-----------+---------------------+
| 2021-03-22 14:17:05 | 0 | 2021-03-22 14:17:05 |
+---------------------+-----------+---------------------+MariaDB [test]> start transaction
-> ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select now();
+---------------------+
| now() |
+---------------------+
| 2021-03-22 14:20:53 |
+---------------------+
1 row in set (0.00 sec)
MariaDB [test]> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
MariaDB [test]> select now();
+---------------------+
| now() |
+---------------------+
| 2021-03-22 14:21:00 |
+---------------------+
1 row in set (0.00 sec)Context
StackExchange Database Administrators Q#287454, answer score: 25
Revisions (0)
No revisions yet.