简体   繁体   English

如何自动递增字母数字字符串主键?

[英]How do I auto-increment a alphanumeric string primary key?

Let's say I have table A with an alphanumeric string primary key.假设我有一个带有字母数字字符串主键的表 A。 The code used to create the table and how the table looks are shown below.用于创建表格的代码以及表格的外观如下所示。

CREATE TABLE A 
(
    ID CHAR(7) NOT NULL,
    ...
    CONSTRAINT PK_A PRIMARY KEY (ID)
)
| ID       | ...      |
| -------- | -------- |
| C000001  | ...      |
| C000002  | ...      |

I want to insert a new row into Table A and I don't want to type out C000003 or C000004 every time.我想在表 A 中插入一个新行,但我不想每次都输入C000003C000004 Is there a way to auto increment this?有没有办法自动增加这个?

I have thought about getting the latest id using我考虑过使用获取最新的 id

select top 1 CustId 
from Customer 
order by CustId desc

For splitting, I used SUBSTRING(ID, 2, 7) .对于拆分,我使用SUBSTRING(ID, 2, 7) For joining back, I can use concat('C', ID + 1) .为了重新加入,我可以使用concat('C', ID + 1)

The issue is, if I add one to the numeric portion, it would give me 3 instead of 000003. Is there a way to save the 0's?问题是,如果我在数字部分加一,它会给我 3 而不是 000003。有没有办法保存 0?

I just need help with incrementing.我只需要增加帮助。

My current code is like this:我当前的代码是这样的:

declare @lastid varchar(7), @newID varchar(7)
set @lastid = (select top 1 ID from A order by ID desc)
set @newID = SUBSTRING(@lastid, 2, 7)
select CONCAT('C', @newID + 1) -- need help with the increment

Any help appreciated任何帮助表示赞赏

EDIT 1: If the numbers are less than 10 (ie one digit), I can manually add in 0's to fill up the gaps.编辑 1:如果数字小于 10(即一位数),我可以手动添加 0 来填补空白。 But if the number has 2 digits or more, I can't do that so I'm thinking of a solution for this.但如果数字有 2 位数或更多,我不能这样做,所以我正在考虑解决这个问题的方法。

One word of advice: DON'T DO THIS!一个忠告:不要这样做! Using this SELECT MAX() + 1 approach is not safe under load, as soon as more than one user will be using your application, you WILL HAVE DUPLICATES - sooner or later.使用这种SELECT MAX() + 1方法在负载下并不安全,一旦有多个用户使用您的应用程序,您就会有重复的应用程序 - 迟早会发生。

The only viable solution is to use唯一可行的解决方案是使用

  • an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value一个ID INT IDENTITY(1,1)列得到 SQL 服务器来处理你的数值的自动递增
  • a computed, persisted column to convert that numeric value to the value you need一个计算的、持久的列,用于将该数值转换为您需要的值

So try this:所以试试这个:

CREATE TABLE dbo.Customer
(
    ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
    CustomerID AS 'C' + RIGHT('000000' + CAST(ID AS VARCHAR(6)), 6) PERSISTED,
    -- .... your other columns here....
)
  

Now, every time you insert a row into Customer without specifying values for ID or CustomerID :现在,每次您在Customer中插入一行而不指定IDCustomerID的值时:

INSERT INTO dbo.Customer(Col1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)

then SQL Server will automatically and safely increase your ID value, and CustomerID will contain values like C000001 , C000002 ,...... and so on - automatically, safely, reliably, no duplicates.然后 SQL 服务器将自动安全地增加您的ID值,并且CustomerID将包含诸如C000001C000002 ……等等的值 - 自动、安全、可靠、无重复。

I think it's not any suggested way that automatically incrementaly change the char value.我认为这不是自动增量更改 char 值的任何建议方法。 By logic we can do.按照逻辑我们可以做到。 Below is method which is perfect method for achive this effectivly.以下是有效实现此目标的完美方法。

Suppose you have one table called stminternal having following column Code, Prefix, Digit, LastNo假设您有一个名为 stminternal 的表,其中包含以下列 Code、Prefix、Digit、LastNo

Insert into stminternal values(1,'C',6,0)

So using this table you can generate new string.所以使用这个表你可以生成新的字符串。 First time WHEN RECORD INSERT C000001.第一次 WHEN RECORD INSERT C000001。

UPDATE stminternal SET LASTNO=2 WHERE CODE=1

Such a way it can be work like charms这样它就可以像魅力一样工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM