MySQL - how to use VARCHAR as AUTO INCREMENT Primary Key

I use VARCHAR as the primary key. I want it to automatically increase it (base 62, lower / upper case, numbers). However, the code below does not work (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 track id growth? (Since auto_increment does not work). Do I need to create another table containing the current iteration of the ID? Or is there a better way to do this?

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

+7
mysql primary-key auto-increment
source share
2 answers

you need to use the INT field
and translate it to any format you want at a specific time

+5
source share

example of solving your problem:

create a file with a unique number and then increase it using the function.

the file name can be a prefix, and the binary content of the file is a number.

when you need a new id for reg invoque function

Example

  String generateID(string A_PREFIX){ int id_value = parsetoInt(readFile(A_PREFIX).getLine()) int return_id_value = id_value++ return return_id_value } 

where "A_PREFIX-" is the name of the file that you use to generate the identifier for the field.

0
source share

All Articles