DATETIME默认值0 MYSQL 5.5.25

时间:2012-06-12 21:10:33

标签: mysql sql datetime date

我正在尝试创建这些表:

CREATE  TABLE IF NOT EXISTS `qa_discountcoupons` (
  `discount_code` INT NOT NULL AUTO_INCREMENT ,
  `status_code` INT NOT NULL ,
  `discount_date` DATETIME NOT NULL DEFAULT 0 ,
  PRIMARY KEY (`discount_code`) ,
  INDEX `discounts_to_status` (`status_code` ASC) ,
  CONSTRAINT `discounts_to_status`
    FOREIGN KEY (`status_code` )
    REFERENCES `qa_status` (`status_code` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

但是我收到了这个错误:

Error Code: 1067. Invalid default value for 'discount_date'

1 个答案:

答案 0 :(得分:3)

您可以使用:

CREATE  TABLE IF NOT EXISTS `qa_discountcoupons` (
 `discount_code` INT NOT NULL AUTO_INCREMENT ,
 `status_code` INT NOT NULL ,
 `discount_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
 PRIMARY KEY (`discount_code`) ,
 INDEX `discounts_to_status` (`status_code` ASC) ,
 CONSTRAINT `discounts_to_status`
 FOREIGN KEY (`status_code` )
 REFERENCES `qa_status` (`status_code` )
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
ENGINE = InnoDB;

从MySQL 5.6.5开始。

我建议你查看How do you set a default value for a MySQL Datetime column?上的帖子 - 这里有很多评论。