简体   繁体   English

使用mysql将auto_increment减少到较低的未使用数字

[英]auto_increment to a lower unused number with mysql

I have an old website and a new website... the old website had 4500 orders placed on it, tracked by a table with a primary key for the order id. 我有一个old网站和一个new网站... old网站上放置了4500个订单,并通过带有主键作为订单ID的表进行跟踪。

When the new website was launched, it was launched before migrating old orders into it. 启动new网站时,它是在将old订单迁移到其中之前启动的。 To accomplish this, the auto_increment value on the new orders table was set to 5000 so any new order placed would not collide with an old id. 为此,将new订单表上的auto_increment值设置为5000,这样放置的任何new订单都不会与old ID发生冲突。

This allows orders to continue being placed on the new website, all is well... 这样可以继续在new网站上下订单,一切都很好...

Now I'd like to run my import script to bring in the old orders into the new website. 现在,我想运行导入脚本以将old订单带入new网站。

Is it possible to temporarily lower the auto_increment value on the new orders table to my desired order id? 是否可以将new订单表上的auto_increment值暂时降低至所需的订单ID?

Disclaimer : This is a migration from a Drupal 5 Ubercart based site, to a Drupal 7 Commerce based site, so I do not (easily) have control over the complex queries involved in assembling the new orders, and cannot simply (AFAIK) supply an order id when assembling an order, because the system always refers to the next available primary key value in the table when creating an order. 免责声明 :这是从基于Drupal 5 Ubercart的站点到基于Drupal 7 Commerce的站点的迁移,因此我无法(轻松地)控制组装新订单所涉及的复杂查询,并且不能简单地(AFAIK)提供组装订单时的订单ID,因为系统在创建订单时始终引用表中的下一个可用主键值。 I can easily take the site offline to run the script, so nothing gets out of sync. 我可以轻松地使站点脱机以运行脚本,因此没有任何不同步的情况。

For importing the "old" orders you don't need to rely on the autoincrement id-- they already have ids, and you probably want to keep those! 导入“旧”订单时,您不需要依赖自动增量ID,因为它们已经有ID,您可能希望保留这些ID!

Modify your import script to insert the complete old records into the new table, id and all! 修改您的导入脚本,以将完整的旧记录插入到新表,id和全部中! As long as the ids don't collide, it shouldn't be a problem. 只要id不冲突,就不成问题。

You can always run ALTER TABLE table_name AUTO_INCREMENT = 1; 您始终可以运行ALTER TABLE table_name AUTO_INCREMENT = 1; (or whatever number you want). (或您想要的任何数字)。

The question is: do you WANT to? 问题是:您是否愿意

If you have any records already in the database, it's probably best to insure the next auto increment value is larger than the maximum already in your database. 如果数据库中已经有任何记录,则最好确保下一个自动增量值大于数据库中已经存在的最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM