作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在 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
时,会按预期生成自动编号;但就像现在一样,我的应用程序生成前者而不是后者。
我的问题是:
是否有可能允许 DEFAULT(id)
生成 下一个自动编号?
应用程序环境的其他哪些变化可能导致这种情况?
我问这些是因为这种行为(插入零)直到最近才出现。
最佳答案
不要提供 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/
我在 tbl 中有这个主键列: `id` INT(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY 当我运行下面的查询时,插入零而不是下一个自动递增数字
我是一名优秀的程序员,十分优秀!