在mysql中创建表时,在新加的字段时间上,碰到下面的问题:
建表语句如下:
CREATE TABLE seckill( seckill_id bigint NOT NULL AUTO_INCREMENT COMMENT '商品库存id', name varchar (120) NOT NULL COMMENT '商品名称', number int NOT NULL COMMENT '库存数量', start_time timestamp NOT NULL COMMENT '秒杀开启时间', end_time timestamp NOT NULL COMMENT '秒杀结束时间', create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (seckill_id), key idx_start_time(start_time), key idx_end_time(end_time), key idx_create_time(create_time) )ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT='秒杀库存表';
执行上面的语句,回报错:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
我使用的mysql版本是5.5.经过查询得到原因:
Changes in MySQL 5.6.5 (2012-04-10, Milestone 8)
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.
意思是:timestamp没有加默认的值,建表语句改为:
CREATE TABLE seckill( seckill_id bigint NOT NULL AUTO_INCREMENT COMMENT '商品库存id', name varchar (120) NOT NULL COMMENT '商品名称', number int NOT NULL COMMENT '库存数量', start_time timestamp DEFAULT 0 NOT NULL COMMENT '秒杀开启时间', end_time timestamp DEFAULT 0 NOT NULL COMMENT '秒杀结束时间', create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (seckill_id), key idx_start_time(start_time), key idx_end_time(end_time), key idx_create_time(create_time) )ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT='秒杀库存表';
就可以创建成功了。