当前位置:网站首页>Date time type and format in MySQL

Date time type and format in MySQL

2022-07-04 23:58:00 1024 questions

Catalog

【1】MySQL Date time type in

① Explain in detail

② SQL Statement instance

③ timestamp Field

④ Test case

【2】 Format date time type ① DATE_FORMAT( ) function

② date_format( ) Transformation format

③ str_to_date() function

【1】MySQL Date time type in

MySQL There are several time types commonly used in :date、datetime、time、year、timestamp

The data type occupies the minimum value and the maximum value of bytes. The zero value indicates
date41000-01-019999-12-310000-00-00
datetime81000-01-01 00:00:009999-12-31 23:59:590000-00-00 00:00: 00
timestamp4197001010800012038 Some time in the year 0000000000000000
time3-838:59:59838:59:5900:00:00
year1190121550000
① Explain in detail

datetime : Time date type , The format is YYYY-mm-dd HH:ii:ss, The range of representation is from 1000 To 9999. But it has zero value ,0000-00-00 00:00:00;

date: date , Namely datetime Medium date part ;

time: Time ( paragraph ), Between a specified interval , from - Time out + Time ( Negative time indicates );

timestamp: Time stamp , It's not a regular timestamp ( Such as :14253685), The scope is ’1970-01-01 00:00:00’ To 2037 year . The format is YYYY-mm-dd HH:ii:ss, Format and datetime Exactly the same ;

year:yy and yyyy,yyyy The range is 1901-2155,yy The range is 1970-2069.

Two of you year(00-69 Express 2000-2069,70-99 Express 1970~1999). When the application only needs to record the year ,year Than date More save a space

② SQL Statement instance create table my_date(d1 datetime,d2 date,d3 time,d4 timestamp,d5 year)charset utf8;desc my_date

As shown in the figure below :year The default is 4 position , namely YYYY; timestamp Can't be empty , Have default values , Refresh this data column when creating new records and modifying existing records .

Insert several pieces of data as follows and correct time Do difference analysis :

insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','2015');insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-11:50:54','2015-09-28 11:51:08','2015');-- -11insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-211:50:54','2015-09-28 11:51:08','2015');-- -2 11insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-2 11:50:54','2015-09-28 11:51:08','2015');-- -2 For the past two days #year use 69 identification -2069insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','69');-- 69#year use 70 identification -1970insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','70');-- 70

③ timestamp Field

By default, as long as the current record is updated , This field will be automatically updated to the current time .

update my_date set d1 = SYSDATE() where d5=69;select * from my_date

that MySQL Can I get the real timestamp ? Certainly. !

select UNIX_TIMESTAMP();

④ Test case

4.1 Query the current time

SELECT SYSDATE() from dual;

4.2 Insert the current time into the above types of columns

insert INTO `user` (name,number,date,datetime,timestamp,time,year)VALUES ('Loum',3,SYSDATE(),SYSDATE(),SYSDATE(),SYSDATE(),2016);

4.3 mysql in datetime Length digits of type

As shown below , Usually we MySQL Middle design datetime The type length defaults to 0:

`work_time` datetime(0) DEFAULT NULL COMMENT ' Collection time ',

At this time, the insertion time is often seen :2020-08-29 12:52:16 Format . But if datetime(n) Medium n Not for 0 Well ?

`work_time` datetime(2) DEFAULT NULL COMMENT ' Collection time ',# datetime(n) Medium n The maximum value is 6`work_time` datetime(6) DEFAULT NULL COMMENT ' Collection time ',

At this moment in MySQL As shown below :

2020-08-29 12:52:16.01
2020-08-29 12:52:16.014057

You will find that there is a decimal point at the end, and the digits after the decimal point will correspond to the corresponding digits – This is called nanosecond .

Summarized below :

date : Only the date , don't have time ;

datetime: Have the time , With date ;

time: Only time , Accurate to minutes and seconds ;

timestamp: Time stamp , Accurate to minutes and seconds ;

year: year , Such as 2002, If it is written as 2002-01-15, Will be calculated , The insertion result is 1986

【2】 Format date time type ① DATE_FORMAT( ) function

have access to date_format( ) Function to convert time .

SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i:%s') from dual;

② date_format( ) Transformation format Format description
%a Abbreviated week name
%b Abbreviated month name
%c month , The number
%D Days of the month with English prefix
%d Day of the month , The number (00-31)
%e Day of the month , The number (0-31)
%f Microsecond
%H Hours (00-23)
%h Hours (01-12)
%I Hours (01-12)
%i minute , The number (00-59)
%j Days of (001-366)
%k Hours (0-23)
%l Hours (1-12)
%M Month name
%m month , The number (00-12)
%pAM or PM
%r Time ,12- Hours (hh:mm:ss AM or PM)
%S second (00-59)
%s second (00-59)
%T Time , 24- Hours (hh:mm:ss)
%U Zhou (00-53) Sunday is the first day of the week
%u Zhou (00-53) Monday is the first day of the week
%V Zhou (01-53) Sunday is the first day of the week , And %X Use
%v Zhou (01-53) Monday is the first day of the week , And %x Use
%W Week name
%w Days of the week (0= Sunday , 6= Saturday )
%X year , Sunday is the first day of the week ,4 position , And %V Use
%x year , Monday is the first day of the week ,4 position , And %v Use
%Y year ,4 position
%y year ,2 position
③ str_to_date() function

String conversion to date:

str_to_date('2016-12-15 16:48:40','%Y-%m-%d %H:%i:%S')

The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041835520655.html