问题描述
我的 SQL Server 中有一个表.目前我使用身份列来唯一标识每条记录,但我不断变化的需求需要以特定格式生成的唯一密钥(由我的客户指定).我试图通过将一个唯一的整数(在每次插入时递增)附加到我的客户对我当前的解决方案不满意的指定格式来从我的应用程序生成唯一键.如果我能被引导到更好的技术来解决我的问题而不是我当前的解决方案,那就太好了.
I have a table in my SQL Server. Currently I am using the identity column to uniquely identify each record but my changing needs required a unique key generated in a certain format (as specified by my client). I have tried to generate the unique key from my application by appending a unique integer (that is incremented on every insert) to the format specified my client is not satisfied with my current solution. It would be great if I can be directed to a better technique to solve my problem rather then my current solution.
格式如下:
PRN-YEAR-MyAppGeneratedInt
推荐答案
基本上,保留当前的 identity
列.这是您识别和管理表中行的最佳方式.
Basically, keep the current identity
column. That is the best way for you to identify and manage rows in the table.
如果客户端需要另一个唯一键,则添加它.据推测,它将是一个字符串(假设它具有格式").您可以将键创建为生成的列.或者,您可能需要使用触发器来计算它.
If the client needs another unique key, then add it. Presumably, it will be a string (given that it has a "format"). You can possibly create the key as a generated column. Alternatively, you may need to use a trigger to calculate it.
一般来说,整数更适合标识列,即使最终用户从未看到它们.以下是一些优点:
In general, integers are better for identity columns, even if end users never see them. Here are some advantages:
- 它们对数据库中行插入的顺序进行编码.例如,您可以获取最后插入的行.
- 它们对于外键引用更有效(因为数字是固定长度的并且通常比字符串短).
- 当需要修复数据时,它们可以直接寻址一行.
这篇关于创建我的自定义唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!