如何添加"ON update current timestamp".到现有表列 [英] How to add "ON update current timestamp" to existing table column

查看:305
本文介绍了如何添加"ON update current timestamp".到现有表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有列ts_activity的数据类型为timestamp,默认值为当前时间戳.我想添加更新,将当前时间戳应用于此列值.

i have column say ts_activity with datatype timestamp in MYSQL and default value to current timestamp. I want to add on update apply current time stamp to this column value.

我无法获取更改查询来执行此操作.在创建新表及其列时,我可以添加该列,但无法通过在更新时添加来修改现有列,以应用当前时间戳.

I am not able to get alter query to do this. while create new table and its column i can add that, But not able to modify existing column by adding on update apply current timestamp.

有人可以告诉我确切的查询来解决这个问题.创建表格后,我们可以更改它吗?

Can some one tell me exact query to handle this. Can we alter this after creating table.

谢谢

推荐答案

Devart的建议会起作用,但是如果我不重命名列,我更喜欢使用MODIFY COLUMN而不是CHANGE COLUMN.

Devart's suggestion will work, but I prefer to use MODIFY COLUMN instead of CHANGE COLUMN if I'm not renaming the column.

我还假设您的ts_activity列不可为空,因为您具有默认值,因此我将其设置为NOT NULL,但这取决于您.

I also assume that your ts_activity column is not nullable since you have a default, so I am setting it to NOT NULL, but that's up to you.

这是我要使用的语句:

ALTER TABLE your_table
  MODIFY COLUMN ts_activity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

这篇关于如何添加"ON update current timestamp".到现有表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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