在使用SQLite更新current_timestamp时 [英] on update current_timestamp with SQLite

查看:514
本文介绍了在使用SQLite更新current_timestamp时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每行更新时,我都希望使用当前时间戳更新字段。

I want to update a field with the current timestamp whenever the row is updated.

在MySQL中,我会在声明表时这样做

In MySQL I would do, when declaring the table

LastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP

但是更新时部分不适用于SQLite。
我找不到自动执行此操作的方法,需要声明触发器吗?

But the "on update" part does not work with SQLite. I could not find a way to do it automatically, do I need to declare a trigger?

编辑:进行记录,这是我当前的触发器:

EDIT: For the record, here is my current trigger:

CREATE TRIGGER [UpdateLastTime]
AFTER UPDATE
ON Package
FOR EACH ROW
BEGIN
UPDATE Package SET LastUpdate = CURRENT_TIMESTAMP WHERE ActionId = old.ActionId;
END

谢谢

推荐答案

是的,您需要使用触发器。 (只需检查:您发布的触发器是否正常工作?乍看之下对我来说还不错。)

Yes, you'd need to use a trigger. (Just checking: is your posted trigger working correctly? At first glance, it looks fine to me.)

MySQL的 ON UPDATE CURRENT_TIMESTAMP 是一个非常独特的单用途快捷方式。就是这样;此构造 不能类似地用于 TIMESTAMP 以外的任何其他值或任何列类型。 (请注意如何在 TIMESTAMP 键入页面,而不是 创建表 code>页面,因为此功能特定于 TIMESTAMP 列而不是 CREATE TABLE 语句通常。)还值得一提的是,虽然它特定于 TIMESTAMP 类型,但 SQLite甚至没有不同的日期/时间类型

MySQL's ON UPDATE CURRENT_TIMESTAMP is a pretty unique, single-purpose shortcut. It is what it is; this construct cannot be used similarly for any other values or for any column types other than TIMESTAMP. (Note how this functionality is defined on the TIMESTAMP type page instead of the CREATE TABLE page, as this functionality is specific to TIMESTAMP columns and not CREATE TABLE statements in general.) It's also worth mentioning that while it's specific to a TIMESTAMP type, SQLite doesn't even have distinct date/time types.

据我所知,没有其他RDBMS提供此快捷方式来代替使用实际触发器。根据我的阅读,必须使用触发器在MS SQL,SQLite,PostgreSQL和Oracle上完成此操作。

As far as I know, no other RDBMS offers this shortcut in lieu of using an actual trigger. From what I've read, triggers must be used to accomplish this on MS SQL, SQLite, PostgreSQL, and Oracle.

给路人的最后一封信:

这不要与 ON UPDATE 与外键约束有关的子句。那是完全不同的,可能所有支持外键约束的RDBMS都具有(包括MySQL和SQLite)。

This is not to be confused with ON UPDATE clauses in relation to foreign key constraints. That's something entirely different, which likely all RDBMSs that support foreign key constraints have (including both MySQL and SQLite).

这篇关于在使用SQLite更新current_timestamp时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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