简体   繁体   中英

Custom Auto incremented “id” using php and mysql

Hey I am newbie to php and mysql i was trying to generate id for my student table where i want my student id to be like this . my student id field is of varchar as well as primary. $student id= LA/YYYY/0001 and go on where as LA is prefix and YYYY is the year and 0001 is the numeric value so i want the id to be like this and code to be auto incremented.(LA/YYYY/0002) and so on . Thank you all for your suggestion .

Instead of writing something overly complex to have custom auto incremented ID's, why not rely on MySQL's autoincrementing INT's? Then, you could have separate columns instead of a composite column for the additional details. In your code, you could then concatenate the columns into one output.

Example:

id | prefix | year | first_name | last_name
-------------------------------------------
1    LA       2014   Half         Crazed
2    FR       2015   Jon          Doe

Then, in your application (or SQL):

SELECT CONCAT(prefix, '/', year, '/', LPAD(id, 4, '0')) AS uuid

(note: untested, for demonstration only)

If you absolutely need to use the auto-incremented (for every record) you can use a composite key as an ID.

So your table will have two fields contributing to the key: 1. id (auto-incremented integer) 2. prefix (varchar with format LA/YYYY)

You can concatenate both to produce the id you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM