当前位置:网站首页>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 .
边栏推荐
- JS convert pseudo array to array
- In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
- 解决无法通过ssh服务远程连接虚拟机
- Nine Qi single chip microcomputer ny8b062d single key control four LED States
- [paper reading] Tun det: a novel network for meridian ultra sound nodule detection
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- The pit of sizeof operator in C language
- Is it safe to open and register new bonds? Is there any risk? Is it reliable?
- Pytoch --- use pytoch to realize linknet for semantic segmentation
- 【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
猜你喜欢

高配笔记本使用CAD搬砖时卡死解决记录

Microservice

圖解網絡:什麼是網關負載均衡協議GLBP?

机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
![[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation](/img/41/eb790e7419a158e985fa503bd7dc17.png)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation

French scholars: the explicability of counter attack under optimal transmission theory
![[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection

如何有效对直流列头柜进行监测

人脸识别5- insight-face-paddle-代码实战笔记

A new method for analyzing the trend chart of London Silver
随机推荐
A new method for analyzing the trend chart of London Silver
基于三维gis平台的消防系统运用
企业应用业务场景,功能添加和修改C#源码
Pytoch --- use pytoch to realize linknet for semantic segmentation
Examples of time (calculation) total tools: start time and end time of this year, etc
实战模拟│JWT 登录认证
快解析内网穿透帮助企业快速实现协同办公
挖财学院开户安全的吗?开户怎么开?
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
[binary tree] the maximum difference between a node and its ancestor
Five papers recommended for the new development of convolutional neural network in deep learning
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Face recognition 5- insight face padding code practice notes
JS convert pseudo array to array
The pit of sizeof operator in C language
取得PMP證書需要多長時間?
Microservice
js如何实现数组转树
How to apply for PMP project management certification examination?
go踩坑——no required module provides package : go.mod file not found in current directory or any parent