今天想要把一張已經有的資料表中記錄建立日期的一列設定成自動插入目前時間的模式,但是怎麼改都報錯:
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部落格的内容