分享mysql的current

2021-11-25,

目录
  • mysql的current_timestamp小坑
    • 报错
    • 我的方案
  • mysql5.5.23 current_timestamp问题
    • 原版说明
    • 解决办法

mysql的current_timestamp小坑

报错

Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

网上的答案基本如下,但是就是没有具体解决

方案原因是当你给一个timestamp设置为on updatecurrent_timestamp的时候,其他的timestamp字段需要显式设定default值

但是如果你有两个timestamp字段,但是只把第一个设定为current_timestamp而第二个没有设定默认值,MySQL能成功建表,但是反过来就不行…

我的方案

卸载当前低版本的mysql,安装5.6以上版本的mysql,推荐mysql5.7版本相对稳定点。

mysql5.5.23 current_timestamp问题

今天新建一个表,里面两个字段:创建时间和更新时间,两个默认值都是当前时间,一执行sql就报错。

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

我在其它的项目里面都是这样用的sql,而且也能正常用,为什么今天执行不成功呢?在确认sql没有语法错误后,在网上查了一下资料。原来是mysql5.5在一个表内只支持一个时间字段使用CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP。但是在5.6里面支持多个。

原版说明

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版本号用 status命令

以上为个人经验,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

《分享mysql的current.doc》

下载本文的Word格式文档,以方便收藏与打印。