Many friends migrating to MySQL from Oracle or SQL SERVER encounter the same problem, that is, the storage and display of milliseconds. Since MySQL only provides datetime, TIMESTAMP, time, date, year, and the minimum unit of datetime and TIMESTAMP is seconds, there is no function to store the millisecond level. But MySQL can recognize the milliseconds in time. And we have a number of ways to get milliseconds, such as functions: Microsecond and so on.
Let me give you a simple example to store the parts before and after the second.
For the application of the Time field as the primary key, we can create the following tables for the corresponding conversions:
mysql> CREATE TABLE Mysql_microsecond (log_time_prefix timestampnot null default 0, Log_time_suffix mediumint not Null 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)
Either use varchar to store all the time fields, or store a hash to ensure performance!
A lot of ways to see how your application is reasonable.
MySQL newer version (MySQL 6.0.5), also has not produced a microsecond function, now () can only be accurate to seconds. There is also no date-time type stored in MySQL with milliseconds or microseconds.
But, oddly enough, MySQL already has a function of extracting (extract) microseconds. For example:
Select Microsecond (' 12:00:00.123456 '); --123456select microsecond (' 1997-12-31 23:59:59.000010 '); --10
Select Extract (microsecond from ' 12:00:00.123456 '); --123456select Extract (microsecond from ' 1997-12-31 23:59:59.000010 '); --10
Select Date_format (' 1997-12-31 23:59:59.000010 ', '%f '); --000010
Still, you want to get a millisecond, microsecond, or something in the application-level program for MySQL. If you get time in your application that contains microseconds: 1997-12-31 23:59:59.000010, when MySQL is stored, you can design two fields: C1 datetime, C2 mediumint, respectively, and hold the date and microsecond. Why not use char to store it? The char type requires bytes, and datetime + Mediumint is only 11 (8+3) bytes.
Millisecond, microsecond noun explanation:
Milliseconds: Millisecond--1 per thousand sec microseconds: microsecond--one out of 10,000 seconds 1 seconds = 1000 milliseconds; 1 milliseconds = 1000 microseconds
MySQL deals with milliseconds and microseconds, MySQL gets milliseconds!