当前位置:网站首页>Usage Summary of datetime and timestamp in MySQL
Usage Summary of datetime and timestamp in MySQL
2022-04-23 11:06:00 【liming89】
Source of the article : Learn through http://www.bdgxy.com/
One 、MySQL How to represent the current time in ?
Actually , There are many ways of expression , The summary is as follows :
- CURRENT_TIMESTAMP
- CURRENT_TIMESTAMP()
- NOW()
- LOCALTIME
- LOCALTIME()
- LOCALTIMESTAMP
- LOCALTIMESTAMP()
Two 、 About TIMESTAMP and DATETIME Comparison
A complete date format is as follows :YYYY-MM-DD HH:MM:SS[.fraction], It can be divided into two parts :date Part and time part , among ,date Part corresponds to... In the format “YYYY-MM-DD”,time Part corresponds to... In the format “HH:MM:SS[.fraction]”. about date For fields , It only supports date part , If you insert time Partial content , It discards the content of that part , And prompt a warning.
As shown below :
mysql> create table test(id int,hiredate date);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test values(1,‘20151208000000’);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(1,‘20151208104400’);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from test;
±-----±-----------+
| id | hiredate |
±-----±-----------+
| 1 | 2015-12-08 |
| 1 | 2015-12-08 |
±-----±-----------+
2 rows in set (0.00 sec)
notes : The first one didn't prompt warning The reason for this is its time Part of it is 0
TIMESTAMP and DATETIME Similarities :
1> Both can be used to express YYYY-MM-DD HH:MM:SS[.fraction] Date of type .?
TIMESTAMP and DATETIME The difference between :
1> They are not stored in the same way
about TIMESTAMP, It converts the time inserted by the client from the current time zone to UTC( World standard time ) For storage . When inquiring , Convert it to the current time zone of the client to return .
And for DATETIME, Don't make any changes , It's basically the original input and output .
below , So let's verify that
First, create two test tables , A use timestamp Format , A use datetime Format .
mysql> create table test(id int,hiredate timestamp);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test values(1,‘20151208000000’);
Query OK, 1 row affected (0.00 sec)
mysql> create table test1(id int,hiredate datetime);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test1 values(1,‘20151208000000’);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
±-----±--------------------+
| id | hiredate |
±-----±--------------------+
| 1 | 2015-12-08 00:00:00 |
±-----±--------------------+
1 row in set (0.01 sec)
mysql> select * from test1;
±-----±--------------------+
| id | hiredate |
±-----±--------------------+
| 1 | 2015-12-08 00:00:00 |
±-----±--------------------+
1 row in set (0.00 sec)
Both outputs are the same .
Secondly, modify the time zone of the current session
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
mysql> set time_zone=‘+0:00’;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
±-----±--------------------+
| id | hiredate |
±-----±--------------------+
| 1 | 2015-12-07 16:00:00 |
±-----±--------------------+
1 row in set (0.00 sec)
mysql> select * from test1;
±-----±--------------------+
| id | hiredate |
±-----±--------------------+
| 1 | 2015-12-08 00:00:00 |
±-----±--------------------+
1 row in set (0.01 sec)
Above “CST” refer to MySQL The system time of the host , It is the abbreviation of Chinese standard time ,China Standard Time UT+8:00
It can be seen from the results ,test The return time in is advanced 8 Hours , and test1 Time does not change in . This fully verifies the difference between the two .
2> The two can store different time ranges
timestamp The range of time that can be stored is :'1970-01-01 00:00:01.000000' To '2038-01-19 03:14:07.999999'.
datetime The range of time that can be stored is :'1000-01-01 00:00:00.000000' To '9999-12-31 23:59:59.999999'.?
summary :TIMESTAMP and DATETIME Except that the storage range and storage mode are different , There's no big difference . Of course , For business across time zones ,TIMESTAMP More appropriate .?
3、 ... and 、 About TIMESTAMP and DATETIME Automatic initialization and update of
First , Let's take a look at the following operations
mysql> create table test(id int,hiredate timestamp);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test(id) values(1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
±-----±--------------------+
| id | hiredate |
±-----±--------------------+
| 1 | 2015-12-08 14:34:46 |
±-----±--------------------+
1 row in set (0.00 sec)
mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE test
(
id
int(11) DEFAULT NULL,
hiredate
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
Does it look a little strange , I'm not right hiredate Field to insert , Its value is automatically changed to the current value , And when creating tables , I don't have a definition “show create table test\G” The result shows “ DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”.
Actually , This feature is automatic initialization and automatic update (Automatic Initialization and Updating).
Automatic initialization means that if the field ( For example, in the above example hiredate Field ) No explicit assignment , It is automatically set to the current system time .
Automatic update means that if other fields are modified , The value of this field will be automatically updated to the current system time .
It is associated with “explicit_defaults_for_timestamp” Parameters are related to .
By default , The value of this parameter is OFF, As shown below :
mysql> show variables like '%explicit_defaults_for_timestamp%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF |
+---------------------------------+-------+
1 row in set (0.00 sec)
Let's take a look at the description of the official file :
By default, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.
A lot of times , That's not what we want , How to disable ?
1. take “explicit_defaults_for_timestamp” Is set to ON.
2. “explicit_defaults_for_timestamp” The value of is still OFF, There are also two ways to disable
???? 1> use DEFAULT Clause specifies a default value for this column
???? 2> Specify for this column NULL attribute .
As shown below :
mysql> create table test1(id int,hiredate timestamp null);
Query OK, 0 rows affected (0.01 sec)
mysql> show create table test1\G
*************************** 1. row ***************************
Table: test1
Create Table: CREATE TABLE test1
(
id
int(11) DEFAULT NULL,
hiredate
timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> create table test2(id int,hiredate timestamp default 0);
Query OK, 0 rows affected (0.01 sec)
mysql> show create table test2\G
*************************** 1. row ***************************
Table: test2
Create Table: CREATE TABLE test2
(
id
int(11) DEFAULT NULL,
hiredate
timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
stay MySQL 5.6.5 Before the release ,Automatic Initialization and Updating Only applicable to TIMESTAMP, And in a table , At most one is allowed TIMESTAMP The field takes this property . from MySQL 5.6.5 Start ,Automatic Initialization and Updating Apply to both TIMESTAMP and DATETIME, And there is no limit to the number .
Reference resources :
1. http://dev.mysql.com/doc/refman/5.6/en/datetime.html
2. http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
This is about Mysql In the relevant Datetime and Timestamp This is the end of the article on the summary of the use of , More about Mysql Datetime and Timestamp Please search rookie tutorial www.piaodoo.com Previous articles or continue to browse the relevant articles below. I hope you can support rookie tutorials in the future www.piaodoo.com!
版权声明
本文为[liming89]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102124711.html
边栏推荐
- More reliable model art than deep learning
- 最强日期正则表达式
- Visual common drawing (IV) histogram
- Manjaro installation and configuration (vscode, wechat, beautification, input method)
- 升级cpolar内网穿透能获得的功能
- How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
- FileProvider 路径配置策略的理解
- CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
- Use of SVN:
- Hikvision face to face summary
猜你喜欢
Visualized common drawing (II) line chart
Visual common drawing (I) stacking diagram
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
Visual solutions to common problems (VIII) mathematical formulas
Jupyter Lab 十大高生产力插件
Visual common drawing (III) area map
关于JUC三大常用辅助类
SVN的使用:
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
随机推荐
Full stack cross compilation x86 completion process experience sharing
Learning Notes 6 - Summary of several deep learning convolutional neural networks
About the three commonly used auxiliary classes of JUC
An interesting interview question
Use of SVN:
Kaggle - real battle of house price prediction
一道有趣的阿里面试题
最强日期正则表达式
使用zerotier让异地设备组局域网
妊娠箱和分娩箱的区别
Mba-day5 Mathematics - application problems - engineering problems
Typora operation skill description (I) md
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
Excel·VBA数组冒泡排序函数
Visualization Road (10) detailed explanation of segmentation canvas function
VM set up static virtual machine
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
Promise详解
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
Source insight 4.0 FAQs