I have a table with a column of type timestamp which defaults current_timestamp and updates to current_timestamp on every update.

I want to remove the "on update" feature on this column. How do I write the alter statement?

I tried the following:

ALTER TABLE mytable alter column time  set DEFAULT now();

but this didn't work.

推荐答案

Pete was almost correct but used the wrong syntax for 'change':

ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

Notice that you must repeat the column name. Also, make sure you are using backticks instead of single quotes to escape the column name time, which prevents it from being interpreted as the mysql column type of time.

By specifying the DEFAULT of CURRENT_TIMESTAMP, MySQL will no longer automatically update the column. From the 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.

Mysql相关问答推荐

是否有一种方法可以在mysql中设置一列,使其自动成为该行的行号的值?

错误:数据包顺序错误.GET:0预期:10 node JS MySQL

在时间戳上搜索大的MySQL表很慢

比较2个mysql json数据类型列

根据现有行 Select 月份日期

MySQL 计数出现的百分比大于

查找表中的唯一记录

根据名称将值从一个字段复制到不同记录的同一字段

如何在 SQL 中迭代所有名称和排名排列

如何使用sql查询在日期列中按值和最大值分组的表中添加列?

Mysql,显示谁已经和没有 Select 退出巴士服务

Mysql Pivot 查询/建议

相关查询 - 在派生表中,我可以有多个嵌套级别吗?

如何知道 Select 查询花费了多少时间?

WHERE SQL 语句中的列顺序是否重要

这是将表单数据插入 MySQL 数据库的安全方法吗?

是否可以在内部连接期间重命名连接列?

#1146 - 表 'phpmyadmin.pma_recent' 不存在

为什么 MySQL 自动增量会在插入失败时增加?

什么是mysql的BETWEEN性能超过..?