我希望有一个带有自动递增主键的MySQL InnoDB表,该表还有一个根据记录类型独立递增的列。
例如,我的记录可能如下所示(显示为两个表以突出显示两个递增列)。
如果删除子递增系列末尾的记录(例如PK 12),然后为同一类型插入记录,则不应复制SecondaryKey,并且新记录应具有PK 17和SecondaryKey 5(而不是4)。
这是如何最好地完成的?
+----+------+--------------+----------------+
| 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 |
+----+------+--------------+----------------+