简体   繁体   English

列名未将时间戳作为默认值

[英]Column name is not taking timestamp as default value

I have two columns created_at and updated_at in same table. 我在同一个表中有两列created_atupdated_at Both the columns have data type as datetime . 这两列的数据类型都是datetime Now when I am trying to modify the column data types as follows - 现在,当我尝试修改列数据类型时,如下所示 -

alter table nodestatics modify column updated_at datetime default current_timestamp; 

alter table nodestatics modify column created_at datetime default current_timestamp;

It is showing the following error 它显示以下错误

Error Code : 1067 Invalid default value for 'updated_at' (0 ms taken) 错误代码:1067“updated_at”的默认值无效(0 ms)

Error Code : 1067 Invalid default value for 'created_at' (0 ms taken) 错误代码:1067“created_at”的默认值无效(0 ms)

My mysql version is 5.5.41-0ubuntu0.14.04.1 (Ubuntu) 我的mysql版本是5.5.41-0ubuntu0.14.04.1(Ubuntu)

It is hard to reference documentation in a comment: 在评论中很难引用文档

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). 从MySQL 5.6.5开始, TIMESTAMPDATETIME列可以自动初始化并更新为当前日期和时间(即当前时间戳)。 Before 5.6.5, this is true only for TIMESTAMP , and for at most one TIMESTAMP column per table. 在5.6.5之前,这仅适用于TIMESTAMP ,并且每个表最多只有一个TIMESTAMP列。

Either upgrade to 5.6.5. 要么升级到5.6.5。 Or use TIMESTAMP instead of DATETIME . 或者使用TIMESTAMP而不是DATETIME

Use TIMESTAMP instead of DATETIME 使用TIMESTAMP而不是DATETIME


Should be something like this: 应该是这样的:

ALTER TABLE nodestatics MODIFY COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

ALTER TABLE nodestatics MODIFY COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;


Or you can use TRIGGERS . 或者你可以使用TRIGGERS

CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
    FOR EACH ROW SET NEW.dateAdded = NOW();

You can look at similar topic . 你可以看一下类似的话题 Happy coding! 快乐的编码!

尝试这个

alter table nodestatics modify column created_at timestamp default current_timestamp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM