如何在创建表时的列上添加“ON UPDATE CURRENT_TIMESTAMP”,或者通过alter table withoud定义其默认值

时间:2016-12-20 08:35:47

标签: mysql

我正在使用以下查询来更改表并在列上添加ON UPDATE CURRENT_TIMESTAMP

ALTER TABLE my_table CHANGE column_name column_name TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP // can I remove this line
ON UPDATE CURRENT_TIMESTAMP;

从上面的查询中,我可以将ON UPDATE CURRENT_TIMESTAMP放在列而不定义其默认值吗?

当我尝试删除该部分时,查询将如下:

alter table new_table CHANGE updated_at updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

并显示错误: alter table new_table CHANGE updated_at updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP错误代码:1067。'updated_at'的默认值无效

1 个答案:

答案 0 :(得分:3)

ALTER TABLE t1
  MODIFY COLUMN c1
  TIMESTAMP
  NULL -- the opposite is NOT NULL, which is implicitly set on timestamp columns
  DEFAULT NULL -- no default value for newly-inserted rows
  ON UPDATE CURRENT_TIMESTAMP;