如何更改 mysql 表列默认值?
我有一个表,其中有一列 timestamp
类型,默认值 current_timestamp
并在每次更新时更新为 current_timestamp
。
我想删除此列上的“更新时”功能。怎样写alter语句呢?
我尝试了以下操作:
ALTER TABLE mytable alter column time set DEFAULT now();
但这不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pete 几乎是正确的,但使用了错误的“更改”语法:
请注意,您必须重复列名称。另外,请确保使用反引号而不是单引号来转义列名时间,这可以防止它被解释为 mysql 列类型的时间。
通过指定CURRENT_TIMESTAMP的DEFAULT,MySQL将不再自动更新该列。来自 MySQL 手册:
Pete was almost correct but used the wrong syntax for 'change':
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:
AFAIK 不能使用 NOW() 等函数作为默认函数。
尝试
(编辑以添加转义和再次使用字段名称)
You can't AFAIK use functions such as NOW() as a default.
Try
(Edited to add escaping and second use of field name)