The difference between NOW, CURRENT_TIMESTAMP and SYSDATE in MYSQL

The difference between NOW, CURRENT_TIMESTAMP and SYSDATE in MYSQL

mysql> select NOW(),CURRENT_TIMESTAMP(),SYSDATE();
  • 1

Execute the above SQL, the result is as follows:

+---------------------+---------------------+---------------------+
| NOW()               | CURRENT_TIMESTAMP() | SYSDATE()           |
+---------------------+---------------------+---------------------+
| 2016-12-30 17:00:46 | 2016-12-30 17:00:46 | 2016-12-30 17:00:46 |
+---------------------+---------------------+---------------------+
  • 1
  • 2
  • 3
  • 4
  • 5

All three functions return the current system time.

So, is there a difference between them?

Let's try to improve the precision of the time and look at the results again:

mysql> select NOW(6),CURRENT_TIMESTAMP(6),SYSDATE(6);
  • 1

As above, we add parameter values ​​to these functions. This parameter value represents the number of decimal places after the second. The maximum value is 6, which means it is accurate to the microsecond level. The default value is 0, which means it is accurate to the second. 
At this point, we get the result:

+----------------------------+----------------------------+----------------------------+
| NOW(6)                     | CURRENT_TIMESTAMP(6)       | SYSDATE(6)                 |
+----------------------------+----------------------------+----------------------------+
| 2016-12-30 17:03:37.619547 | 2016-12-30 17:03:37.619547 | 2016-12-30 17:03:37.619635 |
+----------------------------+----------------------------+----------------------------+
  • 1
  • 2
  • 3
  • 4
  • 5

It can be seen that the results obtained by NOW and CURRENT_TIMESTAMP are the same, while the results of SYSDATE are slightly larger than those of the first two.

If you use MYSQL version 5.5 or lower, these functions do not support adding parameters. To compare the differences between these three functions, the following SQL can be used:

mysql> select NOW(),CURRENT_TIMESTAMP(),SYSDATE(),SLEEP(2),NOW(),CURRENT_TIMESTAMP(),SYSDATE();
  • 1

Among them, SLEEP (2) is to let the SQL wait for two seconds before continuing to execute. 
At this point, the result obtained is:

+---------------------+---------------------+---------------------+----------+---------------------+---------------------+---------------------+
| NOW()               | CURRENT_TIMESTAMP() | SYSDATE()           | SLEEP(2) | NOW()               | CURRENT_TIMESTAMP() | SYSDATE()           |
+---------------------+---------------------+---------------------+----------+---------------------+---------------------+---------------------+
| 2016-12-30 17:01:24 | 2016-12-30 17:01:24 | 2016-12-30 17:01:24 |        0 | 2016-12-30 17:01:24 | 2016-12-30 17:01:24 | 2016-12-30 17:01:26 |
+---------------------+---------------------+---------------------+----------+---------------------+---------------------+---------------------+
  • 1
  • 2
  • 3
  • 4
  • 5

It can be seen that the results obtained by NOW and CURRENT_TIMESTAMP are always the same, while the difference between SYSDATE is 2 seconds before and after the interruption.

In fact, there is no difference between NOW and CURRENT_TIMESTAMP, they both represent the system time when SQL started executing; and SYSDATE represents the system time when this function was executed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325394461&siteId=291194637