[Err] [Dtf] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_T

本文介绍了一种在从高版本MySQL导出数据到较低版本MySQL过程中遇到的TIMESTAMP字段定义冲突问题及其解决方案。通过更改其中一个TIMESTAMP字段为DATETIME类型,可以避免因版本差异导致的错误。

将远端数据库的数据传输到本地时候出现了这样的错误

[Err] [Dtf] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 

 


发生错误的表:

 create table 'order_master' (    
'order_id' varchar(32) not null,    
'buyer_name' varchar(32) not null comment '买家名字',    
'buyer_phone' varchar(32) not null comment '买家电话',    
'buyer_address' varchar(128) not null comment '买家地址',    
'buyer_openid' varchar(64) not null comment '买家微信openid',    
'order_amount' decimal(8,2) not null comment '订单总金额',    
'order_status' tinyint(3) not null default '0' comment '订单状态,默认0新订单'    
'pay_status' tinyint(3) not null default '0' comment '订单状态,默认0未支付'    
'create_time' timestamp not null default current_timestamp comment '创建时间',    
'update_time' timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    
primary key ('order_id'),    
key 'idx_buyer_openid' ('buyer_openid') 
) comment '订单表';

查了相关资料才知道 MySql 5.5和MySql 5.6之后版本的区别:5.5 只能有一个Timestamp,将其中一列类型改为datetime类型就可以解决。

远端mysql版本为5.7.17

本地mysql版本为5.5.49

 

解决方案:

1、备份远程数据转存为sql文件,将其中一列类型Timestamp改为datetime,比如上述错误表改为:

 create table 'order_master' (    
'order_id' varchar(32) not null,    
'buyer_name' varchar(32) not null comment '买家名字',    
'buyer_phone' varchar(32) not null comment '买家电话',    
'buyer_address' varchar(128) not null comment '买家地址',    
'buyer_openid' varchar(64) not null comment '买家微信openid',    
'order_amount' decimal(8,2) not null comment '订单总金额',    
'order_status' tinyint(3) not null default '0' comment '订单状态,默认0新订单'    
'pay_status' tinyint(3) not null default '0' comment '订单状态,默认0未支付'    
'create_time' datetime not null comment '创建时间',    
'update_time' timestamp not null default current_timestamp on update current_timestamp comment '修改时间',    
primary key ('order_id'),    
key 'idx_buyer_openid' ('buyer_openid') 
) comment '订单表';

2、本地数据库加载修改后的sql文件。

### 解决 MySQL 时间戳列定义错误 在处理 `ERROR 1293` 错误时,该错误提示表定义不正确,仅允许一个 `TIMESTAMP` 列带有默认值 `CURRENT_TIMESTAMP` 和 `ON UPDATE CURRENT_TIMESTAMP` 属性。此限制是为了防止多个自动更新的时间戳字段引起的数据冲突。 当创建或修改表结构并尝试设置两个或更多 `TIMESTAMP` 字段都具有这些属性时会遇到这个问题。要解决这个错误,可以采取以下几种方法之一: #### 方法一:移除多余的 `DEFAULT CURRENT_TIMESTAMP` 如果不需要第二个时间戳字段自动生成当前时间,则可以从其定义中删除 `DEFAULT CURRENT_TIMESTAMP` 或者 `ON UPDATE CURRENT_TIMESTAMP` 属性。 ```sql ALTER TABLE your_table MODIFY created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP; ALTER TABLE your_table MODIFY updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; ``` 上述命令确保只有一个 `TIMESTAMP` 字段拥有 `DEFAULT CURRENT_TIMESTAMP` 和/或 `ON UPDATE CURRENT_TIMESTAMP` 特性[^1]。 #### 方法二:使用 `DATETIME` 类型代替另一个 `TIMESTAMP` 另一种解决方案是将其中一个时间戳字段更改为 `DATETIME` 数据类型,这不会受到相同约束的影响。 ```sql ALTER TABLE your_table MODIFY second_timestamp_column DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'; ``` 通过这种方式,可以在不影响现有功能的情况下绕过单个 `TIMESTAMP` 的限制。 #### 方法三:指定不同的默认值 还可以考虑给其他 `TIMESTAMP` 字段赋予特定的静态默认值而不是动态生成的时间戳。 ```sql ALTER TABLE your_table ADD COLUMN log_time TIMESTAMP NOT NULL DEFAULT '2023-01-01 00:00:01'; ``` 这种方法适用于那些确实需要额外的时间记录但是又不想让它们随每次更改而变化的情况。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值