当前位置:网站首页>Date time type and format in MySQL
Date time type and format in MySQL
2022-07-04 23:58:00 【1024 questions】
【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 inMySQL There are several time types commonly used in :date、datetime、time、year、timestamp
| date | 4 | 1000-01-01 | 9999-12-31 | 0000-00-00 |
| datetime | 8 | 1000-01-01 00:00:00 | 9999-12-31 23:59:59 | 0000-00-00 00:00: 00 |
| timestamp | 4 | 19700101080001 | 2038 Some time in the year | 0000000000000000 |
| time | 3 | -838:59:59 | 838:59:59 | 00:00:00 |
| year | 1 | 1901 | 2155 | 0000 |
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 instancecreate table my_date(d1 datetime,d2 date,d3 time,d4 timestamp,d5 year)charset utf8;desc my_dateAs 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

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();
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
have access to date_format( ) Function to convert time .
SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i:%s') from dual;
| %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) |
| %p | AM 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 |
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 .
边栏推荐
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- French scholars: the explicability of counter attack under optimal transmission theory
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- Compare two vis in LabVIEW
- 快解析内网穿透帮助企业快速实现协同办公
- Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
- Introduction to ACM combination counting
- 雅思考试流程、需要具体注意些什么、怎么复习?
- 【监控】zabbix
猜你喜欢

He worked as a foreign lead and paid off all the housing loans in a year

js如何实现数组转树

OSEK standard ISO_ 17356 summary introduction

Combien de temps faut - il pour obtenir un certificat PMP?

Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC

Tester's algorithm interview question - find mode
![[binary tree] the maximum difference between a node and its ancestor](/img/b5/1bc3d102754fc44c6a547807ebab94.png)
[binary tree] the maximum difference between a node and its ancestor

Using the uniapp rich text editor

Hash table, hash function, bloom filter, consistency hash

电力运维云平台:开启电力系统“无人值班、少人值守”新模式
随机推荐
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Illustrated network: what is gateway load balancing protocol GLBP?
华泰证券低佣金的开户链接安全吗?
积分商城游戏设置的基本要点
js正则表达式之中文验证(转)
"Xiaodeng" domain password policy enhancer in operation and maintenance
How many triangles are there in the golden K-line diagram?
OSEK standard ISO_ 17356 summary introduction
What is the difference between port mapping and port forwarding
The caching feature of docker image and dockerfile
[JS] - [dynamic planning] - Notes
Solution record of jamming when using CAD to move bricks in high configuration notebook
模板的进阶
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
「运维有小邓」域密码策略强化器
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Financial markets, asset management and investment funds
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
45 year old professor, she threw two super unicorns
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record