A Brief Analysis on the Time Carrying Problem of MySQL

A Brief Analysis on the Time Carrying Problem of MySQL

The default time type (datetime and timestamp) in MySQL has a precision of seconds. If the time value is set with a precision less than a second, it will be rounded off, which may cause the value in the database to be one second more than the original value. In other words, records that originally belonged to today may be recorded tomorrow.

Below is an example that demonstrates how time is rounded. First create a table:

CREATE TABLE test_time (
 time_sec datetime,
 time_millis datetime(3),
 time_micros datetime(6),
 stamp_sec timestamp,
 stamp_millis timestamp(3),
 stamp_micros timestamp(6)
);

Some readers may not know that datetime and timestamp can be defined with precision. The precision value is 0~6, indicating how many decimal places are retained. The default value is 0. Obviously, retaining 3 digits can be regarded as milliseconds precision, and retaining 6 digits can be regarded as microseconds precision.

Then we insert a record:

INSERT INTO test_time
( time_sec, time_millis, time_micros, 
 stamp_sec, stamp_millis, stamp_micros )
VALUES(
 '2019-11-30 12:34:56.987654', 
 '2019-11-30 12:34:56.987654', 
 '2019-11-30 12:34:56.987654',
 '2019-11-30 12:34:56.987654', 
 '2019-11-30 12:34:56.987654', 
 '2019-11-30 12:34:56.987654'
);

Then do another select * from test_time query and you will see the following results:

time_sec |time_millis |time_micros |stamp_sec |stamp_millis |stamp_micros |
---------------------|-----------------------|--------------------------|---------------------|-----------------------|--------------------------|
2019-11-30 12:34:57.0|2019-11-30 12:34:56.988|2019-11-30 12:34:56.987654|2019-11-30 12:34:57.0|2019-11-30 12:34:56.988|2019-11-30 12:34:56.987654|

You can see that the second values ​​of time_sec and stamp_sec in the database are rounded up, and the millisecond values ​​of time_millis and stamp_millis are rounded up.

It can be seen that there are two ways to avoid such errors:

  • Use datetime(6) or timestamp(6) when defining fields;
  • The field is defined without precision, but the millisecond value is truncated before the time is stored in the database.

Related Documents:

MySQL 5.6 Reference: Fractional Seconds in Time Values

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Summary of MySQL date data type and time type usage
  • Detailed explanation of TIMESTAMP usage in MySQL
  • MySql query time period method
  • MySQL date and time format conversion implementation statement
  • mysql calculate time difference function
  • FROM_UNIXTIME Format MYSQL timestamp function
  • Mysql date time DATE_FORMAT (date, format)
  • Functions and methods for converting dates and timestamps in MySQL
  • How to write various SQL statements in MySQL to get time data for one day, one week, and one month
  • MySQL timestamp automatic update time sharing

<<:  Example of Vue's implementation of the underlying code for simulating responsive principles

>>:  How to solve the Docker container startup failure

Recommend

How to manually scroll logs in Linux system

Log rotation is a very common function on Linux s...

Nginx Layer 4 Load Balancing Configuration Guide

1. Introduction to Layer 4 Load Balancing What is...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

Summary of various implementation methods of mysql database backup

This article describes various ways to implement ...

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

React configuration px conversion rem method

Install related dependencies npm i lib-flexible -...

uniapp realizes the recording upload function

Table of contents uni-app Introduction HTML part ...

Ubuntu 16.04 kernel upgrade steps

1. Environment Ubuntu 16.04 running on a virtual ...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Several methods of implementing two fixed columns and one adaptive column in CSS

This article introduces several methods of implem...

How to add shortcut commands in Xshell

As a useful terminal emulator, Xshell is often us...

Detailed installation tutorial of mysql 5.7.11 under Win7 system

Operating system: Win7 64-bit Ultimate Edition My...

Common methods of Vue componentization: component value transfer and communication

Related knowledge points Passing values ​​from pa...

The use of mysql unique key in query and related issues

1. Create table statement: CREATE TABLE `employee...