MySQL:忘记设置自动增量

发布于 2024-12-12 10:33:04 字数 398 浏览 0 评论 0原文

我设置了一个数据库,其中有一个自动递增列,该列本来就是一个 ID。问题是我在 phpMyAdmin 中创建表时忘记检查自动增量...直到现在才注意到!所以我所拥有的是这样的:

ID   |   column one   |   column two
------------------------------------
0    |   some value   |   some value
0    |   other value  |   something else
0    |   example val. |   something
0    |   my example   |   something

基本上,我的“自动递增”列全面显示零。我如何修改此表以显示所有先前和所有未来行的自动增量?

I set up a database, with an auto incrementing column that was meant to be an ID. The problem is that I forgot to check off auto increment when creating the table in phpMyAdmin...and didn't notice until now! So what I have is something like:

ID   |   column one   |   column two
------------------------------------
0    |   some value   |   some value
0    |   other value  |   something else
0    |   example val. |   something
0    |   my example   |   something

Basically, my "auto incrementing" column shows zeros across the board. How could I modify this table to show auto increment for all previous and all future rows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

好菇凉咱不稀罕他 2024-12-19 10:33:04

如果表是空的,你可以这样做:

ALTER TABLE mytable MODIFY COLUMN id bigint not null primary key auto_increment

但我不确定其中的行会发生什么。最好创建一个具有正确定义的新表,将数据复制到减去 id 列的位置,删除旧的(错误的)表,然后将新表移至旧名称。

-- Either: 
CREATE TABLE newtable LIKE mytable;
ALTER TABLE newtable MODIFY COLUMN id bigint not null primary key auto_increment;
-- or:
CREATE TABLE newtable (
    id bigint not null primary key auto_increment,
    column1 type1,
    column2 type2,
    ...
);

INSERT INTO newtable 
    (column1, column2, ...) 
    SELECT column1, column2, column3, ... 
    FROM mytable;
-- Make SURE that insert worked BEFORE you drop the old table 
-- and lose data if it didn't.
DROP TABLE mytable;
ALTER TABLE newtable RENAME TO mytable;

我确信 phpMyAdmin 能够以更图形化的方式完成此操作。

If the table were empty you could just do:

ALTER TABLE mytable MODIFY COLUMN id bigint not null primary key auto_increment

but I'm not sure what will happen with rows in it. It might be best to create a new one with the correct definitions, copy the data over minus the id column, drop the old (wrong) table, and move the new one to the old name.

-- Either: 
CREATE TABLE newtable LIKE mytable;
ALTER TABLE newtable MODIFY COLUMN id bigint not null primary key auto_increment;
-- or:
CREATE TABLE newtable (
    id bigint not null primary key auto_increment,
    column1 type1,
    column2 type2,
    ...
);

INSERT INTO newtable 
    (column1, column2, ...) 
    SELECT column1, column2, column3, ... 
    FROM mytable;
-- Make SURE that insert worked BEFORE you drop the old table 
-- and lose data if it didn't.
DROP TABLE mytable;
ALTER TABLE newtable RENAME TO mytable;

I'm sure phpMyAdmin has the ability to do this more graphically.

很糊涂小朋友 2024-12-19 10:33:04

首先更新您的记录(否则您将得到重复的主键):

UPDATE `tablename` SET id = 1 WHERE "column one" = 'some value';
UPDATE `tablename` SET id = 2 WHERE "column one" = 'other value';
UPDATE `tablename` SET id = 3 WHERE "column one" = 'example val.';
UPDATE `tablename` SET id = 4 WHERE "column one" = 'my example';

然后添加主键/自动增量:

ALTER TABLE tablename MODIFY COLUMN id int(11) NOT NULL primary key auto_increment;

然后设置下一个自动增量:

ALTER TABLE tablename  AUTO_INCREMENT = 5;

First update your records (otherwise you will get duplicate primary key):

UPDATE `tablename` SET id = 1 WHERE "column one" = 'some value';
UPDATE `tablename` SET id = 2 WHERE "column one" = 'other value';
UPDATE `tablename` SET id = 3 WHERE "column one" = 'example val.';
UPDATE `tablename` SET id = 4 WHERE "column one" = 'my example';

Then add the primary key/auto_increment:

ALTER TABLE tablename MODIFY COLUMN id int(11) NOT NULL primary key auto_increment;

Then set the next auto_increment:

ALTER TABLE tablename  AUTO_INCREMENT = 5;
司马昭之心 2024-12-19 10:33:04

您可以删除 ID 列并重新创建它。如果您记得启用自动增量,则会重新分配 id;)

You could drop the ID column and re-create it. The ids will be reassigned, if you remembered to enable the auto increment ;)

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