存在表后添加新ID(自增)的麻烦
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仅当您的表与其他表没有关系时,您才可以毫无问题地添加此操作。
您必须删除旧主键并相应地上传表(也许在旧主键上添加唯一索引)。
像这样继续:
转储数据库
像这样删除主键
表将被查看并更新 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
The table will be looked and the AutoInc updated.
这将向您的 MySQL 表添加一个自动增量 ID:
根据表上的数据量,可能需要几分钟才能完成。
This will add an auto increment ID to your MySQL table:
Depending on the amount of data on your table it could take a few minutes to complete.
这是我使用 MySQL Workbench 尝试的解决方案:
我不知道这是否是正确的方法,但我还没有注意到我的 Java EE 应用程序有任何问题。
This is the solution that i tried with MySQL Workbench:
I don't know if this is correct way, but I didn't notice any problem in my Java EE Application yet.
我处理非常大的数据,并且有一个 id 列填充了 NULL。我选择运行以下 SQL 来填充数字...然后我将 id 列设置为我的主键。
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.
仅插入一条记录可能会遇到麻烦。然而......
一般来说,添加新字段通常是非常安全的,无论它是否是自动递增列。如果您不确定所有依赖项,则更改或删除最有可能导致问题的列。
但是,为了安全起见,我会首先在副本(测试)版本上尝试,然后首先针对测试版本运行您的应用程序,只是为了安全起见。无论如何,使用测试环境是一个很好的做法。
您能更具体地说明您担心什么类型的麻烦吗?
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?
您可能需要先将其添加为主键,然后才能将其设置为自动增加字段
您可以查看此示例
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
您对任何字段的定义更改的问题是更改是“破坏性的”。最好将此类更改视为数据迁移。我还没有使用 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.