ios - mysql中Invalid default value for 'created_at'问题
大家讲道理
大家讲道理 2017-04-17 14:20:57
0
4
641

环境

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”也同样报错。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
迷茫

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'
迷茫
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;
洪涛

timestamp is a number

1970-00-00 This is the date format

Different types

伊谢尔伦

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....

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!