mysql 非严格模式插入一条为 null 的数据失败,插入多条就成功,有大佬指点下吗

2019-07-12 14:24:13 +08:00
 husky
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper

Connection id:		29740
Current database:
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.26 MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			6 days 23 hours 30 min 27 sec

Threads: 1  Questions: 1984187  Slow queries: 0  Opens: 1555  Flush tables: 1  Open tables: 947  Queries per second avg: 3.290
--------------

mysql> SHOW VARIABLES LIKE "sql_mode";
+---------------+----------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                              |
+---------------+----------------------------------------------------------------------------------------------------+
| sql_mode      | 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)

mysql> desc test;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255)     | NO   |     |         |                |
| add_time | int(11)          | NO   |     | 0       |                |
+----------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> INSERT INTO test (`name`, `add_time`) VALUES ('Albert', NULL);
ERROR 1048 (23000): Column 'add_time' cannot be null
mysql> INSERT INTO test (`name`, `add_time`) VALUES ('Albert', NULL),('Ben', NULL);
Query OK, 2 rows affected, 1 warning (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 1

mysql> select * from test;
+----+--------+----------+
| id | name   | add_time |
+----+--------+----------+
|  1 | Albert |        0 |
|  2 | Ben    |        0 |
+----+--------+----------+
2 rows in set (0.00 sec)
1062 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/582373

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX