I wish to have a MySQL InnoDB table with an auto-incrementing primary key which also has a column which increments independently based on the type of record.
For instance, my records might be as shown below (shown as two tables to highlight the two incrementing columns).
If a record at the end of the sub-incrementing series is deleted such as PK 12, and then a record is inserted for that same type, the SecondaryKey should not be duplicated, and the new record should have PK 17 and SecondaryKey 5 (and not 4).
How is this best accomplished?
+----+------+--------------+----------------+
| PK | Type | SecondaryKey | Data |
+----+------+--------------+----------------+
| 1 | a | 1 | Data for PK 1 |
| 2 | a | 2 | Data for PK 2 |
| 3 | c | 1 | Data for PK 3 |
| 4 | d | 1 | Data for PK 4 |
| 5 | a | 3 | Data for PK 5 |
| 6 | b | 1 | Data for PK 6 |
| 7 | c | 2 | Data for PK 7 |
| 8 | a | 4 | Data for PK 8 |
| 9 | d | 2 | Data for PK 9 |
| 10 | b | 2 | Data for PK 10 |
| 11 | d | 3 | Data for PK 11 |
| 12 | d | 4 | Data for PK 12 |
| 13 | a | 5 | Data for PK 13 |
| 14 | b | 3 | Data for PK 14 |
| 15 | b | 4 | Data for PK 15 |
| 16 | c | 3 | Data for PK 16 |
+----+------+--------------+----------------+
+----+------+--------------+----------------+
| PK | Type | SecondaryKey | Data |
+----+------+--------------+----------------+
| 1 | a | 1 | Data for PK 1 |
| 2 | a | 2 | Data for PK 2 |
| 5 | a | 3 | Data for PK 5 |
| 8 | a | 4 | Data for PK 8 |
| 13 | a | 5 | Data for PK 13 |
| 6 | b | 1 | Data for PK 6 |
| 10 | b | 2 | Data for PK 10 |
| 14 | b | 3 | Data for PK 14 |
| 15 | b | 4 | Data for PK 15 |
| 3 | c | 1 | Data for PK 3 |
| 7 | c | 2 | Data for PK 7 |
| 16 | c | 3 | Data for PK 16 |
| 4 | d | 1 | Data for PK 4 |
| 9 | d | 2 | Data for PK 9 |
| 11 | d | 3 | Data for PK 11 |
| 12 | d | 4 | Data for PK 12 |
+----+------+--------------+----------------+