简体   繁体   English

MySQL自增列需要唯一还是主键

[英]Does the MySQL Auto Increment Column Need to Be Unique or a Primary Key

I was developing a database in SQL Server where I was using an identity column as a seed for a primary key field.我正在 SQL 服务器中开发一个数据库,在该服务器中我使用标识列作为主键字段的种子。 The intention was to reset the identity to 1 at the beginning of every year.目的是在每年年初将标识重置为 1。 This would allow us to create a PK of the Year - Identity Column.这将允许我们创建年度 PK - 身份列。

Create Table Issues (
IssueID  AS RIGHT(CONVERT(VARCHAR, Year(getdate()), 4),2) + '-' + RIGHT(REPLICATE('0', 2) + 
CONVERT(VARCHAR, RecordID),3) NOT NULL PRIMARY KEY,
RecordID int Identity (1,1),.........)

The result would be结果将是

     
IssueID RecordID 20-001 1 20-002 2 20-003 3 21-001 1 etc....

Now I've been told we are going to use a MySQL database instead.现在有人告诉我,我们将改用 MySQL 数据库。

  1. Can an Auto-Increment field in MySQL contain duplicate values like it can in SQL Server? MySQL 中的自动增量字段是否可以像 SQL 服务器中一样包含重复值?
  2. If Not, how can I do what I need to do in MySQL?如果不是,我该如何在 MySQL 中做我需要做的事情?

In MySQL a table can have only one auto_increment column and this column must be a part of the primary key.在 MySQL 中,一个表只能有一个 auto_increment 列,并且该列必须是主键的一部分。 See details here . 在此处查看详细信息。

Technical workaround for your task would be creating of a table with a single auto_increment column, and you can obtain auto_increment value by inserting a record into this table and immediately calling standard MySQL function last_inser_id() .您的任务的技术解决方法是创建一个具有单个 auto_increment 列的表,您可以通过在该表中插入一条记录并立即调用标准 MySQL function last_inser_id()来获取 auto_increment 值。 When time comes you should truncate the table - in this case the auto_increment count will be reset.到时候你应该截断表格——在这种情况下,auto_increment 计数将被重置。

In MySQL, you can't use the default auto-increment feature for what you describe, a incrementing value that starts over per year.在 MySQL 中,您不能对所描述的内容使用默认的自动增量功能,即每年开始的增量值。

This was a feature of the MyISAM storage engine years ago.这是几年前 MyISAM 存储引擎的一个特性。 An auto-increment that was the second column of a multi-column primary key would start counting from one for each distinct value in the first column of the PK.作为多列主键的第二列的自动增量将从 PK 的第一列中的每个不同值开始计数。 See the example under "MyISAM Notes" on this page: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html请参阅本页“MyISAM 注释”下的示例: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.ZFC35FDC70D5FC679A53EZC

But it's considered not a good idea to use MyISAM because it does not support ACID .但是使用 MyISAM 被认为不是一个好主意,因为它不支持 ACID In general, I would find another way of solving this task.一般来说,我会找到另一种解决此任务的方法。 I would not use MyISAM.我不会使用 MyISAM。

In InnoDB, there's no way the table will generate a value that is a duplicate of a value currently in the table, or even a value less than the maximum value previously generated for that table.在 InnoDB 中,表不会生成与表中当前值重复的值,甚至不会生成小于先前为该表生成的最大值的值。 In other words, there's no way to "fill in the gaps" using auto-increment.换句话说,没有办法使用自动增量来“填补空白”。

You can use ALTER TABLE mytable AUTO_INCREMENT=1 to reset the counter, but the value you set it will automatically advance to the max value currently in the table + 1.您可以使用ALTER TABLE mytable AUTO_INCREMENT=1来重置计数器,但是您设置的值会自动前进到当前表中的最大值 + 1。

So you'll have to generate it using either another table, or else something other than the MySQL database.因此,您必须使用另一个表或 MySQL 数据库以外的其他表来生成它。 For example, I've seen some people use memcached, which supports an atomic "increment and return counter" operation.例如,我看到有人使用 memcached,它支持原子的“递增和返回计数器”操作。

Another thing to consider: If you need a row counter per year, this is actually different from using MySQL's auto-increment feature.另一件需要考虑的事情:如果您每年需要一个行计数器,这实际上与使用 MySQL 的自动增量功能不同。 It's not easy to use the latter as a row counter.将后者用作行计数器并不容易。 Besides, what happens if you roll back a transaction or delete a row?此外,如果您回滚事务或删除行会发生什么? You'd end up with non-consecutive RecordId values, with unexplained "gaps."您最终会得到不连续的 RecordId 值,并带有无法解释的“间隙”。 It's also a fact about the auto-increment feature that it guarantees that subsequent id's will be greater, but it does not guarantee to generate all values consecutively .这也是关于自动增量功能的一个事实,它保证后续的 id 会更大,但它不保证连续生成所有值 So you'll get gaps eventually anyway.所以无论如何你最终都会得到差距。

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

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