简体   繁体   中英

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. 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. Field 2 has an auto increment that increases slightly differently. records 1,2 and 3 were made at the same time. records 4 and 5 were made at the same time. record 6 was made individually.

Would it be best to read the last entry for field 2 and then increment it by one in my php program? 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.

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.

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. I wouldn't recommend that though. You will run into trouble down the line.

MySQL only offers one auto-increment column per table. 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.

Don't use PHP to query the largest ID number, then increment it and use it. 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.

The Oracle DBMS has an object called a sequence which gives back guaranteed-unique numbers. But you're using 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.

The second query ( DELETE ) keeps the table from getting big and wasting space. We don't care about any rows in the table except for the most recent one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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