Methods for storing millisecond data in Mysql _mysql

Source: Internet
Author: User

Quite a few friends who have just come in contact with MySQL have the same problem with the storage and display of milliseconds. Since the MySQL data type only provides DateTime, TIMESTAMP, Time, DATE and year of these types of times, and the minimum unit of datetime and TIMESTAMP is seconds, there is no function that stores the millisecond level. However, MySQL can recognize the millisecond portion of time. And there are a number of ways we can get milliseconds, like functions: Microsecond and so on.

I'll give you a simple example to store the seconds before and after.
For the application of the Time field as a primary key, we can create the following table for the corresponding transformation:

Mysql> CREATE TABLE Mysql_microsecond (log_time_prefix timestamp NOT null default 0, Log_time_suffix mediumint not nul L default 0) engine Innnodb;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

Mysql> ALTER TABLE Mysql_microsecond add primary key (Log_time_prefix, log_time_suffix);
Query OK, 0 rows affected (0.01 sec)
records:0 duplicates:0 warnings:0

mysql> Set @a = Convert (Concat (now (), '. 222009 '), datetime);
Query OK, 0 rows Affected (0.00 sec)

mysql> INSERT INTO Mysql_microsecond select Date_format (@a, '%y-%m-%d%h-%i-%s '), Date_format (@a, '%f ');
Query OK, 1 row Affected (0.00 sec)
records:1 duplicates:0 warnings:0

Mysql> select * from Mysql_microsecond;
+---------------------+-----------------+
| log_time_prefix |   log_time_suffix
| +---------------------+-----------------+
| 2009-08-11 17:47:02 |     222009 |
+---------------------+-----------------+
1 row in Set (0.00 sec)


or use varchar to store all the time fields, or store a hash to ensure performance!
A lot of ways, it depends on your application how to use reasonable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.