Differences between several date and time types in MySQL and their use

Source: Internet
Author: User

There are several times in MySQL: Date, time, DateTime, timestamp, year

MySQL data type meaning

Date only stored date, ' 2018-07-12 '

Time is stored only seconds and minutes, ' 09:51:02 '

DateTime storage Month day minute minute, ' 2018-07-12 09:51:02 '

Timestamp storage date and minute, ' 2018-07-12 09:51:02 ' Note: This type of field time will automatically refresh as other field values are modified

Year only stores years, ' 2018 '

You can do it yourself in the database

Create the test table as follows:

DROP TABLE IF EXISTS' test ';CREATE TABLE' Test ' (' Time ' timeDEFAULT NULL, ' Date ' dateDEFAULT NULL,  `datetime`datetime DEFAULT NULL,  `timestamp`timestamp NULL DEFAULT NULL  on UPDATE Current_timestamp,  ` Year` Year(4)DEFAULT NULL) ENGINE=MyISAMDEFAULTCHARSET=UTF8;

Insert a piece of data:

INSERT  into datetime timestamp  Year VALUES ('2018-07-12 09:51:02'2018-07-12 09:51:02'  2018-07-12 09:51:02'2018-07-12 09:51:02'  2018');

Show below:

It is important to note that the year Type field, when inserting data, can only insert two-bit/four-bit digits, or two-bit/four-bit numeric strings,

Insert a two-digit number:

Less than 70 of the time, save to the database is ' 20** ';

When greater than or equal to 70, save to the database is ' 19** ';

Insert a four-digit number:

Only four digits between 1901--2099 are inserted

These people can test themselves.

Differences between several date and time types in MySQL and their use

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.