• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Auto increment a varchar in MySQL

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to auto increment a varchar value. I'm getting as an exception. I want to do without triggers,procedures and function.
So any suggestions are welcome.
insert into idsdf(name,sid) values("JKL",concat("JKL",convert(id,char)));
The output is like this
id(auto_increment) name sid
1 JKL JKL0

i want sid value as JKL1.

Please help me.
Thanks in advance..
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's pretty clear from the MySQL documentation that you can only use numeric columns as auto-incremented values. In fact if you poke around a bit you'll find this statement:

MySQL wrote:AUTO_INCREMENT applies only to integer and floating-point types.



So your hopes of no extra coding aren't going to come true.

If it was me I would aim for a solution where you had a proper auto-increment column and then build your "JKL1" value later. However I have no idea what your requirements are because you asked a question about a proposed solution and not a question about your actual problem. If you'd like to explain your requirements you might well get suggestions which solve your problem better than the idea you proposed here, I think.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try this formula in the create table query:



So whenever you insert data, sid column will be in required format.

 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:However I have no idea what your requirements are because you asked a question about a proposed solution and not a question about your actual problem. If you'd like to explain your requirements you might well get suggestions which solve your problem better than the idea you proposed here, I think.



How many blind alleys have we all gone down be cause we did exactly this? :)
 
reply
    Bookmark Topic Watch Topic
  • New Topic