MySql无法使列自动增量

发布于 2024-10-24 21:21:24 字数 727 浏览 6 评论 0原文

我有一个表“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

时光是把杀猪刀 2024-10-31 21:21:24

如果表包含 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.

凶凌 2024-10-31 21:21:24

当尝试将列转换为 auto_increment(其中一行的值为 0)时,我也遇到了这个问题。暂时更改 0 值的另一种方法是通过设置:

SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';

对于会话。

这允许将列更改为 auto_increment,并且 id 为零。

零并不理想 - 我也不建议在 auto_increment 列中使用它。不幸的是,它是继承数据集的一部分,所以我现在坚持使用它。

最好在之后清除该设置(以及任何其他设置):

SET SESSION sql_mode='';

尽管当前客户端会话关闭时它将被清除。

有关“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:

SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';

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:

SET SESSION sql_mode='';

although it will be cleared when the current client session clsoes.

Full details on the 'NO_AUTO_VALUE_ON_ZERO' setting here.

谈下烟灰 2024-10-31 21:21:24

当 MySQL 无法确定正确的 auto_increment 值时,就会发生这种情况。在您的情况下,MySQL 选择 1 作为下一个 auto_increment 值,但是表中已经存在具有该值的行。

解决该问题的一种方法是自己选择合适的 auto_increment 值:(

ALTER TABLE ... CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 123456;

请注意最后的 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:

ALTER TABLE ... CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 123456;

(Note the AUTO_INCREMENT=123456 at the end.)

只怪假的太真实 2024-10-31 21:21:24

我发现解决此问题的最简单方法是在更改列之前首先设置表的自动增量值。只需确保将自动增量值设置为高于该列中当前的最大值即可:

ALTER TABLE `aafest`.`aafest_bestelling` 
AUTO_INCREMENT = 100, 
CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT

我在 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:

ALTER TABLE `aafest`.`aafest_bestelling` 
AUTO_INCREMENT = 100, 
CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT

I tested this on MySQL 5.7 and it worked great for me.

妖妓 2024-10-31 21:21:24

编辑:不知道具体是如何造成的,但我确实有一个解决方法。

首先,创建一个像旧表一样的新表:

CREATE TABLE aafest_bestelling_new LIKE aafest_bestelling;

ALTER TABLE `aafest`.`aafest_bestelling_new` 
CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT

然后更改新数据中的转储

INSERT INTO aafest_bestelling_new
 (KlantId, Datum, BestellingTypeId) 
SELECT 
KlantId, Datum, BestellingTypeId 
FROM aafest_bestelling;

:移动表:

RENAME TABLE 
aafest_bestelling TO aafest_bestelling_old, 
aafest_bestelling_new TO aafest_bestelling;

也许发生了一些损坏,这也可以解决这个问题。

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:

CREATE TABLE aafest_bestelling_new LIKE aafest_bestelling;

Then change the column

ALTER TABLE `aafest`.`aafest_bestelling_new` 
CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUTO_INCREMENT

Dump in the new data:

INSERT INTO aafest_bestelling_new
 (KlantId, Datum, BestellingTypeId) 
SELECT 
KlantId, Datum, BestellingTypeId 
FROM aafest_bestelling;

Move the tables:

RENAME TABLE 
aafest_bestelling TO aafest_bestelling_old, 
aafest_bestelling_new TO aafest_bestelling;

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 ;)

别念他 2024-10-31 21:21:24

我有类似的问题。问题是该表有一条 ID = 0 的记录,与 SystemParadox 指出的类似。我通过以下步骤处理了我的问题:

步骤:

  1. 将记录 id 0 更新为 x 其中 x = MAX(id)+1
  2. 更改表设置主键和自动增量设置
  3. 将种子值设置为 x+1
  4. 将记录 ID x 更改回 0

代码示例:

UPDATE foo SET id = 100 WHERE id = 0;
ALTER TABLE foo MODIFY COLUMN id INT(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE foo AUTO_INCREMENT = 101;
UPDATE foo SET id = 0 WHERE id = 100;

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:

  1. Update record id 0 to be x where x = MAX(id)+1
  2. Alter table to set primary key and auto increment setting
  3. Set seed value to be x+1
  4. Change record id x back to 0

Code Example:

UPDATE foo SET id = 100 WHERE id = 0;
ALTER TABLE foo MODIFY COLUMN id INT(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE foo AUTO_INCREMENT = 101;
UPDATE foo SET id = 0 WHERE id = 100;
¢蛋碎的人ぎ生 2024-10-31 21:21:24

出现此错误的原因是 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 .

我不咬妳我踢妳 2024-10-31 21:21:24

发生这种情况是因为您的主键列已经有值。

正如错误所述...

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.

三生殊途 2024-10-31 21:21:24

如果 MyISAM 表具有复合 AUTO_INCRMENT PRIMARY KEY 并且尝试组合键,也会发生此错误

例如

CREATE TABLE test1 (
 `id` int(11) NOT NULL,
 `ver` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`,`ver`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO test1 (`id`, `ver`) VALUES (1,NULL),(1,NULL),(1,NULL), (2,NULL),(2,NULL),(2,NULL);

ALTER TABLE test1 DROP PRIMARY KEY, ADD PRIMARY KEY(`ver`);

This error will also happen if have a MyISAM table that has a composite AUTO_INCREMENT PRIMARY KEY and are trying to combine the keys

For example

CREATE TABLE test1 (
 `id` int(11) NOT NULL,
 `ver` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`,`ver`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO test1 (`id`, `ver`) VALUES (1,NULL),(1,NULL),(1,NULL), (2,NULL),(2,NULL),(2,NULL);

ALTER TABLE test1 DROP PRIMARY KEY, ADD PRIMARY KEY(`ver`);
贱人配狗天长地久 2024-10-31 21:21:24

如果您尝试修改的列包含在另一个表的外键关系中,也会发生无法将现有列设置为 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文