2

私はこれらのテーブルを作成しようとしています:

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'
4

1 に答える 1

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?のスレッドもチェックすることをお勧めします。- これには多くのコメントがあります。

于 2012-06-12T21:22:37.350 に答える