如何更改mysql表列的默认值? [英] How do I alter a mysql table column defaults?

查看:185
本文介绍了如何更改mysql表列的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 timestamp 的列的表格,默认为 current_timestamp 并更新为 current_timestamp

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

我要删除此列上的更新时功能。我如何写alter语句?

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

我尝试了以下操作:

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

但不起作用。

推荐答案

Pete几乎是正确的,但是对'change'使用了错误的语法:

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

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

请注意,您必须重复列名称。此外,确保您使用反引号而不是单引号来转义列名称时间,从而防止将其解释为mysql列的时间类型。

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.

通过指定DEFAULT为CURRENT_TIMESTAMP,MySQL不再自动更新列。从 MySQL手册

By specifying the DEFAULT of CURRENT_TIMESTAMP, MySQL will no longer automatically update the column. From the MySQL Manual:


对于DEFAULT CURRENT_TIMESTAMP子句,并且没有ON UPDATE子句,该列具有其默认值的当前时间戳,但不会自动更新。

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表列的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆