Home  >  Q&A  >  body text

ios - mysql中Invalid default value for 'created_at'问题

环境

mac + mysql

问题

在创建表的时候遇到了Invalid default value for 'created_at'的错误,不知道什么原因造成的。

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '1970-01-01 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

默认数据使用“0000-00-00 00:00:00”报错,使用“1970-01-01 00:00:00”也同样报错。

大家讲道理大家讲道理2742 days ago636

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 14:22:57

    First of all, thank you for your help. After a lot of effort, I finally found the solution:

    First use the following command to check sql_mode

    show variables like 'sql_mode';

    If the query results are as follows:

    ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_pISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION

    If the result contains NO_ZERO_IN_DATE, NO_ZERO_DATE
    , execute the following command:

    set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_pISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:22:57

    CREATE TABLE `test` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
      `created_at` timestamp NOT NULL DEFAULT 0,
      `updated_at` timestamp NOT NULL DEFAULT 0,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:22:57

    timestamp is a number

    1970-00-00 This is the date format

    Different types

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:22:57

    The character type timestamp has a range. If you want to use 0000-00-00 00:00:00, you can only use a string

    You can refer to this blog of mine: http://www.cnblogs.com/chenmh/p/4565986....

    reply
    0
  • Cancelreply