【问题标题】:DATETIME Default value 0 MYSQL 5.5.25DATETIME 默认值 0 MYSQL 5.5.25
【发布时间】:2012-06-12 21:10:33
【问题描述】:

我正在尝试创建这些表:

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'

【问题讨论】:

标签: mysql sql datetime date


【解决方案1】:

你可以使用:

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? 上的帖子 - 上面有很多 cmets。

【讨论】:

  • 有什么办法可以设置为0?
  • 嗯 - 0 并不是真正的日期 - 但您可以选择一些任意日期并将其用作您的“零点” - 例如 1970 年 1 月 1 日或其他日期。不过,一个好的设计会将 NULL 用作 0。
  • 如果您的 SQL 模式不包括 NO_ZERO_DATE (dev.mysql.com/doc/refman/5.5/en/…),您可以 使用 discount_date DATETIME NOT NULL DEFAULT '0000-00-00',但正如 Kerbocat 建议的那样,NULL 绝对是更可取的。
猜你喜欢
  • 2013-12-08
  • 1970-01-01
  • 2020-10-23
  • 2015-03-02
  • 1970-01-01
相关资源
最近更新 更多