我有一个表,其中有一列类型为 timestamp
,默认为 current_timestamp
并在每次更新时更新为 current_timestamp
。
我想删除此列的“更新时”功能。如何编写 alter 语句?
我尝试了以下方法:
ALTER TABLE mytable alter column time set DEFAULT now();
但这不起作用。
最佳答案
Pete 几乎是正确的,但“更改”使用了错误的语法:
ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
请注意,您必须重复列名。此外,请确保您使用反引号而不是单引号来转义列名时间,这样可以防止将其解释为 mysql 列类型的时间。
通过指定 CURRENT_TIMESTAMP 的 DEFAULT,MySQL 将不再自动更新该列。来自 MySQL Manual :
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
https://stackoverflow.com/questions/1770203/