MySQL-如何使用VARCHAR作为AUTO INCREMENT主键 [英] MySQL - how to use VARCHAR as AUTO INCREMENT Primary Key

查看:208
本文介绍了MySQL-如何使用VARCHAR作为AUTO INCREMENT主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VARCHAR作为主键.我想自动递增(以62为基数,大写/小写,数字),但是,以下代码失败(出于明显的原因):

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;

但是,这可行:

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;

跟踪自己的"id"增量的最佳方法是什么? (因为auto_increment不起作用).我是否需要制作另一个包含ID的当前迭代的表?还是有更好的方法来做到这一点?

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?

我想澄清一下,我知道使用INT是一个auto_increment主键是顺理成章的方法.这个问题是对我之前看到的一些对话的回应.谢谢

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

推荐答案

您必须使用INT字段
并在选择的时间将其翻译成您想要的任何格式

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

这篇关于MySQL-如何使用VARCHAR作为AUTO INCREMENT主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆