如何在不使用自动增量的情况下生成唯一ID

时间:2022-11-25 12:26:13

I am exporting data from csv into mysql database, I want the database to generate a unique id for each imported record. I want to use the unique field in generating a receipt number in crystal report.

我从csv导出数据到mysql数据库,我希望数据库为每个导入的记录生成唯一的id。我想在crystal report中使用唯一字段来生成收据号。

What would you suggest as the best way of doing this?

你认为最好的办法是什么?

2 个解决方案

#1


3  

If you don't want to auto increment the only other way to virtually guarantee uniqueness is to use a GUID as the id.

如果您不希望自动递增,那么确保惟一性的惟一方法就是使用GUID作为id。

I say "virtually" as there is a theoretical possibility that the same GUID could be generated twice, but it's effectively zero for all everyday uses. From the Wikipedia article:

我说的是“虚拟”,因为理论上有一种可能,即相同的GUID可以生成两次,但对于所有日常使用来说,它实际上是零。*的文章:

The total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is extremely small, and certain techniques have been developed to help ensure that numbers are not duplicated.

独特的键的总数(2128或3.4×1038)相同数量的概率很大,产生两次非常小,和某些技术已经开发来帮助确保数字不重复。

#2


2  

Autoincrement is absolutely the right way to do this in MySQL, is there are good reason not to use it?

在MySQL中自动递增绝对是正确的方法,有什么理由不使用它吗?

If you want something more like Oracle's sequences, you can use another table with an autoincrement column.

如果您想要更像Oracle的序列,可以使用另一个具有自动递增列的表。

#1


3  

If you don't want to auto increment the only other way to virtually guarantee uniqueness is to use a GUID as the id.

如果您不希望自动递增,那么确保惟一性的惟一方法就是使用GUID作为id。

I say "virtually" as there is a theoretical possibility that the same GUID could be generated twice, but it's effectively zero for all everyday uses. From the Wikipedia article:

我说的是“虚拟”,因为理论上有一种可能,即相同的GUID可以生成两次,但对于所有日常使用来说,它实际上是零。*的文章:

The total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is extremely small, and certain techniques have been developed to help ensure that numbers are not duplicated.

独特的键的总数(2128或3.4×1038)相同数量的概率很大,产生两次非常小,和某些技术已经开发来帮助确保数字不重复。

#2


2  

Autoincrement is absolutely the right way to do this in MySQL, is there are good reason not to use it?

在MySQL中自动递增绝对是正确的方法,有什么理由不使用它吗?

If you want something more like Oracle's sequences, you can use another table with an autoincrement column.

如果您想要更像Oracle的序列,可以使用另一个具有自动递增列的表。