且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL在其他列中输入的每个不同的值会自动递增?

更新时间:2023-01-12 21:37:12

您似乎正在要求一种自动递增,该自动递增应在另一列中为每个不同的值保持单独的递增.

It seems like you're asking for an auto-increment that maintains a separate increment per distinct value in another column.

正如@RickJames回答的那样,该功能存在于MyISAM存储引擎中,但在InnoDB中不起作用.原因与InnoDB的行级锁定有关:为了防止竞争条件与另一个并发客户端插入相同的类"(按照您的示例),InnoDB必须在 all 上创建间隙锁em>给定类的行.

As @RickJames answers, this feature exists in the MyISAM storage engine, but it doesn't work in InnoDB. The reason is related to InnoDB's row-level locking: in order to prevent a race condition with another concurrent client inserting to the same 'class' (per your example), InnoDB would have to create a gap lock on all the rows for the given class.

这不会影响MyISAM,后者无论如何都会执行表级锁定,但是对于InnoDB,这将严重损害并发工作负载期间的吞吐量.

This doesn't affect MyISAM, which does table-level locking anyway, but for InnoDB it would greatly harm throughput during concurrent workloads.

这不是使用MyISAM的好理由.

This is not a good reason to use MyISAM.

但是您应该放弃自动递增是某种序数或行号的想法,而并非如此.它只是一个 unique 值;它没有其他含义.

But you should abandon the idea that an auto-increment is some kind of ordinal or row number—it isn't. All it is is a unique value; it has no other meaning.

如果您需要为每个班级分配连续的学生ID,请在您的应用程序中进行分配,并显式插入值.不要依靠自动增量机制来做到这一点.

If you need to assign consecutive student id's per class, then do so in your application and insert values explicitly. Don't rely on the auto-increment mechanism to do that.