Generate autonumber manually in MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
klaingny
Forum Newbie
Posts: 1
Joined: Tue Jul 27, 2010 8:12 pm

Generate autonumber manually in MySQL

Post 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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Generate autonumber manually in MySQL

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Generate autonumber manually in MySQL

Post by Christopher »

Look into emulating sequences in MySQL. The code is fairly simple to implement.
(#10850)
Post Reply