MYSQL查询改变时间戳列

时间:2017-03-19 21:47:51

标签: php mysql

MYSQL VERSION - 5.7.14

在mysql版本中,我无法在插入查询中插入null的时间戳值。

我尝试使用以下查询更改mysql中的timestamp coulmn:

ALTER TABLE `business` MODIFY `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
ALTER TABLE `business` MODIFY `modified` TIMESTAMP on update CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;

但是我收到了错误 #1067 - Invalid default value for 'modified'

1 个答案:

答案 0 :(得分:0)

这应该有效:

// TO ALLOW NULL AND SET CURRENT_TIMESTAMP AS DEFAULT:
ALTER TABLE `business` MODIFY `created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP;
// TO ALLOW NULL AND SET CURRENT_TIMESTAMP AS DEFAULT AND ON UPDATE:  
ALTER TABLE `business` MODIFY `modified` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

如果您不想允许null,则只需删除NULL

// DO NOT ALLOW NULL, SET CURRENT_TIMESTAMP AS DEFAULT:
ALTER TABLE `business` MODIFY `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
// DO NOT ALLOW NULL, SET CURRENT_TIMESTAMP AS DEFAULT AND ON UPDATE:  
ALTER TABLE `business` MODIFY `modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;