存在表后添加新ID(自增)的麻烦

发布于 2024-10-14 22:19:08 字数 98 浏览 3 评论 0原文

我有一个包含 38.000 条记录的表,但没有任何自动增量列,如 ID
现在我必须添加一个ID列,我想知道会不会有麻烦?

I have a table with 38.000 records, but without any auto increment column like ID.
Now I have to add an ID column, and I'm wondering could there be troubles?

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

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

发布评论

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

评论(7

隔岸观火 2024-10-21 22:19:08

仅当您的表与其他表没有关系时,您才可以毫无问题地添加此操作。

您必须删除旧主键并相应地上传表(也许在旧主键上添加唯一索引)。

像这样继续:

  • 转储数据库

  • 像这样删除主键

ALTER TABLE XXX DROP PRIMARY KEY
  • 添加像这样的新列
ALTER TABLE XXX add column Id INT NOT NULL AUTO_INCREMENT FIRST, ADD primary KEY Id(Id)

表将被查看并更新 AutoInc。

You can add do that without problem only if your table doesn't have relationship with others.

You must remove the old primary key and upload the table accordingly (perhaps add an unique index on the old primary key).

Proceed like that :

  • Make a dump of your database

  • Remove the primary key like that

ALTER TABLE XXX DROP PRIMARY KEY
  • Add the new column like that
ALTER TABLE XXX add column Id INT NOT NULL AUTO_INCREMENT FIRST, ADD primary KEY Id(Id)

The table will be looked and the AutoInc updated.

清风疏影 2024-10-21 22:19:08

这将向您的 MySQL 表添加一个自动增量 ID:

ALTER TABLE  `your_table_name` ADD  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

根据表上的数据量,可能需要几分钟才能完成。

This will add an auto increment ID to your MySQL table:

ALTER TABLE  `your_table_name` ADD  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

Depending on the amount of data on your table it could take a few minutes to complete.

黎歌 2024-10-21 22:19:08

这是我使用 MySQL Workbench 尝试的解决方案:

ALTER TABLE `tableName` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT  ;

我不知道这是否是正确的方法,但我还没有注意到我的 Java EE 应用程序有任何问题。

This is the solution that i tried with MySQL Workbench:

ALTER TABLE `tableName` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT  ;

I don't know if this is correct way, but I didn't notice any problem in my Java EE Application yet.

热风软妹 2024-10-21 22:19:08

我处理非常大的数据,并且有一个 id 列填充了 NULL。我选择运行以下 SQL 来填充数字...然后我将 id 列设置为我的主键。

set @row = 0;
UPDATE philadelphia.msg_geo_sal SET id = @row := @row + 1;

I work with very large data and had an id column filled with NULLS. I chose to run the following SQL to fill with numbers... then I set the id column to be my primary key.

set @row = 0;
UPDATE philadelphia.msg_geo_sal SET id = @row := @row + 1;
↘紸啶 2024-10-21 22:19:08

“会不会有麻烦?”

仅插入一条记录可能会遇到麻烦。然而......

一般来说,添加新字段通常是非常安全的,无论它是否是自动递增列。如果您不确定所有依赖项,则更改或删除最有可能导致问题的列。

但是,为了安全起见,我会首先在副本(测试)版本上尝试,然后首先针对测试版本运行您的应用程序,只是为了安全起见。无论如何,使用测试环境是一个很好的做法。

您能更具体地说明您担心什么类型的麻烦吗?

"could there be troubles?"

There could be troubles just inserting a record. However...

Generally, adding a new field usually is pretty safe to do, whether it's an auto-incrementing column or not. It's changing or deleting columns that are the most likely to cause you issues if you're not sure of all the dependencies.

To be safe, however, I'd try it on a copy (test) version first and run your app(s) against the test version first, just to be safe. It's good practice to use a test environment no matter what anyway.

Can you be more specific about what types of troubles you're worried about?

月野兔 2024-10-21 22:19:08

您可能需要先将其添加为主键,然后才能将其设置为自动增加字段

您可以查看此示例

ALTER TABLE `category`
ADD PRIMARY KEY (`cat_id`);
ALTER TABLE `category`
MODIFY `cat_id` int(11) NOT NULL AUTO_INCREMENT;

You may need to add it as primary key first before you can make it as auto increase field

You can look at this example

ALTER TABLE `category`
ADD PRIMARY KEY (`cat_id`);
ALTER TABLE `category`
MODIFY `cat_id` int(11) NOT NULL AUTO_INCREMENT;
誰ツ都不明白 2024-10-21 22:19:08

您对任何字段的定义更改的问题是更改是“破坏性的”。最好将此类更改视为数据迁移。我还没有使用 MySQL 完成此操作,但必须向 SQL Server 添加自动编号。基本思想是一样的。

检查文档,因为 MySQL 可能有一种在不重置字段的情况下添加 aut 增量的机制。如果是这样,通过 SQL 来解决问题。一般来说,我建议不要使用工具以视觉方式进行这些类型的更改,因为大多数工具的方法都具有很大的破坏性。

You problem with definition change to any field is the change is "destructive". It is best to treat changes like this as a data migration. I have not done this with MySQL, but have had to add auto numbering to SQL Server. The basic idea is the same.

Check the documentation, as MySQL may have a mechanism for adding aut increment without resetting the field. If so, doing it via SQL will solve the problem. In general, I recommend against doing these types of changes visually, with a tool, as most tools are very destructive in their methdology.

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