简体   繁体   English

组条目的Mysql自动增量

[英]Mysql Auto Increment For Group Entries

I need to setup a table that will have two auto increment fields. 我需要设置一个具有两个自动增量字段的表。 1 field will be a standard primary key for each record added. 每个添加的记录的标准主键为1个字段。 The other field will be used to link multiple records together. 另一个字段将用于将多个记录链接在一起。

Here is an example. 这是一个例子。

field 1  |  field 2
  1           1
  2           1
  3           1
  4           2
  5           2
  6           3

Notice that each value in field 1 has the auto increment. 请注意,字段1中的每个值都有自动增量。 Field 2 has an auto increment that increases slightly differently. 字段2的自动增量略有不同。 records 1,2 and 3 were made at the same time. 记录1,2和3同时进行。 records 4 and 5 were made at the same time. 记录4和5同时进行。 record 6 was made individually. 记录6是单独制作的。

Would it be best to read the last entry for field 2 and then increment it by one in my php program? 最好阅读字段2的最后一个条目,然后在我的php程序中将其递增1? Just looking for the best solution. 只是寻找最佳解决方案。

You should have two separate tables. 您应该有两个单独的表。

ItemsToBeInserted
id, batch_id, field, field, field

BatchesOfInserts
id, created_time, field, field field

You would then create a batch record, and add the insert id for that batch to all of the items that are going to be part of the batch. 然后,您将创建一个批次记录,并将该批次的插入ID添加到将成为该批次一部分的所有项目中。

You get bonus points if you add a batch_hash field to the batches table and then check that each batch is unique so that you don't accidentally submit the same batch twice. 如果将batch_hash字段添加到批处理表中,然后检查每个批处理是否唯一,那么您将获得加分,这样就不会意外地将同一批提交两次。

If you are looking for a more awful way to do it that only uses one table, you could do something like: 如果您正在寻找一种仅使用一张表的更糟糕的方法,则可以执行以下操作:

$batch = //Code to run and get 'SELECT MAX(BATCH_ID) + 1 AS NEW_BATCH_ID FROM myTable' 

and add that id to all of the inserted records. 并将该ID添加到所有插入的记录中。 I wouldn't recommend that though. 我不建议这样做。 You will run into trouble down the line. 您会遇到麻烦。

MySQL only offers one auto-increment column per table. MySQL每张表仅提供一个自动增量列。 You can't define two, nor does it make sense to do that. 您不能定义两个,这样做也没有意义。

Your question doesn't say what logic you want to use to control the incrementing of the second field you've called auto-increment. 您的问题不是说要使用什么逻辑来控制第二个字段的增量(称为自动增量)。 Presumably your PHP program will drive that logic. 大概您的PHP程序将驱动该逻辑。

Don't use PHP to query the largest ID number, then increment it and use it. 不要使用PHP查询最大的ID号,然后递增并使用它。 If you do your system is vulnerable to race conditions. 如果这样做,您的系统很容易受到竞争条件的影响。 That is, if more than one instance of your PHP program tries that simultaneously, they will occasionally get the same number by mistake. 也就是说,如果您的PHP程序的多个实例同时尝试该操作,它们有时会错误地得到相同的数字。

The Oracle DBMS has an object called a sequence which gives back guaranteed-unique numbers. Oracle DBMS有一个称为sequence的对象,该对象会返回保证唯一的数字。 But you're using MySQL. 但是您正在使用MySQL。 You can obtain unique numbers with a programming pattern like the following. 您可以使用如下所示的编程模式获取唯一编号。

First create a table for the sequence. 首先为序列创建一个表。 It has an auto-increment field and nothing else. 它具有一个自动递增字段,仅此而已。

CREATE TABLE sequence (
    sequence_id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`sequence_id`)
)

Then when you need a unique number in your program, issue these three queries one after the other: 然后,当您在程序中需要唯一编号时,一个接一个地发出以下三个查询:

INSERT INTO sequence () VALUES ();
DELETE FROM sequence WHERE sequence_id < LAST_INSERT_ID();
SELECT LAST_INSERT_ID() AS sequence;

The third query is guaranteed to return a unique sequence number. 确保第三个查询返回唯一的序列号。 This guarantee holds even if you have dozens of different client programs connected to your database. 即使您有数十个不同的客户端程序连接到数据库,该保证仍然有效。 That's the beauty of AUTO_INCREMENT. 这就是AUTO_INCREMENT的优点。

The second query ( DELETE ) keeps the table from getting big and wasting space. 第二个查询( DELETE )防止表变大和浪费空间。 We don't care about any rows in the table except for the most recent one. 除了最近的那一行,我们不在乎表中的任何行。

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

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