您的位置:首页 > 数据库 > MySQL

MySQL8.0建立一张能自动记录创建时间和更新时间的数据表

2019-04-08 20:52 1006 查看

今天想要把一张已经有的数据表中记录创建日期的一列设置成自动插入当前时间的模式,但是怎么改都报错:

ERROR 1067 (42000): Invalid default value for 'UpdateTime'

于是就从百度上搜集信息,得知想要这样功能的列,需要在建表的时候就设置好,不知道建表之后是不能修改还是我没有找到正确的方法,在这种情况下我尝试重新建表,然后成功得到我想要的具有自动记录插入时间和最后修改时间的功能的列,在这里记录一下,建表语句如下:

create table table_timestamp(
message_pk bigint not null auto_increment,
message_content varchar(99),
CreateTime timestamp not null default current_timestamp comment '创建时间',
UpdateTime timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
primary key(message_pk)
);

注意:

经过检查定位到下面语句,经过网上查询的资料发现,是默认的时间格式’0000-00-00 00:00:00’和timestamp的时间范围不符合导致的问题,将默认的时间格式改为CURRENT_TIMESTAMP或者是’1970-01-01 00:00:01’以上就可以正常导入了。

以上注意是引用自A2Z_development博客的内容

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: