MySQL(DEFAULT + ON UPDATE)TIMESTAMPs [英] MySQL (DEFAULT + ON UPDATE) TIMESTAMPs

查看:159
本文介绍了MySQL(DEFAULT + ON UPDATE)TIMESTAMPs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中有两个字段:

  dateCreated 
dateUpdated

我希望两个字段都保存时间戳,dateCreated具有DEFAULT CURRENT TIMESTAMP标志,dateUpdated具有ON UPDATE CURRENT TIMESTAMP行为,但是似乎我不能在同一个表上有多个时间戳字段类型。



如果数据库可以为我照顾这一点,这将是非常有用的


b b b b b b b b p>您可以使用 触发器 更新字段,如下所示:

 创建触发器CreationTimestamp插入MyTable每行
begin
update MyTable set CreateField = UpdateField where id = new.id
end

这样,您可以使用本机MySQL方法设置自动更新字段,并使用此方法捕获创建日期。


I've a table where I've two fields:

dateCreated
dateUpdated

I want both fields to hold timestamps, dateCreated to have a DEFAULT CURRENT TIMESTAMP flag and dateUpdated to have a ON UPDATE CURRENT TIMESTAMP behavior, however seems that I can't have more than one timestamps field types on the same table.

It would be really useful if the database could take care of this for me, can I circumvent this issue somehow?

解决方案

YES WE CAN.

You can use a trigger to update the field, like so:

create trigger CreationTimestamp after insert on MyTable for each row
begin
    update MyTable set CreateField = UpdateField where id = new.id
end

This way, you can set your auto-update field using the native MySQL method, and use this to capture the creation date.

这篇关于MySQL(DEFAULT + ON UPDATE)TIMESTAMPs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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