如何在Mysql中使用触发器/否则存储创建记录的时间

时间:2011-03-02 09:33:29

标签: mysql triggers

我想自动将request_time存储为更新关键字的当前时间。

这是一个示例表:

id     request_time           keyword
1      2011-02-03 8:00:08     free

在插入free时,request_time应设为NOW();

这是我的触发器,但它不起作用:

delimiter //
create trigger ai_update_userinput after update on user_input 
for each row begin update user_input
set request_time = NOW(); end//

帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

即使没有触发器,也应该使用timestamp列。请查看http://dev.mysql.com/doc/refman/5.0/en/timestamp.html,特别是表创建中列规范的'on update'子句的示例。

答案 1 :(得分:0)

时间戳列已设置DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP时已提供此功能。请参阅the documentation