MySQL timestamp 默认值(explicit_defaults_for_timestamp)
迁移数据库遇 timestamp 字段 set null。出现报错
解决方案
mysql> show variables like 'explicit_defaults_for_timestamp';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | ON |
+---------------------------------+-------+
>mysql> set session explicit_defaults_for_timestamp=OFF;
set session 只针对当前会话有效。想要永久生效在数据库配置文件中添加 explicit_defaults_for_timestamp=OFF
修改后实验结果
mysql> CREATE TABLE `test_timestamp` (
-> `id` int NOT NULL AUTO_INCREMENT,
-> `c1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> `c2` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
-> `c3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
-> `c4` timestamp NULL DEFAULT '0000-00-00 00:00:00',
-> `c5` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb3;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql>
mysql> insert into test_timestamp (c1, c2, c3, c4, c5) values (null, null, null, null, null);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql>
mysql> select * from test_timestamp;
+----+---------------------+------+---------------------+------+---------------------+
| id | c1 | c2 | c3 | c4 | c5 |
+----+---------------------+------+---------------------+------+---------------------+
| 8 | 2024-11-21 17:52:36 | NULL | 2024-11-21 17:52:36 | NULL | 2024-11-21 17:52:36 |
+----+---------------------+------+---------------------+------+---------------------+
1 row in set (0.00 sec)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构