MySql无法使列自动增量
我有一个表“Bestelling”,有 4 列:“Id”(PK)、“KlantId”、“Datum”、“BestellingsTypeId”,现在我想让列 Id 自动递增,但是,当我尝试这样做时,我收到此错误:
ERROR 1062: ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'
SQL Statement:
ALTER TABLE `aafest`.`aafest_bestelling` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT
ERROR: Error when running failback script. Details follow.
ERROR 1046: No database selected
SQL Statement:
CREATE TABLE `aafest_bestelling` (
`Id` int(11) NOT NULL,
`KlantId` int(11) DEFAULT NULL,
`Datum` date DEFAULT NULL,
`BestellingstypeId` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
有人有想法吗?
I have a table "Bestelling" with 4 columns: "Id" (PK), "KlantId", "Datum", "BestellingsTypeId", now I want to make the column Id auto_increment, however, when I try to do that, I get this error:
ERROR 1062: ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'
SQL Statement:
ALTER TABLE `aafest`.`aafest_bestelling` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT
ERROR: Error when running failback script. Details follow.
ERROR 1046: No database selected
SQL Statement:
CREATE TABLE `aafest_bestelling` (
`Id` int(11) NOT NULL,
`KlantId` int(11) DEFAULT NULL,
`Datum` date DEFAULT NULL,
`BestellingstypeId` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Anyone got an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果表包含 id 为 0(或负数)的现有记录,就会发生这种情况。更新所有现有记录以使用正值将允许在该列上设置 auto_increment。
编辑:有些人问那个 0 是怎么进去的。为了澄清起见,MySQL 参考手册指出“对于数字类型,默认值为 0,但对于使用 AUTO_INCRMENT 属性声明的整数或浮点类型除外,默认值是序列中的下一个值。”因此,如果在启用 auto_increment 之前对表执行插入操作而没有为数字列提供值,则在插入期间将使用默认值 0。更多详细信息可以在 https://dev.mysql.com/doc/ 找到refman/5.0/en/data-type-defaults.html。
This will happen if the table contains an existing record with an id of 0 (or negative). Updating all existing records to use positive values will allow auto_increment to be set on that column.
Edit: Some people asked how that 0 got in there. For clarification, the MySQL Reference Manual states that "For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence." So, if you performed an insert on a table without providing a value for the numeric column before the auto_increment was enabled, then the default 0 would be used during the insert. More details may be found at https://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html.
当尝试将列转换为 auto_increment(其中一行的值为 0)时,我也遇到了这个问题。暂时更改 0 值的另一种方法是通过设置:
对于会话。
这允许将列更改为 auto_increment,并且 id 为零。
零并不理想 - 我也不建议在 auto_increment 列中使用它。不幸的是,它是继承数据集的一部分,所以我现在坚持使用它。
最好在之后清除该设置(以及任何其他设置):
尽管当前客户端会话关闭时它将被清除。
有关“NO_AUTO_VALUE_ON_ZERO”设置的完整详细信息此处。
I also had this issue when trying to convert a column to auto_increment where one row had a value of 0. An alternative to changing the 0 value temporarily is via setting:
for the session.
This allowed the column to be altered to auto_increment with the zero id in place.
The zero isn't ideal - and I also wouldn't recommend it being used in an auto_increment column. Unfortunately it's part of an inherited data set so I'm stuck with it for now.
Best to clear the setting (and any others) afterwards with:
although it will be cleared when the current client session clsoes.
Full details on the 'NO_AUTO_VALUE_ON_ZERO' setting here.
当 MySQL 无法确定正确的 auto_increment 值时,就会发生这种情况。在您的情况下,MySQL 选择
1
作为下一个 auto_increment 值,但是表中已经存在具有该值的行。解决该问题的一种方法是自己选择合适的 auto_increment 值:(
请注意最后的
AUTO_INCRMENT=123456
。)This happens when MySQL can not determine a proper auto_increment value. In your case, MySQL choose
1
as next auto_increment value, however there is already row with that value in the table.One way to resolve the issue is to choose a proper auto_increment value yourself:
(Note the
AUTO_INCREMENT=123456
at the end.)我发现解决此问题的最简单方法是在更改列之前首先设置表的自动增量值。只需确保将自动增量值设置为高于该列中当前的最大值即可:
我在 MySQL 5.7 上对此进行了测试,它对我来说非常有用。
The easiest way that I have found to solve this issue is to first set the table's
AUTO INCREMENT
value before altering the column. Just make sure that you set the auto increment value higher than the largest value currently in that column:I tested this on MySQL 5.7 and it worked great for me.
编辑:不知道具体是如何造成的,但我确实有一个解决方法。
首先,创建一个像旧表一样的新表:
列
然后更改新数据中的转储
:移动表:
也许发生了一些损坏,这也可以解决这个问题。
PS:作为一个荷兰人,我强烈建议用英语编码;)
Edit: Don't know exactly how that would be caused, but I do have a workaround.
First, create a new table like the old one:
Then change the column
Dump in the new data:
Move the tables:
Maybe there's some corruption going on, and this would fix that as well.
P.S.: As a dutchman, I'd highly recommend coding in english ;)
我有类似的问题。问题是该表有一条
ID = 0
的记录,与 SystemParadox 指出的类似。我通过以下步骤处理了我的问题:步骤:
x
其中x = MAX(id)+1
x+1
x
更改回0
代码示例:
I had a similar issue. Issue was the table had a record with
ID = 0
similar to what SystemParadox pointed out. I handled my issue by the following steps:Steps:
x
wherex = MAX(id)+1
x+1
x
back to0
Code Example:
出现此错误的原因是 Any 表包含 id 为 0(或负数)的现有记录。更新所有现有记录以使用正值将允许在该列上设置 auto_increment。
如果这不起作用,则导出所有数据并将其保存在计算机中的任何位置,并且不要先建立外键关系,然后在父表中填充数据。
This error comes because the any table contains an existing record with an id of 0 (or negative). Update all existing records to use positive values will allow auto_increment to be set on that column.
If this didn't work then export all the data and save it any where in you computer and dont first make foreign key relation then fill data in parent table .
发生这种情况是因为您的主键列已经有值。
正如错误所述...
ALTER TABLE 导致 auto_increment 重新排序,导致键“PRIMARY”出现重复条目“1”,
这意味着您的列已经具有主键值 1 > 当您 auto_increment 时,该列被重新分配,导致重复,因此出现此错误
,解决方案是删除主约束,然后清空该列。然后再次更改表设置主键,这次使用自动增量。
This happens because your primary key column already has values.
As the error says ...
ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'
which means that your column already has a primary key value 1 which when you auto_increment that column is reassigned causing duplication and hence this error
the solution to this is to remove the primary constraint and then empty the column. Then alter the table setting the primary key again, this time with auto increment.
如果 MyISAM 表具有复合
AUTO_INCRMENT
PRIMARY KEY
并且尝试组合键,也会发生此错误例如
This error will also happen if have a MyISAM table that has a composite
AUTO_INCREMENT
PRIMARY KEY
and are trying to combine the keysFor example
如果您尝试修改的列包含在另一个表的外键关系中,也会发生无法将现有列设置为 auto_increment 的情况(尽管它不会产生提到的错误消息在问题中)。
(我添加了这个答案,尽管它与问题正文中的特定错误消息无关,因为这是在搜索与无法设置现有 MySQL 相关的问题时在 Google 上显示的第一个结果列自动递增。)
Not being able to set an existing column to auto_increment also happens if the column you're trying to modify is included in a foreign key relation in another table (although it won't produce the error message referred to in the question).
(I'm adding this answer even though it doesn't relate to the specific error message in the body of the question because this is the first result that shows up on Google when searching for issues relating to not being able to set an existing MySQL column to auto_increment.)