MySQL Auto Increment Custom Values

佐手、 提交于 2019-11-28 11:36:02

Based on your comments, my recommendation is to do the following:

Use a regular integer auto_increment column as the primary key for the row, and then have a column of type varchar or one of the *text types (depending on your mysql server version and data storage requirements) to store your "identifier" that the customer uses.

The identifier can be auto-generated using a trigger.

If you're going to do lookups based on the identifier (i.e. perhaps the user enters an identifier to "jump to" a record) you will want an index on that column.

You can generate custom auto-increment values using stored procedures, as described in:

http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/

You will get keys like:

SELECT * FROM custom_autonums;
+----+------------+------------+
| id | seq_1      | seq_2      |
+----+------------+------------+
|  4 | 001-000001 | DBA-000001 |
|  5 | 001-000002 | DBA-000002 |
|  6 | 001-000003 | DBA-000003 |
|  7 | 001-000676 | DBA-000004 |
|  8 | 001-000677 | DBA-000005 |
|  9 | 001-000678 | DBA-000006 |

But you can modify the stored procedures to generate your pattern: 000, 001, 002, ..., 009, 00A, 00B, ..., 00Z, 010, ..., 0ZZ, ..., 100

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!