gpt4 book ai didi

mysql - INSERT INTO table DEFAULT(key_col) 插入零?

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

我在 tbl 中有这个主键列:

`id` INT(5) unsigned NOT NULL  AUTO_INCREMENT PRIMARY KEY

当我运行下面的查询时,插入零而不是下一个自动递增数字:

INSERT INTO `tbl` (id, col1, ...) VALUES (DEFAULT(id), "value 1", ...)

有趣的是,这种行为是最近发生的,让我想知道我可能更改了哪些设置会导致这种情况。

我首先考虑的是 NO_AUTO_VALUE_ON_ZERO。我已将其添加到配置文件中的 sql_mode,现在是这样的:sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_VALUE_ON_ZERO

我知道当 DEFAULT(id) 被替换为 NULL 时,会按预期生成自动编号;但就像现在一样,我的应用程序生成前者而不是后者。

我的问题是:

  1. 是否有可能允许 DEFAULT(id) 生成 下一个自动编号?

  2. 应用程序环境的其他哪些变化可能导致这种情况?

我问这些是因为这种行为(插入零)直到最近才出现。

最佳答案

不要提供 id 作为 auto_increment PK 列的列。当你这样做时,你只是在自找麻烦。也许驱动程序在某个层上得到了更新。当你像以前那样做时,麻烦就会来敲门,而且确实如此。

CREATE TABLE myTable999
( id int auto_increment primary key,
thing varchar(100) not null,
colC int not null
);

insert myTable999(thing,colC) values ('fish',5),('mustard',11),('gators',-12);

select * from myTable999;
+----+---------+------+
| id | thing | colC |
+----+---------+------+
| 1 | fish | 5 |
| 2 | mustard | 11 |
| 3 | gators | -12 |
+----+---------+------+

您可以遵守手册,也可以掷骰子。 Here is the Manual .看第一个例子。

关于mysql - INSERT INTO table DEFAULT(key_col) 插入零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951529/

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