SQL: 如何为主键生成唯一值
SQL: How to generate a unique value for a Primary Key
如标题所述。我有以下 table:
CREATE TABLE exampleTable(
ID int NOT NULL,
Text varchar(255),
PRIMARY KEY (ID)
);
通过插入值:
INSERT INTO exampleTable (ID, Text)
VALUES (123, 'Hello World');
有时可以为已经在 Table 中的主键添加一个值,从而给您一个 Error
.
问题:
通过什么方式可以保证选择的Primary Key的值始终唯一?
如何生成始终唯一的主键值。
对于2.问题,可能的做法:
//Check if the new Value is already in the Table by
//Iterating and comparing through all the Primary Key Values.
第一个答案很简单
主键约束保证值是唯一的,如果不是
,你会得到一个error
更复杂,您可以尝试获取 max(id) 然后在源代码或 BEFORE INSERT TRIGGER 中增加它,但在重型服务器中您仍然会遇到错误,这就是我们使用 auto_increment 或 uuids
的原因
如果您发送主键约束,它只允许唯一值。
您可以启用 Auto-increment/IsIDentity 列 true。
另一种方法是将 id 的类型更改为 uniqueidentifier 并手动插入 Guid。
那总是独一无二的。
但不推荐
为什么要在不使用 auto-increment 的情况下获取主键的唯一值?一个答案:当有多个人试图生成跨所有实例唯一的密钥的分布式应用程序时 AND 网络延迟禁止访问中央服务器以获取密钥 AND允许有一个小的“碰撞”率。
对此答案的评论:
- 这很麻烦,可能会占用太多计算机时间。
- 它不能 100% 保证唯一键——只是非常非常接近。
- 可以通过生成更长的密钥(将 Result_Length 更改为更大的数字。
来获得生成的密钥唯一的任意置信度。
- 我已经在 8 个字母的密钥上测试了该算法,发现每生成 1,000,000 个密钥,冲突少于 1 次。
VBA代码中表达的解决方案:
Public Function fnGenerateAlmostUniqueKey as String
' Function to return almost-guaranteed-unique key value
' Increasing Result_Length decreases the probability of a collision
' Function returns a string like "AF4r9U7Y"
Const Result_Length as long = 8 ' Adjust as needed
Const Alpha as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Dim idx as long
Dim Result as string
Dim s as string ' Intermediate value
Randomize(Now()) ' Prevents everyone from using the same RND sequence
For idx = 1 To Result_Length
s = Mid(Alpha, Int(1 + Rnd() * 60), 1)
Result = Result & s
Next idx
fnGenerateAlmostUniqueKey = Result
End Function
如标题所述。我有以下 table:
CREATE TABLE exampleTable(
ID int NOT NULL,
Text varchar(255),
PRIMARY KEY (ID)
);
通过插入值:
INSERT INTO exampleTable (ID, Text)
VALUES (123, 'Hello World');
有时可以为已经在 Table 中的主键添加一个值,从而给您一个 Error
.
问题:
通过什么方式可以保证选择的Primary Key的值始终唯一?
如何生成始终唯一的主键值。
对于2.问题,可能的做法:
//Check if the new Value is already in the Table by
//Iterating and comparing through all the Primary Key Values.
第一个答案很简单
主键约束保证值是唯一的,如果不是
,你会得到一个error
更复杂,您可以尝试获取 max(id) 然后在源代码或 BEFORE INSERT TRIGGER 中增加它,但在重型服务器中您仍然会遇到错误,这就是我们使用 auto_increment 或 uuids
的原因
如果您发送主键约束,它只允许唯一值。 您可以启用 Auto-increment/IsIDentity 列 true。
另一种方法是将 id 的类型更改为 uniqueidentifier 并手动插入 Guid。 那总是独一无二的。 但不推荐
为什么要在不使用 auto-increment 的情况下获取主键的唯一值?一个答案:当有多个人试图生成跨所有实例唯一的密钥的分布式应用程序时 AND 网络延迟禁止访问中央服务器以获取密钥 AND允许有一个小的“碰撞”率。
对此答案的评论:
- 这很麻烦,可能会占用太多计算机时间。
- 它不能 100% 保证唯一键——只是非常非常接近。
- 可以通过生成更长的密钥(将 Result_Length 更改为更大的数字。 来获得生成的密钥唯一的任意置信度。
- 我已经在 8 个字母的密钥上测试了该算法,发现每生成 1,000,000 个密钥,冲突少于 1 次。
VBA代码中表达的解决方案:
Public Function fnGenerateAlmostUniqueKey as String
' Function to return almost-guaranteed-unique key value
' Increasing Result_Length decreases the probability of a collision
' Function returns a string like "AF4r9U7Y"
Const Result_Length as long = 8 ' Adjust as needed
Const Alpha as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Dim idx as long
Dim Result as string
Dim s as string ' Intermediate value
Randomize(Now()) ' Prevents everyone from using the same RND sequence
For idx = 1 To Result_Length
s = Mid(Alpha, Int(1 + Rnd() * 60), 1)
Result = Result & s
Next idx
fnGenerateAlmostUniqueKey = Result
End Function