MySQL不允许在DATETIME字段中使用ON UPDATE CURRENT_TIMESTAMP [英] MySQL is not allowing ON UPDATE CURRENT_TIMESTAMP for a DATETIME field
问题描述
我有一个MySQL表与TIMESTAMP(对于该字段已创建)和DATETIME(每次该字段更新时)。它看起来像这样:
CREATE TABLE'vis'(
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
ENTRY VARCHAR(255),
AUTHOR VARCHAR(255),
CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT DATETIME ON UPDATE CURRENT_TIMESTAMP,
UPDATED_BY VARCHAR(255)
)
当我尝试这个时,我得到的错误是:(SQL错误:1294 SQL状态:HY000) - 'updated_at'字段的ON UPDATE子句无效
我读过的任何地方(即使是堆栈溢出)建议我应该能够做到这一点,但我得到这个错误。也许还有一种方法可以让每次更新时自动更新时间的字段?
我正在使用MySQL Server 5.5。
DATETIME
不能在更新时使用 CURRENT_TIMESTAMP
而是将其更改为 TIMESTAMP
。
或者,考虑对这种情况使用触发器: http://dev.mysql.com/doc/refman/5.0/en/trigger -syntax.html
I have seen a lot of related questions, but I cannot place my finger on this specific question:
I have a MySQL table with both a TIMESTAMP (for when the field was created) and a DATETIME (for each time the field gets updated). It looks like this:
CREATE TABLE 'vis' (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
ENTRY VARCHAR(255),
AUTHOR VARCHAR(255),
CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT DATETIME ON UPDATE CURRENT_TIMESTAMP,
UPDATED_BY VARCHAR(255)
)
When I try this though, the error I am getting is: (SQL Error: 1294 SQL State: HY000) - Invalid ON UPDATE clause for 'updated_at' field
Everywhere I have read (even on Stack Overflow) suggests I should be able to do this, yet I am getting this error. Perhaps there is another way to have a field that automatically updates the time each time I update it?
I am using MySQL Server 5.5.
DATETIME
cannot use CURRENT_TIMESTAMP
on update. Instead, change it to a TIMESTAMP
.
Or, consider using a trigger for this situation: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
这篇关于MySQL不允许在DATETIME字段中使用ON UPDATE CURRENT_TIMESTAMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!