views:

122

answers:

1

I am using a VARCHAR as my primary key. I want to auto increment it (base 62, lower/upper case, numbers), However, the below code fails (for obvious reasons):

CREATE TABLE IF NOT EXISTS `campaign` (
  `account_id` BIGINT(20) NOT NULL,
  `type` SMALLINT(5)  NOT NULL,
  `id` VARCHAR(16) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

however, this works:

CREATE TABLE IF NOT EXISTS `campaign` (
  `account_id` BIGINT(20) NOT NULL,
  `type` SMALLINT(5)  NOT NULL,
  `id` VARCHAR(16) NOT NULL PRIMARY KEY
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

What is the best way to keep track of incrementation of 'id' myself? (Since auto_increment doesn't work). Do i need to make another table that contains the current iteration of ID? Or is there a better way to do this?

EDIT: I want to clarify that I know that using INT is a auto_increment primary key is the logical way to go. This question is in response to some previous dialogue I saw. Thanks

A: 

you have to use an INT field
and translate it to whatever format you want at select time

Col. Shrapnel
That is not possible in my case as the ID is passed around as part of the URL.
KennyCason
Also, I am specifically looking for the best way to manually increment the ID. I know Bit.ly and possibly Tiny URL do something very similar.
KennyCason
@Kenny lol everything is possible for the programmer :) convert it before passing to the query. not a big deal. These url shorteners do it as I said.
Col. Shrapnel
Thanks, you don't HAVE to use INT field, some custom URL shorteners (for example) use a BIGINT as their auto incrementing primary key. But there are services that use VARCHAR as their primary Key and they Manually increment them. So my question is, is there an efficient way to "increment" a VARCHAR primary key. And I very much understand "everything is possible for the programmer" :) However, that also leads to many problems by implementing known, established practices!
KennyCason
@Kenny got an example of such a "problem"? What if your lame "varchar autoincrement" will lead to some problems too? Concurrency ones, for example
Col. Shrapnel
Well, I'm sorry that you think it's "lame" I'm just asking a question because of something I read in a discussion about using VARCHAR as a primary field, and was shocked to hear of a auto incrementing implementation of VARCHAR. As to "problems" with "everything is possible for the programmer", just because it is possible, doesn't mean it is right :P I don't think i need to provide an example.
KennyCason
@Kenny that was just answer to your "impossible". No examples needed. just quit that nonsense.
Col. Shrapnel
Agreed. natively `id` VARCHAR(16) NOT NULL AUTO_INCREMENT PRIMARY KEY is impossible. However, using VARCHAR as a primary key is not nonsense. I was merely asking how to "increment" it to guarantee uniqueness. And btw, I do use BIGINT for my ID, as it provides more than enough ID's for my implementation. "just quit that nonsense" <-- I really do appreciate you taking your time to answer, and I respect your high reputation on SO, but please have a little more tact, as my question was not impossible
KennyCason