[英]TIMESTAMP default value error
I exported SQL script with workbench to phpMyAdmin and I get an error because of TIMESTAMP default value. 我将带有workbench的SQL脚本导出到phpMyAdmin,因为TIMESTAMP的默认值而出错。
Here's a piece of code: 这是一段代码:
CREATE TABLE IF NOT EXISTS `test`.`is_users` (
`count` INT(10) UNIQUE NOT NULL AUTO_INCREMENT,
`active` ENUM('1', '0') NULL DEFAULT 1,
`id` VARCHAR(36) UNIQUE NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:01',
`updated_at` TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:01',
`deleted_at` TIMESTAMP NULL DEFAULT NULL,
And I get an error : 我收到一个错误:
#1067 - Invalid default value for 'created_at'
#1067 - 'created_at'的默认值无效
To avoid an error, the default date should be for example 1970-01-01 01:01:01, it means that there cannot be zeros. 为避免错误,默认日期应为例如1970-01-01 01:01:01,这意味着不能有零。 How could I fix that?
我怎么能解决这个问题? Thanks.
谢谢。
Try 尝试
SELECT FROM_UNIXTIME(1);
If your system runs in another timezone than UTC then you will not get '1970-01-01 00:00:01' as result. 如果您的系统在UTC的另一个时区运行,那么您将无法获得“1970-01-01 00:00:01”。 For example, my system is running in CET which results in '1970-01-01 01:00:01'.
例如,我的系统在CET中运行,导致'1970-01-01 01:00:01'。 So this result should work as a default value for your table.
因此,此结果应作为表的默认值。
00:00:01 is valid for a time. 00:00:01有效一段时间。
TIMESTAMP DEFAULT processing changed at 5.6.5. TIMESTAMP DEFAULT处理在5.6.5处更改。 What version are you using?
你用的是什么版本? See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
请参阅http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
I do not get an error in 5.6.12. 我在5.6.12中没有收到错误。
Solution: Upgrade to 5.6. 解决方案:升级到5.6。
Actually it is enough to delete this part of code: 实际上删除这部分代码就足够了:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';`
On 5.7, you want to disable the TRADITIONAL
in sql_mode
in order to to be able to support '0000-00-00 00:00:00'
in datetime and timestamp, or 0s in time as you need. 在5.7,你想在
sql_mode
中禁用TRADITIONAL
,以便能够在datetime和timestamp中支持'0000-00-00 00:00:00'
,或者在你需要的时候支持0s。 For instance, by default on 5.7.10, SQL MODES are: 例如,默认情况下5.7.10, SQL MODES是:
mysql> SELECT @@sql_mode;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
You would do this by either setting sql_mode
in your my.cf
file or with command the line as follow: 您可以通过在
my.cf
文件中设置sql_mode
或使用命令行来执行此操作,如下所示:
SET sql_mode='ALLOW_INVALID_DATES';
You may want to keep some other modes on, and you should have a look on the documentation linked above. 您可能希望保留其他一些模式,您应该查看上面链接的文档。 This is quite tricky as some modes are dependent on others.
这非常棘手,因为有些模式依赖于其他模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.