The main misunderstanding in MySQL with timestamps is that by default MySQL returns and saves timestamps without a fraction .
SELECT current_timestamp() => 2018-01-18 12:05:34
which can be converted to seconds mark in seconds
SELECT UNIX_TIMESTAMP(current_timestamp()) => 1516272429
To add a fractional part:
SELECT current_timestamp(3) => 2018-01-18 12:05:58.983
which can be converted to a timestamp in microseconds, like
SELECT CAST( 1000*UNIX_TIMESTAMP(current_timestamp(3)) AS UNSIGNED INTEGER) ts => 1516272274786
There are several tricks with table storage. If your table was created as
CREATE TABLE 'ts_test_table' ( 'id' int(1) NOT NULL, 'not_fractional_timestamp' timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ('id') ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
than MySQL will not store the fractional part inside it:
id, not_fractional_timestamp 1, 2018-01-18 11:35:12
If you want to add a fractional part to your table, you need to create your table differently:
CREATE TABLE 'ts_test_table2' ( 'id' int(1) NOT NULL, 'some_data' varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL, 'fractional_timestamp' timestamp(3) NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), PRIMARY KEY ('id') ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
which leads to the desired result:
id, some_data, fractional_timestamp 1, 8, 2018-01-18 11:45:40.811
The function current_timestamp () can get a value up to 6, but I found (at least in my installed version of MySQL 5.7.11) that the accuracy of fraction 6 leads to the same constant value of 3 digits in the tail, in my case 688
id, some_data, fractional_timestamp 1, 2, 2018-01-18 12:01:54.167688 2, 4, 2018-01-18 12:01:58.893688
This means that the actual usable precision in MySQL is 3.