gpt4 book ai didi

php - Mysql多插入查询

转载 作者:可可西里 更新时间:2023-11-01 07:32:44 25 4
gpt4 key购买 nike

在插入 mysql 数据时,我遇到了以下问题。这里的电子邮件字段不能为空。在执行以下查询(查询 1 和 2)时,我们预计会出现错误。但是查询 1 给出了错误,而查询 2 给出了成功。

表结构

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

查询 1:

INSERT INTO `users` (`id`, `name`, `email`) VALUES (NULL, 'manu', NULL);

Result:-#1048 - Column 'email' cannot be null

查询 2:

INSERT INTO `users` (`id`, `name`, `email`)
VALUES (NULL, 'manu', NULL),(NULL, 'Jose', NULL);

Result:-2 rows inserted.

Both query returns success.

查询有没有错误?任何想法将不胜感激。

最佳答案

来自 documentation

Inserting NULL into a column that has been declared NOT NULL. For multiple-row INSERT statements or INSERT INTO ... SELECT statements, the column is set to the implicit default value for the column data type. This is 0 for numeric types, the empty string ('') for string types, and the “zero” value for date and time types. INSERT INTO ... SELECT statements are handled the same way as multiple-row inserts because the server does not examine the result set from the SELECT to see whether it returns a single row. (For a single-row INSERT, no warning occurs when NULL is inserted into a NOT NULL column. Instead, the statement fails with an error.)

关于php - Mysql多插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28810726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com