列名未将时间戳作为默认值
Column name is not taking timestamp as default value
我在同一个 table 中有两列 created_at 和 updated_at。两列的数据类型均为 datetime。现在,当我尝试按如下方式修改列数据类型时 -
alter table nodestatics modify column updated_at datetime default current_timestamp;
alter table nodestatics modify column created_at datetime default current_timestamp;
显示以下错误
Error Code : 1067 Invalid default value for 'updated_at' (0 ms taken)
Error Code : 1067 Invalid default value for 'created_at' (0 ms taken)
我的mysql版本是5.5.41-0ubuntu0.14.04.1 (Ubuntu)
很难在评论中引用 documentation:
As of MySQL 5.6.5, TIMESTAMP
and DATETIME
columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP
, and
for at most one TIMESTAMP
column per table.
要么升级到 5.6.5。或者使用 TIMESTAMP
而不是 DATETIME
.
试试这个
alter table nodestatics modify column created_at timestamp default current_timestamp;
使用TIMESTAMP
代替DATETIME
应该是这样的:
ALTER TABLE nodestatics MODIFY COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE nodestatics MODIFY COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
或者您可以使用 TRIGGERS
.
CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
FOR EACH ROW SET NEW.dateAdded = NOW();
你可以看看similar topic。编码愉快!
我在同一个 table 中有两列 created_at 和 updated_at。两列的数据类型均为 datetime。现在,当我尝试按如下方式修改列数据类型时 -
alter table nodestatics modify column updated_at datetime default current_timestamp;
alter table nodestatics modify column created_at datetime default current_timestamp;
显示以下错误
Error Code : 1067 Invalid default value for 'updated_at' (0 ms taken)
Error Code : 1067 Invalid default value for 'created_at' (0 ms taken)
我的mysql版本是5.5.41-0ubuntu0.14.04.1 (Ubuntu)
很难在评论中引用 documentation:
As of MySQL 5.6.5,
TIMESTAMP
andDATETIME
columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only forTIMESTAMP
, and for at most oneTIMESTAMP
column per table.
要么升级到 5.6.5。或者使用 TIMESTAMP
而不是 DATETIME
.
试试这个
alter table nodestatics modify column created_at timestamp default current_timestamp;
使用TIMESTAMP
代替DATETIME
应该是这样的:
ALTER TABLE nodestatics MODIFY COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE nodestatics MODIFY COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
或者您可以使用 TRIGGERS
.
CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
FOR EACH ROW SET NEW.dateAdded = NOW();
你可以看看similar topic。编码愉快!