创建我的自定义唯一键

2022-11-23数据库问题
2

本文介绍了创建我的自定义唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

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

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12