Datetime,timestamp,date,year comparison of time types in MySQL

Source: Internet
Author: User

MySQL date type, date format, storage space, date range comparison.
Date type storage space Date format date range
------------ ---------   --------------------- -----------------------------------------
DateTime 8 bytes Yyyy-mm-dd HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59
Timestamp 4 bytes Yyyy-mm-dd HH:MM:SS 1970-01-01 00:00:01 ~ 2038
Date 3 bytes Yyyy-mm-dd 1000-01-01 ~ 9999-12-31
Year 1 bytes YYYY 1901 ~ 2155
When you create a table in MySQL, there are advantages to both datetime types: DateTime has a large date range, and timestamp occupies less storage space than half of the DateTime.
There are three default values for timestamp:
NULL
Not NULL DEFAULT Current_timestamp
Not NULL DEFAULT current_timestamp on UPDATE current_timestamp
which
Current_timestamp-Timestamp does not change when this record is updated
Current_timestamp on Update Current_timestamp-when this record is updated, the timestamp will change to the latest time
After setting the Current_timestamp default, inserting or updating records does not need to maintain the field, which is maintained entirely by MySQL, just like the AutoIncrement field, but if you use the DateTime field set above, you need to manually construct the time update , like now () and so on.
For current_timestamp on update current_timestamp, there is a special case where MySQL ignores the update operation if your updated field value (update) does not change, so the timestamp does not change. Also, if there are multiple current_timestamp on the Update current_timestamp column, then MySQL will only update the first one.

Datetime,timestamp,date,year comparison of time types in MySQL

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.