MySQL - how to use VARCHAR as AUTO INCREMENT Primary Key

后端 未结 3 1728
日久生厌
日久生厌 2020-12-16 22:36

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):



        
3条回答
  •  眼角桃花
    2020-12-16 22:53

    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.

提交回复
热议问题