MySQL 4.0 中同时具有“创建”和“上次更新”时间戳列
我有以下表架构;
CREATE TABLE `db1`.`sms_queue` (
`Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`Message` VARCHAR(160) NOT NULL DEFAULT 'Unknown Message Error',
`CurrentState` VARCHAR(10) NOT NULL DEFAULT 'None',
`Phone` VARCHAR(14) DEFAULT NULL,
`Created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`TriesLeft` tinyint NOT NULL DEFAULT 3,
PRIMARY KEY (`Id`)
)
ENGINE = InnoDB;
它失败并出现以下错误:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
我的问题是,我可以同时拥有这两个字段吗? 或者我是否必须在每次事务期间手动设置 LastUpdated 字段?
I have the following table schema;
CREATE TABLE `db1`.`sms_queue` (
`Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`Message` VARCHAR(160) NOT NULL DEFAULT 'Unknown Message Error',
`CurrentState` VARCHAR(10) NOT NULL DEFAULT 'None',
`Phone` VARCHAR(14) DEFAULT NULL,
`Created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`TriesLeft` tinyint NOT NULL DEFAULT 3,
PRIMARY KEY (`Id`)
)
ENGINE = InnoDB;
It fails with the following error:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
My question is, can I have both of those fields? or do I have to manually set a LastUpdated field during each transaction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
来自 MySQL 5.5 文档:
MySQL 5.6.5 中的更改:
From the MySQL 5.5 documentation:
Changes in MySQL 5.6.5:
有一个技巧可以同时拥有两个时间戳,但是有一点限制。
您只能在一张表中使用其中一个定义。 像这样创建两个时间戳列:
请注意,在
insert
期间必须在两列中输入null
:There is a trick to have both timestamps, but with a little limitation.
You can use only one of the definitions in one table. Create both timestamp columns like so:
Note that it is necessary to enter
null
into both columns duringinsert
:您可以同时拥有它们,只需去掉创建字段上的“CURRENT_TIMESTAMP”标志即可。 每当您在表中创建新记录时,只需使用“NOW()”作为值。
或者。
相反,删除“ON UPDATE CURRENT_TIMESTAMP”标志并发送该字段的 NOW()。 这种方式实际上更有意义。
You can have them both, just take off the "CURRENT_TIMESTAMP" flag on the created field. Whenever you create a new record in the table, just use "NOW()" for a value.
Or.
On the contrary, remove the 'ON UPDATE CURRENT_TIMESTAMP' flag and send the NOW() for that field. That way actually makes more sense.
如果您决定让 MySQL 处理时间戳更新,您可以设置一个触发器来更新插入时的字段。
MySQL 参考: http://dev.mysql.com/doc/refman/ 5.0/en/triggers.html
If you do decide to have MySQL handle the update of timestamps, you can set up a trigger to update the field on insert.
MySQL Reference: http://dev.mysql.com/doc/refman/5.0/en/triggers.html
这就是你如何拥有自动& 使用触发器灵活的createDate/lastModified字段:
首先像这样定义它们:
然后添加这些触发器:
但这里有一个很好的部分:
This is how can you have automatic & flexible createDate/lastModified fields using triggers:
First define them like this:
Then add these triggers:
But here's the nice part:
从 MySQL 5.6 开始,它变得简单...尝试一下:
As of MySQL 5.6 its easy-peasy... give it a try:
这个问题似乎在 MySQL 5.6 中已经得到解决。 我在 MySQL 5.5 之前就注意到了这一点; 这是一个示例代码:
在 MySQL 5.5 上运行此代码给出:
在 MySQL 5.6 上运行此代码
This issue seemed to have been resolved in MySQL 5.6. I have noticed this until MySQL 5.5; here is an example code:
Running this on MySQL 5.5 gives:
Running this on MySQL 5.6
来源:http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql /
source: http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql/
我认为这是对 stamp_created 和 stamp_updated 更好的查询,
因为创建记录时,
stamp_created
应该由now()
填充,而stamp_updated
应该是由'0000-00-00 00:00:00'
填充i think this is the better query for stamp_created and stamp_updated
because when the record created,
stamp_created
should be filled bynow()
andstamp_updated
should be filled by'0000-00-00 00:00:00'
这将添加两列用于创建和更新。
两者都会在插入和更新时更新。
this will add two column for creation and updation.
both will get updated while inserting and updating.
对于 mysql 5.7.21,我使用以下命令并且工作正常:
For mysql 5.7.21 I use the following and works fine:
我的网络主机停留在 mysql 5.1 版本上,因此像我这样无法升级的人可以按照以下说明操作:
http://joegornick.com/2009/12/30/mysql-created-and-modified-date-fields/
My web host is stuck on version 5.1 of mysql so anyone like me that doesn't have the option of upgrading can follow these directions:
http://joegornick.com/2009/12/30/mysql-created-and-modified-date-fields/