Details determine success or failure-Time Storage in mysql (datetime, timestamp, int),-mysqltimestamp

Source: Internet
Author: User

Details determine success or failure-Time Storage in mysql (datetime, timestamp, int),-mysqltimestamp

To store time in mysql, we can use datetime, timestamp, or int format. So what should we consider when designing?


First, I think we should understand how these formats are, and then look at their differences so that we can think about when to use the correct format.


DATETIME format, the default is "YYYY-MM-DD HH: MM: SS", represented by 19 characters, from 1000-01-01 00:00:00-9999-12-31 23:59:59.


The TIMESTAMP format is also 'yyyy-MM-DD HH: MM: ss'. Unlike DATETIME, the value range of the year is 1970-2037.


The INT format is an integer. It can control the number of digits. Generally, we can set it to 10 digits.


When Will DATETIME be used? When the format to be displayed is relatively fixed, that is, the format of year, month, day, hour, minute, and second will be used. Although the timestamp is smaller than the datetime range, however, we generally think that the range is large enough. In addition, it can also be set to automatically modify the value when it is updated, that is, when we modify the database, such as insert or update, when we set this field

CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, which is automatically filled in with the current time.


So when should we use int? int should not be in the time format. However, after a lot of programming practices, we often need to format the display time in php programming, for example, to display only the year, month, and day, if the database stores int-type time, we can directly use the php built-in function date () for conversion, which is very convenient, in addition, it is easier to compare the time, because it is of the int type, you can directly compare the size to compare the time.


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.