MYSQL错误TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE的解决方案

在我的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.5版本,而在5.6.5 之后mysql做了如下的变化:

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.


原先建表语句是:

DROP TABLE IF EXISTS `activity`;

CREATE TABLE `activity` (
  `activity_id` int(11) NOT NULL AUTO_INCREMENT,
  `activity_name` varchar(100) NOT NULL,
  `activity_starttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `activity_endtime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `type_limt` int(11) DEFAULT '1',
  `time_limt` int(11) DEFAULT '0',
  `activity_detail` varchar(1000) DEFAULT NULL,
  `admin_id` int(11) NOT NULL,
  `activity_creattime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`activity_id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

通过触发器替代之后为:

CREATE TABLE `activity` (
  `activity_id` INT(11) NOT NULL AUTO_INCREMENT,
  `activity_name` VARCHAR(100) NOT NULL,
  `activity_starttime` DATETIME NOT NULL,
  `activity_endtime` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
  `type_limt` INT(11) DEFAULT '1',
  `time_limt` INT(11) DEFAULT '0',
  `activity_detail` VARCHAR(1000) DEFAULT NULL,
  `admin_id` INT(11) NOT NULL,
  `activity_creattime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`activity_id`)
) ENGINE=MYISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
DROP TRIGGER IF EXISTS `update_activity_trigger`;
DELIMITER //
CREATE TRIGGER `update_activity_trigger` BEFORE UPDATE ON `activity`
 FOR EACH ROW SET NEW.`activity_starttime` = NOW()
//
DELIMITER ;

之后再执行发现没有问题了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值