简体   繁体   English

SQL Server-格式标识列

[英]SQL Server - formatted identity column

I would like to have a primary key column in a table that is formatted as FOO-BAR-[identity number], for example: 我想在一个表中具有一个主键列,该表的格式设置为FOO-BAR- [identity number],例如:

FOO-BAR-1  
FOO-BAR-2  
FOO-BAR-3  
FOO-BAR-4  
FOO-BAR-5  

Can SQL Server do this? SQL Server可以这样做吗? Or do I have to use C# to manage the sequence? 还是我必须使用C#来管理序列? If that's the case, how can I get the next [identity number] part using EntityFramwork? 如果是这样,我如何使用EntityFramwork获得下一个[身份编号]部分?

Thanks 谢谢

EDIT: 编辑:

I needed to do this is because this column represents a unique identifier of a notice send out to customers. 我需要执行此操作是因为此列代表发送给客户的通知的唯一标识符。

  • FOO will be a constant string FOO将是一个常量字符串
  • BAR will be different depending on the type of the notice (either Detection, Warning or Enforcement) BAR会根据通知的类型(检测,警告或执行)而有所不同

So is it better to have just an int identity column and append the values in Business Logic Layer in C#? 因此,最好只具有一个int标识列,并在C#中的Business Logic Layer中附加值?

If you want this 'composited' field in your reports, I propose you to: 如果您希望在报告中使用此“合成”字段,建议您:

  1. Use INT IDENTITY field as PK in table 在表中使用INT IDENTITY字段作为PK
  2. Create view for this table. 为此表创建视图。 In this view you can additionally generate the field that you want using your strings and types. 在此视图中,您还可以使用字符串和类型生成所需的字段。
  3. Use this view in your repoorts. 在您的报告中使用此视图。

But I still think, that there is BIG problem with DB design. 但是我仍然认为,数据库设计存在大问题。 I hope you'll try to redesign using normalization. 我希望您会尝试使用规范化进行重新设计。

You can set anything as the PK in a table. 您可以将任何内容设置为表中的PK。 But in this instance I would set IDENTITY to just an auto-incrementing int and manually be appending FOO-BAR- to it in the SQL, BLL, or UI depending on why it's being used. 但是在这种情况下,我会将IDENTITY设置为仅是一个自动递增的int,然后根据使用原因,在SQL,BLL或UI中手动将FOO-BAR-附加到该对象上。 If there is a business reason for FOO and BAR then you should also set these as values in your DB row. 如果出于商业原因需要FOOBAR则还应该在数据库行中将它们设置为值。 You can then create a key in the DB between the two three columns depending on why your actually using the values. 然后,您可以根据实际使用这些值的原因,在数据库的两三列之间创建一个键。

But IMO I really don't think there is ever a real reason to concatenate an ID in such a fashion and store it as such in the DB. 但是IMO我真的不认为有真正的理由以这种方式连接ID并将其存储在DB中。 But then again I really only use an int as my ID's. 但是话又说回来,我实际上只使用一个int作为我的ID。

It is quite possible by using computed column like this: 通过使用像这样的计算列是完全有可能的:

CREATE TABLE #test (
    id INT IDENTITY UNIQUE CLUSTERED,
    pk AS CONCAT('FOO-BAR-', id) PERSISTED PRIMARY KEY NONCLUSTERED,
    name NVARCHAR(20)
)

INSERT INTO #test (name) VALUES (N'one'), (N'two'), (N'three')

SELECT id, pk, name FROM #test

DROP TABLE #test

Note that pk is set to NONCLUSTERED on purpose because it is of VARCHAR type, while the IDENTITY field, which will be unique anyway, is set to UNIQUE CLUSTERED. 请注意,由于pk是VARCHAR类型,因此有意将pk设置为NONCLUSTERED,而无论如何将是唯一的IDENTITY字段设置为UNIQUE CLUSTERED。

Another option would be to use what an old team I used to be on called a codes and value table. 另一种选择是使用我以前的老团队称为代码和价值表。 We didn't use it for precisely this (we used it in lieu of auto-incrementing identities to prevent environment mismatches for some key tables), but what you could do is this: 我们没有将它用于此目的(我们使用它来代替自动递增的标识来防止某些关键表的环境不匹配),但是您可以执行以下操作:

  1. Create a table that has a row for each of your categories. 创建一个表格,其中每个类别都有一行。 Two (or more) columns in the row - minimum of category name and next number. 该行中的两列(或更多列)-类别名称和下一个数字的最小值。
  2. When you insert a record in the other table, you'll run a stored proc to get the next available identity number for that category, increment the number in the codes and values table by 1, and concatenate the category and number together for your insert. 在另一个表中插入记录时,将运行存储的proc以获取该类别的下一个可用标识号,将代码和值表中的数字递增1,并将类别和数字连接在一起以进行插入。

However , if you're main table is a high-volume table with lots of inserts, it's possible you could wind up with stuff out of sequence. 但是 ,如果您的主表是具有大量插入内容的高容量表,则可能会出现顺序混乱的情况。

In any event, even if it's not high volume, I think you'd be better off to reexamine why you want to do this, and see if there's another, better way to do it (such as having the business layer or UI do it, as others have suggested). 无论如何,即使数量不多,我认为最好还是重新考虑为什么要这样做,并查看是否还有另一种更好的方法(例如让业务层或UI进行) ,正如其他人所建议的那样。

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

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