0

I'm setting up a table that needs two auto-incrementing fields, 'id' and 'member#'.

I'll use AUTO_INCREMENT = 1001 on the latter for new data, as there is old data with member numbers less than 1000.

I'll use 'MAX(id)+1' on the 'id' field to auto-increment it.

But I'm not sure if this will do the job whenever there's an INSERT, or even where to put that bit of code. All I'm trying to do here is auto-increment the field, not SELECTing anything.

And out of curiosity, why is there only one AUTO_INCREMENTing field per table?

Surely, it can't be difficult to code AUTO_INCREMENT_2, AUTO_INCREMENT_3 etc.

All answers and assistance appreciated.

================================

ADDITIONAL INFORMATION AND LINKS

Sorry for the delay in my response, I've been doing additional research.

Ok so to explain further, we have people joining our group via the net. As such we need to assign a unique membership number to each person. Two John Does? Two different membership numbers. For this I've set the member# column as AUTO_INCREMENT, and then AUTO_INCREMENT = 1001 as a table option. Old membership numbers have three digits, new memberships have four. So each time someone registers as a new member on the web, there's an insert command that automatically assigns the next four digit membership number in the series to the new member.

member# INT(6) UNSIGNED NOT NULL UNIQUE KEY AUTO_INCREMENT And as a table option AUTO_INCREMENT = 1001

I hope this is clear. Other situations where someone might want to use a similar strategy could be assigning consecutive invoice numbers, receipt numbers, account numbers, etc. So how does one guarantee a +1 result, ie consecutive numbers?

Now we also need a table id column. Lots of tables need a table id. It too needs to be assigned an AUTO_INCREMENT value, in our case, beginning with 1, and incrementing by 1 (the default), to identify and distinguish one row from another. But unfortunately there can be only one AUTO_INCREMENT column per table in MySQL. :-/

So this situation belongs to a class of problems known as MAX+1 problems. (It may also be related to ROW_COUNT and LAST_INSERT_ID solutions.) The limit of a single AUTO_INCREMENT field per table requires a MAX+1 workaround and I am looking for advice on the best way to implement this. For example, is there a way to set this up inside the CREATE TABLE command itself, when defining the id field? Or something else of an equally simple nature, such as writing a function. It is indeed preferable to optimize for efficiency and use only needed features rather than implement a series of commands. Typically a suggested work around might be:

Lock tables membership write;
$max = SELECT MAX(id) FROM membership;
INSERT INTO membership ( id, firstname, lastname )
VALUES ($max+1 , 'jane', 'smith')
unlock tables;

Is there something better?

As whether AUTO_INCREMENT_2 /_3... features should exist. Well, I'd have to point out that there are a lot of features in MySQL that I'll never use, but obviously someone needs them. Nevertheless, it would be convenient to have this for those (rare) occasions when you might need it. Perhaps there is a distinction to be drawn between having a feature available and using it on any given table. I doubt an unused feature requires much in the way of additional memory or clicks (which are pretty cheap these days anyways).

Some links that may prove useful in understanding this situation:

https://duckduckgo.com/?q=mysql+max%2B1+problems&t=ffab&atb=v1-1&ia=web

Insert and set value with max()+1 problems

Problem with MySql INSERT MAX()+1

https://bugs.mysql.com/bug.php?id=3575

All answers, advice and assistance appreciated.

Bill.M
  • 11
  • 3
  • 1
    You typically don't need two auto incrementing fields. If you can share some sample data, perhaps more can be said. – Tim Biegeleisen Jan 16 '20 at 18:15
  • _I'm setting up a table that needs two auto-incrementing fields_ Are you absolutely sure about that. I would suggest that you might be doing something wrong in that case. Can you expand on WHY you think you need 2 autoincrement columns – RiggsFolly Jan 16 '20 at 18:16
  • This is barely programming related... even difficult to identify any question. [3.6.9 Using AUTO_INCREMENT](https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html). – Martin Zeitler Jan 16 '20 at 18:27

2 Answers2

0

Each InnoDB table has at most one counter for its auto-increment. This is part of the implementation. If you could define N auto-increment columns, in the same table, it would need more storage space to store N counters. It would require the auto-increment lock to last longer while you incremented N counters.

As for why is there only one per table, sure, it is possible that they could implement it to support more than one, but why?

It would make the implementation a lot more complex, and hinder performance, for cases that 99.99% of apps don't need.

They were trying to solve the needs for the majority of cases. In nearly every case of a table with an auto-increment, one per table is sufficient.

In nearly every case where someone like you thinks they need more than one per table, you'd be wise to step back and reconsider your design.

Bill Karwin
  • 462,430
  • 80
  • 609
  • 762
0
  1. In MySQL the table structure cannot contain more than one auto_increment field. When you try to create a table with 2 autoincremented fields or alter the table in attempt to create second autoincrement, the query fails.

  2. Autoincrement guarantees that each next value generated in the field will be greater than previous one in current connection. But it do NOT guarantee, that each next value generated in the field will be greater than previous value by 1. The "delta" may be 2 or even 1000... it cannot be negative or zero only.

Akina
  • 21,183
  • 4
  • 9
  • 16
  • An AI value is guaranteed to be greater than a previous value allocated in _any_ connection, not just the current connection. – Bill Karwin Jan 16 '20 at 18:35
  • @BillKarwin Thanks! It turns out that this problem has been fixed for a long time... I had remembered it from ancient times, and now I can forget it. – Akina Jan 16 '20 at 18:42
  • You might be thinking of `LAST_INSERT_ID()`, which is a function that reports the most recent AI value generated in the current session, even if other sessions have already generated subsequent values. – Bill Karwin Jan 16 '20 at 18:44
  • @BillKarwin No, I use this function never. – Akina Jan 16 '20 at 18:45
  • You might be interested in reading the documentation: https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id – Bill Karwin Jan 16 '20 at 18:46
  • @BillKarwin I know. But I don't like it and prefer `INSERT INTO A(M) SELECT 'X'; INSERT INTO B(A_ID, N) SELECT A.ID, 'Y' FROM A WHERE A.M = 'X'`. This works correctly for both single and bulk inserts. (for single value I may use VALUES instead of SELECT sometimes). – Akina Jan 16 '20 at 18:49