Sample Header Ad - 728x90

Is it possible to have an auto-increment field per a primary key in MySQL

1 vote
1 answer
36 views
I'm sure I used to be able to do this (15+ years ago), but I don't remember how, or know if it's still possible. I have a table with a composite primary key - MainID and AutoIncrementID. The AutoIncrementID is set to auto increment. If I insert three entries with a MainID of 1, the AutoIncrementID will be 1, 2, and 3 respectively. If I then insert two entries with a MainID of 2, I would like the AutoIncrementID to be 1, then 2. If I then insert another entry with a MainID of 1, I would like the AutoIncrementID to be 4 It doesn't work this way by default in MySQL default, is it possible to do, and how do I configure it? All my searches are flooded with general auto increment information. This question is for both MySQL 5.7 and MySQL 8.0. Thanks!
Asked by Ben Holness (183 rep)
May 12, 2025, 10:51 PM
Last activity: May 13, 2025, 01:43 AM