当前位置:网站首页>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_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
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 %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 .
边栏推荐
- QT personal learning summary
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
- ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
- Etcd database source code analysis - brief process of processing entry records
- PMP证书续证流程
- Hash table, hash function, bloom filter, consistency hash
- Why does infographic help your SEO
- 使用快解析搭建自己的minecraft服务器
- Netcore3.1 JSON web token Middleware
- 跨域请求
猜你喜欢
Galera cluster of MariaDB - dual active and dual active installation settings
香港珠宝大亨,22亿“抄底”佐丹奴
QT addition calculator (simple case)
45 year old professor, she threw two super unicorns
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Using the uniapp rich text editor
What is the difference between port mapping and port forwarding
取得PMP证书需要多长时间?
业务场景功能的继续修改
Using fast parsing intranet penetration to realize zero cost self built website
随机推荐
快解析——好用的内网安全软件
Build your own minecraft server with fast parsing
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
QT personal learning summary
多回路仪表在基站“转改直”方面的应用
Font design symbol combination multifunctional wechat applet source code
Actual combat simulation │ JWT login authentication
[monitoring] ZABBIX
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Meet ThreadPoolExecutor
Significance of acrel EMS integrated energy efficiency platform in campus construction
如何报考PMP项目管理认证考试?
城市轨道交通站应急照明疏散指示系统设计
"Xiaodeng" domain password policy enhancer in operation and maintenance
Solution record of jamming when using CAD to move bricks in high configuration notebook
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
巩固表达式C# 案例简单变量运算
挖财学院开户安全的吗?开户怎么开?
认识ThreadPoolExecutor