在数据库中自动增加表id的字符串组合 [英] Autoincrement of table id using string combination in database automatically

查看:119
本文介绍了在数据库中自动增加表id的字符串组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道任何表的id是整数类型,并且该表中的自动增量是正确的。但我的问题是,我想增加一个字符串和整数的组合。例如
String:ABC是唯一ID的起始代码。

We know that id of any table is of integer type and auto-increment in that table is fine. But my problem is that i want to increment a combination of string and integer. For example String : ABC is the starting code for the unique id.

我的表的第一个ID应该是主键和

First Id of my table should be primary key and

即ABC10001,ABC10002,...等。

ie ABC10001, ABC10002,..... and so on.

不使用另一个键作为自动增量。请不要使用两种类型的唯一键。

Without using another key as auto-increment. Please do not use two type of unique key.

推荐答案

不能在一列中执行,除非指定 BEFORE INSERT 触发器会预填充它,完全由你来避免任何种类的条件auto_increment处理它自己。 auto_increment只能用于整数或浮点列。

You cannot do it in one column, unless you specify a BEFORE INSERT trigger which would prefill it, leaving it wholly up to you to avoid any kind of race conditions the auto_increment handles on it's own. auto_increment can only be used on integer or floating-point columns.

在我看来,'要求'PK有一个特定的格式, '是互斥的。

In my opinion, 'requiring' the PK the have a specific format, and 'just having it auto increment' are mutually exclusive.

这并不意味着你不能在选择上伪造它:

That doesn't mean you cannot 'fake' it on selects:

SELECT CONCAT('ABC1',LPAD(id,5,'0')) FROM tablename;

这篇关于在数据库中自动增加表id的字符串组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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