Page 1 of 1

Generate autonumber manually in MySQL

Posted: Tue Jul 27, 2010 8:23 pm
by klaingny
:( I am a new programmer with php and MySQL. I want to generate autonumber in MySQL manually. I have a table with fields such stu_id, stu_name, gender.
I want to generate stu_id as uatonumber: S001, S002, I don't have any concept to do this. Please help me or give me direct link to learn this method. Thank you

Re: Generate autonumber manually in MySQL

Posted: Tue Jul 27, 2010 10:24 pm
by JakeJ
You can't manually generate an autonumber. Autonumber is "automatic" meaning no intervention required. Manual mean you decide what the number is.

But, if you want to find out what the most recent user_id created is, query max(user_id) and add one to it. Use a regular expression to strip off the s and leading zeros.

But really, is there any reason you have to store s001, s002, etc? why not just use autonumber and then prepend it with an s as needed. Unless the number is assigned somewhere else and you're storing it, in which case it's user input.

Re: Generate autonumber manually in MySQL

Posted: Wed Jul 28, 2010 2:51 am
by Christopher
Look into emulating sequences in MySQL. The code is fairly simple to implement.