Auto increment for varchar in mysql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Auto increment for varchar in mysql

it is possible to increment numbers automatically in MySQL. in the same way I want to increment for varchar. I heard we need some trigger but I don't have any idea how to do it and where to write the code for that. any help would be appreciated

16th Jan 2017, 8:29 AM
Shyam Dhuriya
Shyam Dhuriya - avatar
8 Answers
+ 3
http://stackoverflow.com/questions/21145426/php-alphanumeric-auto-increment-insert-into-sql-database First answer. Exactly what you're trying to do. [Keywords: mysql increment alphanumeric]
27th Jan 2017, 4:51 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
auto increment is not possible for varchar generally it is done just adding the word AUTO_INCREMENT in query durring creating table. CREATE TABLE Students{ ID int AUTO_... } you can manually increment it. or convert or during making it by php do like incrementing it by one and inserting ietc it will not be automatic
16th Jan 2017, 8:58 AM
Sandeep Chatterjee
+ 2
yes that would be possible by server script in PHP you can do like $new_auto_increment=$last_id_fetched_from_database+1; $id="BR000".$new_auto_increment; $query='INSERT INTO Students ID VALUES "$id" '; you can do that in varchar by automatic incrementing using program for example last one is 0005 make program that gets it then new no is 5+1 that is 6 now generates BR00006 and then inserts it after automatic incrementing into the varchar column.
16th Jan 2017, 9:10 AM
Sandeep Chatterjee
+ 2
yes the stack overflow answer is nice, and it's by SQL only without using script to increment
27th Jan 2017, 5:27 PM
Sandeep Chatterjee
+ 1
I heard some trigger is used... any idea?
16th Jan 2017, 9:00 AM
Shyam Dhuriya
Shyam Dhuriya - avatar
+ 1
There's no way you can increment a varchar i.e. some string in nature, only AUTO_INCREMENT which apply to integers i.e. 1, 2, 3 and so on
16th Jan 2017, 9:02 AM
Tuchy
Tuchy - avatar
+ 1
cool... thanks Sandeep
16th Jan 2017, 9:11 AM
Shyam Dhuriya
Shyam Dhuriya - avatar
0
okay... for example my series should be like BR001, BR002... is this possible?
16th Jan 2017, 9:04 AM
Shyam Dhuriya
Shyam Dhuriya - avatar