我想要一个"lastmodified"时间戳(或者datetime?不确定除了数据的显示之外,它是否会产生差异)来记录该记录条目的最后修改日期/时间.
显然,这是可能的使用触发器.因为我以前没有使用过触发器,所以我想我可以先try 一下"更新规则",因为这对我来说也是新的:
http://www.postgresql.org/docs/8.3/static/rules-update.html
我有一张表记录客户的会话数据:
CREATE TABLE customer_session (
customer_sessionid serial PRIMARY KEY,
savedsearch_contents text,
lastmodified timestamp default now()
); /*
@ lastmodified - should be updated whenever the table is updated for this entry, just for reference.
*/
然后我可以创建一个这样的规则.我不确定语法,也不确定是使用新的还是旧的.有人能告诉我正确的语法吗?
CREATE RULE customer_session_lastmodified AS
ON UPDATE TO customer_session
DO UPDATE customer_session SET lastmodified = current_timestamp WHERE customer_sessionid = NEW.customer_sessionid
正如你所看到的,我只想更新customer_sessionid的上次修改条目,所以我不确定如何引用它.更新查询如下所示:
UPDATE customer_session SET savedsearch_contents = 'abcde'
WHERE customer_sessionid = {unique customer ID}
非常感谢!