Mysql应用MySQL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法

Mysql应用MySQL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法,第1张

概述介绍《Mysql应用MySQL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法》开发教程,希望对您有用。

《MysqL应用MysqL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法》要点:
本文介绍了MysqL应用MysqL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法,希望对您有用。如果有疑问,可以联系我们。

在部署程序时遇到的一个问题,MysqL定义举例如下:
MysqL教程

代码如下:
CREATE table `example` (
  `ID` INTEGER UNSIGNED NOT NulL auto_INCREMENT,
  `created` TIMESTAMP NOT NulL DEFAulT CURRENT_TIMESTAMP,
  `lastUpdated` TIMESTAMP NOT NulL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB;
这段sql是我从项目中摘取出来的,在测试机器上一切正常,但是部署到生产机器上MysqL报错:
代码如下:
ERROR 1293 (HY000): Incorrect table deFinition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAulT or ON UPDATE clause.

意思是只能有一个带CURRENT_TIMESTAMP的timestamp列存在,但是为什么本地测试却没有任何问题呢,本地测试的机器安装的MysqL版本5.6.13,而生产机器上安装的却是5.5版本,搜索网络后得知这两种版本之间对于timestamp处理的区别在于:MysqL教程


在MysqL 5.5文档有这么一段话:
MysqL教程

代码如下:
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column,as the auto-update value,or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
而在MysqL 5.6.5做出了以下改变:
代码如下:
PrevIoUsly,at most one TIMESTAMP column per table Could be automatically initialized or updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column deFinition can have any combination of DEFAulT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. In addition,these clauses Now can be used with DATETIME column deFinitions. For more information,see automatic Initialization and Updating for TIMESTAMP and DATETIME.
根据网上的办理方案,可以使用触发器来替代一下:
代码如下:
CREATE table `example` (
  `ID` INTEGER UNSIGNED NOT NulL auto_INCREMENT,
  `lastUpdated` DATETIME NOT NulL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB;
DROP TRIGGER IF EXISTS `update_example_trigger`;
DEliMITER //
CREATE TRIGGER `update_example_trigger` BEFORE UPDATE ON `example`
 FOR EACH ROW SET NEW.`lastUpdated` = Now()
//
DEliMITER ;

欢迎参与《MysqL应用MysqL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法》讨论,分享您的想法,内存溢出PHP学院为您提供专业教程。

总结

以上是内存溢出为你收集整理的Mysql应用MySQL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法全部内容,希望文章能够帮你解决Mysql应用MySQL错误TIMESTAMP column with CURRENT_TIMESTAMP的解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1153509.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存