MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法
问题重现
在写这篇文章之前,明确我的MySQL版本,MariaDB 或者你使用 MySQL 8 也会出现如下问题

现在有这样的需求,一张表中有一个字段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同时创建多个更新当前时间戳字段 解决方法的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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的就可以了
- MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析
在写这篇文章之前.明白我的MySQL版本号. mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5 ...
随机推荐
- 1.后期特效合成AE概述&&工作流程&&磁盘缓存清理
1.简介: After Effects:是一款专业的后期制作与特效合成软件: 2.AE界面介绍 2.1 项目面板 2.2 合成预览面板 2.3 时间线面板 2.4 辅助面板 2 ...
- 剑指offer:二维数组的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- 【Python】编程小白的第一本python(最基本的魔法函数)
Python官网中各个函数介绍的链接:https://docs.python.org/3/library/functions.html 几个常见的词: def (即 define,定义)的含义是创建函 ...
- Mysql存储引擎中InnoDB与Myisam的区别
1. 事务处理innodb 支持事务功能,myisam 不支持.Myisam 的执行速度更快,性能更好. 2. select ,update ,insert ,delete 操作MyISAM:如果执行 ...
- [CSS] Create a Card Flip Animation with CSS
Animation can be a powerful way to enhance a user experience. In this lesson, we'll walk through the ...
- grpc提供http服务
package main import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials&qu ...
- javaweb学习笔记(二)
一.javaweb学习是所需要的细节 1.Cookie的注意点 ① Cookie一旦创建,它的名称就不能更改,Cookie的值可以为任意值,创建后允许被修改. ② 关于Cookie中的setMaxAg ...
- mongodb 集群配置文件
本文档是在mongodb为3.4下编写的,仅作为参考,详细内容请参考:https://docs.mongodb.com/manual/reference/configuration-options/# ...
- NOIP(划掉)CSP2019一轮知识点
今年似乎变动很大呢…… 去年总结的 历年真题 以下标题中打*的是我认为的重点内容 *一.关于计算机 (一)计算机组成 计算机的工作原理跟人的大脑很相似,而且还是大脑功能的延伸,所以习惯上叫它电脑. 硬 ...
- OSPF区域间+NAT详解