问题重现

在写这篇文章之前,明确我的MySQL版本,MariaDB 或者你使用 MySQL 8 也会出现如下问题

MySQL 版本

现在有这样的需求,一张表中有一个字段created_at记录创建该条记录的时间戳,另一个字段updated_at记录更新该条记录的时间戳。
我们尝试创建以下语句。

CREATE TABLE temp
(
id INT() PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(),
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

执行后报错:

报ERROR 1293 (HY000)错误。(完整错误信息:ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause)

解决方案一

第一种,created_at 使用 DEFAULT CURRENT_TIMESTAMP 或者 DEFAULT now(),而 updated_at 使用触发器。

CREATE TABLE temp
(
id INT() PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(),
created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NULL
);

在temp上创建触发器,实现更新时记录更新时间

delimiter |
DROP TRIGGER IF EXISTS tri_temp_updated_at;
CREATE TRIGGER tri_temp_updated_at BEFORE UPDATE ON temp
FOR EACH ROW
BEGIN
SET NEW.updated_at = now();
END;
|
delimiter ;

解决方案二

第二种,created_at使用触发器,updated_at使用DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP或者DEFAULT now() ON UPDATE now();

CREATE TABLE temp
(
id INT() PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(),
created_at timestamp NULL,
updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

在temp上创建触发器,实现插入数据记录创建时间

delimiter |
DROP TRIGGER IF EXISTS tri_temp_created_at;
CREATE TRIGGER tri_temp_created_at BEFORE INSERT ON temp
FOR EACH ROW
BEGIN
IF new.created_at IS NULL
THEN
SET new.created_at=now();
END IF;
END;
|
delimiter ;

解决方案三

第三种,created_at 指定 timestamp DEFAULT ‘0000-00-00 00:00:00’,updated_at 指定 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 或者 timestamp DEFAULT now() ON UPDATE now();

CREATE TABLE temp
(
id INT() PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(),
created_at timestamp NULL DEFAULT '0000-00-00 00:00:00',
updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

插入数据:

mysql> INSERT INTO temp(name,created_at,updated_at) \
VALUES('robin',now(),now());
Query OK, row affected (0.01 sec) mysql> INSERT INTO temp(name,created_at,updated_at) \
VALUES('wentasy',now(),now());
Query OK, row affected (0.01 sec)

解决方案四

第四种,更换MySQL版本,MySQL 5.6已经去除了此限制。

我们可以看下MySQL 5.5和5.6帮助文档对于这个问题的解释。

MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法的更多相关文章

  1. Mysql运行SQL文件 错误Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    问题描述 想从服务器上DOWN下数据库.操作:先把数据库转存为SQL文件,然后在本地利用navicate运行SQL文件,出现错误信息: Incorrect table definition;there ...

  2. Mysql [Err] 1293 there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    问题: mysql数据 导入数据出错 [Err] 1293 - Incorrect table definition; there can be only one TIMESTAMP column w ...

  3. msyql同步的时候报错 : 错误代码: 1293 Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    场景,两个不同服务器上的数据库,进行数据库同步 但是执行之后,提示报错 错误代码: 1293 Incorrect table definition; there can be only one TIM ...

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

    在数据库执行脚本文件时,执行到一半会遇到  这种问题: 出错处:2018-05-14 18:53:38 行号:712454 错误代码: 1293 - Incorrect table definitio ...

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

    错误描述 在DBeaver执行DDL语句时报错:SQL 错误 [1293] [HY000]: Incorrect table definition; there can be only one TIM ...

  6. mysql单表多timestamp报错#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到#1293 - Incorrect table definition; there can be o ...

  7. there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    建表语句: create table test_table(   id integer not null auto_increment primary key,   stamp_created tim ...

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

    我使用的5.5的mysql数据库会报这个错, 换成5.7的就可以了

  9. MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析

    在写这篇文章之前.明白我的MySQL版本号. mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5 ...

随机推荐

  1. linux ssh_config和sshd_config配置文件学习

    在远程管理linux系统基本上都要使用到ssh,原因很简单:telnet.FTP等传输方式是‍以明文传送用户认证信息,本质上是不安全的,存在被网络窃听的危险.SSH(Secure Shell)目前较可 ...

  2. ICS2019汇编实验在Linux下使用GDB调试程序

  3. ASP.NET 中TextBox设置ReadOnly="true" 无法取到值的做法

    当 TextBox设置了ReadOnly="true" 后,要是在前台为控件添加了值,后台是取不到的,值为“空” 原理没想通,说不清楚微软是出于什么考虑的,https://www. ...

  4. jenkins+ant+jmeter接口自动化的持续集成

    一.jmeter.jenkins安装 这里不再说明,请看上一个随笔!!! 链接:https://www.cnblogs.com/magicYJ/p/11839646.html 二.ant安装 下载地址 ...

  5. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  6. CF: Long Number

                                                    题目链接 #include<iostream> #include<string> ...

  7. 《AlwaysRun!》第七次作业:团队项目设计完善&编码

    项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nwnu-daizh/   这个作业的要求在哪里 https://www.cnblogs.com/nwnu-da ...

  8. epoll版http服务器

    epoll是事件通知方式接收数据,效率比轮询要高 代码: import socket import re import select def client_server(new_client,recv ...

  9. 备份docker运行的gitlab

    #!/bin/bash data=$(date "+%Y-%m-%d %H:%M:%S") gitBak='/data/gitlab/data/backups' delFile=` ...

  10. linux下MySQL的启动与访问

    启动与停止 1.启动 MySQL安装完成后启动文件mysql在/etc/init.d目录下,在需要启动时运行下面命令即可. [root@test1 init.d]# /etc/init.d/mysql ...