创建我的自定义唯一键

编程入门 行业动态 更新时间:2024-10-27 22:27:05
本文介绍了创建我的自定义唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的 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:

它们对数据库中行插入的顺序进行编码.例如,您可以获取最后插入的行.它们对于外键引用更有效(因为数字是固定长度的并且通常比字符串短).当需要修复数据时,它们可以直接寻址一行.

这篇关于创建我的自定义唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 20:42:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   一键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!