Difference between DATETIME and TIMESTAMP in MySQL

Source: Internet
Author: User

First Copy a document to show you:

DateTime
A date and time combination. The supported range is '2017-01-01 00:00:00 'to '2017-12-31 23:59:59 '.
MySQL displays DATETIME values in 'yyyy-MM-DD HH: MM: ss' format, but allows you to assign values to DATETIME columns using either strings or numbers.
TimeStamp
A timestamp. The range is '2017-01-01 00:00:00 'to partway through the year 1970.
A timestamp column is useful for recording the date and time of an INSERT or UPDATE operation.
The first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself.
You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.

Now let's start a specific comparison:

DATETIME, 8 bytes, value range: "1000-01-01 00:00:00 -- 9999-12-31 23:59:59"

The corresponding Java type is java. SQL. Timestamp.

During the INSERT or UPDATE operation, the system does not automatically modify the value. You cannot set the default value. You must manually INSERT a required field. We recommend that you use new ()

MySql formatted data by YYYY-MM-DD HH: MM: SS, allowed to be submitted in string and Number Format

Eg: insert into time_table (CreateDate) values ('2017-06-09 15:01:01 ')

Or insert into time_table (CreateDate) values ('20140901 ')

TIMESTAMP. The number of bytes is 4 and the value range is "197001000000001--20108119111407"

The corresponding Java type is java. SQL. Timestamp.

During the INSERT or UPDATE operation (without manual assignment), the system automatically updates and inserts the current system time. The default value is CURRENT_TIMESTAMP ()

When you submit a manual assignment to NULL, the assignment will also be assigned to the current system time, and the error will be filled with 0

When using TIMESTAMP, be sure to pay attention to its time range (see above ).

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.