MySQL - 将一些 ID 号从随机生成迁移到自动增量

发布于 2024-12-14 11:26:45 字数 470 浏览 3 评论 0原文

我正在重写公司的整个系统。最初的开发人员有点愚蠢,在他的数据库中随机为每个客户报告生成了 ID 号。每个 ID 号码的长度最多为 7 位数字 - 但可以是任何数字。

我正在将他的所有旧数据迁移到我们新的、逻辑结构更加合理的数据库中。我显然想对我们的 ID 字段使用 MySQL 自动增量。 但是,我们保留旧的 ID 号码至关重要,因为客户仍然每天打电话索要这些号码作为参考。

理想情况下,完美的场景是我们于 12 月 1 日上线 - 12 月 1 日之前的所有内容都在全部随机 ID,从 12 月 1 日起,它们从旧数据库中的最高随机 ID 开始自动递增。

MySQL 可以做到这样的事情而不出现任何问题吗?我当前使用两列 - 一列是我们的逻辑自动增量 ID,第二列名为 old_id,它在迁移过程中使用。但我们需要呼叫中心工作人员只能使用一个 ID,否则会出现大规模混乱。

谢谢!

I am in the process of rewriting a company's entire system. The original developer was a bit silly and generated ID numbers for each customer report randomly in his database. Each ID number is up to 7 digits long - but could be anything.

I am migrating over all his old data to our new, far more logically structured database. I obviously want to use a MySQL auto-increment for our ID field. However, it's vital that we keep the old ID numbers as customers still phone up each day with those to reference against.

Ideally, the perfect scenario would be we go live December 1st - everything up to December 1st is all randomly IDed, and from December 1st onwards they automatically increment starting at the highest random ID in the old database.

Is such a thing possible with MySQL without any issues? I am currently using two columns - one, our logical autoincrementing ID, and a second column called old_id which was being used during migration. But we need the call centre staff to only be using one ID or mass confusion will ensue.

Thanks!

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

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

发布评论

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

评论(2

彻夜缠绵 2024-12-21 11:26:45

如果从最高随机值开始编号,只需将字段更改为自动增量就足够了,正常行为是mysql不会更改已设置的id,并从最高值+1开始编号。

如果你想从一个特定的值(比如10,000,000)开始,你可以设置

ALTER TABLE theTableInQuestion AUTO_INCREMENT=10000000

当然,一定要创建备份并测试,但这根本不应该造成任何问题。 (请注意,旧记录将按照 id 字段的顺序存储,该顺序是随机的,并且不会反映创建顺序。)

If you start numbering from the highest random value, just changing the field to autoincrement should be enough, the normal behaviour is that mysql won't change ids already set, and starts numbering from the highest value+1.

If you want to start from a specific value (say 10,000,000) you can set

ALTER TABLE theTableInQuestion AUTO_INCREMENT=10000000

Of course, be sure to create backups and test, but it should not pose any problems at all. (Note that the old records will be stored in order of the id-field, which is random, and won't reflect the creation order.)

╰つ倒转 2024-12-21 11:26:45

由于您需要保留旧 ID,我假设您将为自动增量 ID 创建一个新列,该列将成为您的主键,但保留现有 ID 列并将其重命名(也许是 old_id?) 。我还假设您在客户注册时进行记录。

如果您将旧 ID 列设置为可为空(允许 NULL 作为有效值),那么您可以简单地检查旧 ID 列是否为 NULL。如果它不为 NULL,则将其视为 ID,否则使用自动增量列。

查找客户:

SELECT *
FROM customer
WHERE (id = /*Put your ID here*/ AND reg_date >= /*Put the date the new regime starts here*/)
OR (id_old = /*put your ID here*/ AND reg_date < /*Put the date the new regime starts here*/)

这有时会返回 2 行,因此您必须使用其他一些条件来唯一标识相关客户。

至于将旧客户与数据库中的其他表关联起来,一旦生成新 ID,您始终可以在整个数据库内部使用它。显然,您必须更新使用旧 ID 作为外键的表。

UPDATE target_table 
JOIN customers on target_table.cust_id = customers.id_old
SET target_table.cust_id = customers.id;

(注意:上面只是一个快速而肮脏的查询,尚未经过测试!我建议在真正尝试之前先在数据库的副本上进行测试!)

As you need to keep the old IDs, I'm going to assume that you're going to create a new column for autoincrement ID that will become your primary key but keep the existing ID column and rename it (to old_id, maybe?). I'm also going to assume you record when a customer signed up.

If you make your old ID column nullable (allow NULL as a valid value) then you can simply check whether or not the old ID column is NULL. If it's not NULL then treat that as the ID, otherwise use the autoincrement column.

Finding a customer:

SELECT *
FROM customer
WHERE (id = /*Put your ID here*/ AND reg_date >= /*Put the date the new regime starts here*/)
OR (id_old = /*put your ID here*/ AND reg_date < /*Put the date the new regime starts here*/)

This will occasionally return 2 rows so you'll have to use some other criteria to uniquely identify the customer in question.

As for associating an old customer with other tables in the database, you can always use the new ID internally throughout the entire DB once its generated. You will have to update tables that are using the old ID as the foreign key, obviously.

UPDATE target_table 
JOIN customers on target_table.cust_id = customers.id_old
SET target_table.cust_id = customers.id;

(Note: The above is just a quick and dirty query that hasn't been tested! I'd suggest testing on a copy of the database before you try it for real!)

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