mysql5.7之后版本datetime默認值不能設(shè)置為0000-00-00的問題

歡迎訪問個人博客 http://blog.colinspace.com

根據(jù)報錯猜測可能是因為MySQL版本的問題,導(dǎo)出的SQL文件是從MySQL 5.6導(dǎo)出的, 目前Mac上面的MySQL版本是5.7

查詢官網(wǎng)發(fā)現(xiàn):

參考地址https://dev.mysql.com/doc/refman/5.7/en/datetime.html

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' 
format. The supported range is '1000-01-01' to '9999-12-31'.

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values 
in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of 
'1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

故相關(guān)的默認值需要設(shè)置成'1000-01-01 00:00:00''9999-12-31 23:59:59' 之間即可

MySQL設(shè)置默認為當前時間日期

一般給創(chuàng)建時間和更新時間給定當前時間和日期在某些場景下是很有效果的。

新建測試表如下:

CREATE TABLE `temp_lc` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

新增datetime 字段

mysql> alter table temp_lc add column regdate datetime default current_timestamp comment 'reg date';
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

插入測試數(shù)據(jù)并驗證

mysql> insert into temp_lc(name)values('James') ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from temp_lc ;
+----+-------+---------------------+
| id | name  | regdate             |
+----+-------+---------------------+
|  1 | James | 2017-10-18 19:51:25 |
+----+-------+---------------------+
1 row in set (0.00 sec)

字段也可以設(shè)置成timestamp 類型

mysql> alter table temp_lc add column regdatet timestamp default current_timestamp comment 'reg date2';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

插入測試數(shù)據(jù)并驗證

mysql> insert into temp_lc (name) values('Reg-timestamp-col') ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from temp_lc ;
+----+-------------------+---------------------+---------------------+
| id | name              | regdate             | regdatet            |
+----+-------------------+---------------------+---------------------+
|  1 | James             | 2017-10-18 19:51:25 | 2017-10-18 20:06:06 |
|  2 | Reg-timestamp-col | 2017-10-18 20:06:25 | 2017-10-18 20:06:25 |
+----+-------------------+---------------------+---------------------+
2 rows in set (0.00 sec)

附加

查看當前的sql-mode配置

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

sql-mode 官網(wǎng)介紹https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容