MySQL - 如何使用 VARCHAR 作为自动增量主键
我使用 VARCHAR 作为主键。我想自动递增它(基数 62,小写/大写,数字),但是,下面的代码失败了(出于明显的原因):
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 的表?或者有更好的方法来做到这一点吗?
编辑:我想澄清一下,我知道使用 INT 是 auto_increment 主键是合乎逻辑的方法。这个问题是对我之前看到的一些对话的回应。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须使用 INT 字段
并在选择的时间将其翻译为您想要的任何格式
you have to use an INT field
and translate it to whatever format you want at select time
问题解决方案的示例:
创建一个具有唯一编号的文件,然后使用函数递增。
文件名可以是前缀,文件二进制内容代表一个数字。
当您需要一个新的 id 到 reg 中时,请调用函数
示例
,其中“A_PREFIX-”是您用来生成字段 id 的文件名。
example of a solution to your problem:
create a file with a unique number and then increment with a function.
the filename can be the prefix and the file binary content represent a number.
when you need a new id to the reg invoque the function
Example
where "A_PREFIX-" is the file name wich you use to generate the id for the field.
或者只是创建一个序列并使用该序列维护 pk 字段,以通过 nextval 函数生成主键值。如果性能是一个问题,请按顺序使用缓存。
但正如其他人所说,这是次优的,如果您的主键包含编号序列,那么最好使用 int 和自动增量。
我没有看到 pk 必须自动递增但又是 varchar 数据类型的用例,这没有意义。
Or just create a sequence and maintain the pk field using the sequence to generate the primary key value with nextval function. And if perf is an issue, use cache on sequence.
But as others have stated, this is sub-optimal, if your primary key contains a numbered sequence then it's better to use int and auto-increment.
I don't see a use case where pk has to auto-increment but be a varchar data type, it doesn't make sense.
假设由于数据库外部的原因,您确实需要该 varchar 列,并且需要 autoIncrement,那么如何创建一个触发器来获取现有的 autoIncrement 值并使用 Convert() 将该值转换为 VarChar,删除 VarChar进入感兴趣的领域。正如前面的答案中提到的,如果有一些优势,您可以将表名与新的 varChar 值连接起来。
Assuming that for reasons external to the database, you do need that varchar column, and it needs to autoIncrement, then how about creating a trigger that grabs the existing autoIncrement value and uses Convert() to convert that value into a VarChar, dropping the VarChar into the field of interest. As mentioned in a previous answer, you could concatenate the table-name with the new varChar value, if there is some advantage to that.