Custom Primary Keys with Prefix Auto-Incrementation
In database design, it's common to require custom primary keys that follow a specific format, such as in this case where the ID field needs to increment in the form 'LHPL001','LHPL002','LHPL003'.
Solution 1: Trigger and Sequence Table
This approach utilizes a separate table to generate sequences and a trigger to assign these sequences to new records.
Implementation:
1 2 3 |
|
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 |
|
With this setup, new rows inserted into the table1 will automatically generate a sequence number prefixed with 'LHPL' in the id field.
Example:
1 2 |
|
Result:
ID | NAME |
---|---|
LHPL001 | Jhon |
LHPL002 | Mark |
The above is the detailed content of How to Automatically Generate Custom Primary Keys with a Prefix in MySQL?. For more information, please follow other related articles on the PHP Chinese website!