Comparison of datetime and timestamp in MySQL

Source: Internet
Author: User

In mysql, there are many time and date functions that I commonly use: datetime and timestamp. Although they are all called date periods, they are still different in terms of expression and range, next I will introduce it.


Comparison


Storage

TIMESTAMP

Storage of 1.4 bytes (Time stamp value is stored in 4 bytes)

2. Save the value in UTC format (it stores the number of milliseconds)

3. Time Zone conversion. during storage, the current time zone is converted, and then retrieved to the current time zone.

Datetime

1.8 bytes (8 bytes storage)

2. Storage in actual format (Just stores what you have stored and retrieves the same thing which you have stored .)

3. It has nothing to deal with the TIMEZONE and Conversion .)


Summary

Similarities

Can be used to indicate the time
Are displayed as strings

Differences

As the name suggests, date only represents the date in the form of 'yyyy-MM-DD ', datetime represents the date plus time in the form of 'yyyy-MM-DD HH: mm: ss', timestamp and datetime display form is the same.
The time range that date and datetime can represent is '2017-01-01 'to '2017-12-31'. timestamp is limited by 32-bit int type, it indicates the UTC time between '2017-01-01 00:00:01 'and '2017-01-19 03:14:07.
When mysql stores the timestamp type, it converts the time to UTC time, and then restores it to the current time zone when reading the data. If you have stored a value of the timestamp type and modified the mysql time zone, you will get an error time when you read the value again. This does not happen in date and datetime.
 
The timestamp type provides the automatic update function. You only need to set its default value to CURRENT_TIMESTAMP.
Except that date is retained to the day, datetime and timestamp are retained to the second, while millisecond is ignored.

Related Article

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.