The use of datetime and timestamp on the definition of mysql automatic update time field

The use of datetime and timestamp on the definition of mysql automatic update time field

发现网上尤其是这个博客里,一大堆的坑,而且都是深坑,很多看起来是大佬的人,结果给出来的答案都是坑,模拟两可,根本解决不了问题,而且还坑了不少萌新们的时间,所以我觉得应要写点什么,为遇到根我相同问题的萌新们一点小小的帮十足。

1. The target reason is to design two time fields, one is to create the time field to record the time when the data is stored in the database, and the other is to design and record when other fields are updated, the last update time will be automatically recorded.
Let's create a table.
For example, we want to create a membership table, which is used to record the member's name and points. We need to know the time when the member joins the membership and the time when his points change. create table users ( user_id
int
(
10 ) PRIMARY KEY NOT NULL AUTO_INCREMENT,
user_name varchar(20),
user_createtime datetime NOT NULL, (note that datetime is used here)
user_updatetime timestamp, (this is, every time we change the content of a database field, this field will Automatically record time)
user_phone int(20),
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
The above is just a simple example to illustrate how this field is defined.
In the mysql environment DESC users;
insert image description here en
the above picture, if you look carefully, you will find that the last line in the extra item after the last line user_updatetime indicates that the time will be automatically updated.
If there is an error, leave a message.
I found a lot of blogs on the Internet about the issue of automatic update time, and found that there are pitfalls everywhere. Finally found the final answer. I hope to help everyone, here is also received
https://blog.csdn.net/zhf_2016cs/article/details/52260063 This blogger's answer found the final answer! !

Guess you like

Origin blog.csdn.net/weixin_41863241/article/details/94277215