当前位置:网站首页>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 .
边栏推荐
- Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
- Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
- 积分商城游戏设置的基本要点
- 他做国外LEAD,用了一年时间,把所有房贷都还清了
- [IELTS reading] Wang Xiwei reading P4 (matching1)
- French scholars: the explicability of counter attack under optimal transmission theory
- 微软禁用IE浏览器后,打开IE浏览器闪退解决办法
- Summary of week 22-07-02
- Servlet+jdbc+mysql simple web exercise
- 快解析内网穿透帮助企业快速实现协同办公
猜你喜欢
Jar batch management gadget
业务场景功能的继续修改
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
同事的接口文档我每次看着就头大,毛病多多。。。
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
巩固表达式C# 案例简单变量运算
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
Etcd database source code analysis - brief process of processing entry records
Significance of acrel EMS integrated energy efficiency platform in campus construction
随机推荐
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
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
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
Galera cluster of MariaDB - dual active and dual active installation settings
积分商城游戏设置的基本要点
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
Cross domain request
企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)
人生无常,大肠包小肠, 这次真的可以回家看媳妇去了。。。
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
认识ThreadPoolExecutor
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
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Using fast parsing intranet penetration to realize zero cost self built website
How to use fast parsing to make IOT cloud platform
PMP certificate renewal process
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
实战模拟│JWT 登录认证
Jar batch management gadget