我应该如何创建唯一的帐单/帐号?

发布于 2024-11-27 04:25:46 字数 71 浏览 2 评论 0原文

你们用什么来生成唯一的帐号? 有些使用 Autoinc 字段,有些则使用其他... 在运行插入查询之前获取帐号的正确方法是什么?

What do you people use for generating unique account numbers?
Some use Autoinc field, others something else...
What would be the proper way i.e to get an account number before I run the insert query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信愁 2024-12-04 04:25:46

如果您使用的是 SQL 数据库,请使用生成器。如果您想使用独立的机制,您可以考虑使用 GUID。

If you are using a SQL database, use a Generator. If you want to use an independent mechanism you could consider using a GUID.

朮生 2024-12-04 04:25:46

您没有告诉我们您正在使用什么数据库系统,但从它的声音来看,您正在谈论 Delphi 中的 Paradox 表。如果是这样,autoInc 列可以工作,但如果我没记错的话,在使用 Paradox autoInc 列移动数据时必须非常小心,因为它们在移动时从零重新生成。

如前所述,您可以使用 GUID - sysutils.function CreateGUID(out Guid: TGUID): HResult; - 它们始终是唯一的,但 GUID 的缺点是按这些键进行排序并不直观,而且可能毫无意义,因此您需要某种时间戳列来维护插入的顺序,这可能很重要。此外,GUID 是一个相当长的字符串,用作 account# 时效率不高,假定它是许多表中的主键或外键。

因此,如果您想要自动的东西,我会坚持使用 autoInc,但如果您必须移动数据并且需要维护原始密钥,请将原始 autoinc 作为整数列加载到新位置,否则最终可能会损坏整个数据库。 (我相信还有其他情况也会导致 Paradox 表中的 autoIncs 重置 - 如果相关的话请研究一下 - 自从我使用 Pdox 以来已经很长时间了,并且对于其他平面文件数据库来说这可能不是问题)

如果您确实使用数据库服务器 - SQLServer、Oracle、Interbase 等,它们都具有 autoInc/indentity 或生成器功能,有时与触发器结合使用 - 这是您的最佳选择。 >

如果您想在 Delphi 代码中自己处理这个问题,Dorin 的答案也是一个很好的解决方案。创建一个全局的、线程安全的函数来实现它——这将确保非常高的安全性。

华泰

You haven't told us what database system you are using, but from the sound of it, you're talking about the Paradox tables in Delphi. If so, an autoInc column can work, although if I recall correctly, you have to be very careful when moving your data around with Paradox autoInc columns because they re-generate from zero when moved.

As has been mentioned, you can use GUIDs - sysutils.function CreateGUID(out Guid: TGUID): HResult; - they will always be unique, but the downside in GUIDS is that ordering by these keys will not be intuitive and probably be meaningless, so you'll need a timestamp column of some sort to maintain the order of your inserts, which can be important. Also, a GUID is a rather long character string and will not be very efficient for use as an account#, which assumedly will be a primary or foreign key in many tables.

So I'd stick to autoInc if you want something automatic, but if you have to move data around and you need to maintain your original keys, load your original autoincs as integer columns in their new location or you could end up corrupting your entire database. (I believe there are other scenarios that also cause autoIncs to reset in Paradox tables - research this if it's relevant - been a long time since I've used Pdox, and it may not be a problem with other flat file databases)

If you are indeed using a database server - SQLServer, Oracle, Interbase, etc, they all have autoInc/indentity or generator functionality, sometimes in conjuction with a trigger - that is your best option.

Dorin's answer is also an excellent solution if you want to handle this yourself from within your Delphi code. Create a global, thread safe function to implement it - that will ensure a very high level of safety.

HTH

感性不性感 2024-12-04 04:25:46

根据您想要号码的长度,您可以使用 Jamies MD5 转换或:

var
  LDateTime: TDateTime;
  LBytes: array[0..7] of Byte absolute LDateTime;
  LAccNo: string;
  Index: Integer;
begin
  LDateTime := Now;
  LAccNo := EmptyStr;
  for Index := 0 to 7 do
    LAccNo := LAccNo + IntToHex( LBytes[ Index ], 2 );
  // now you have a code in LAccNo, use it wisely (:
end;

Depending on how long you want the number, you can go with Jamies MD5 conversion or:

var
  LDateTime: TDateTime;
  LBytes: array[0..7] of Byte absolute LDateTime;
  LAccNo: string;
  Index: Integer;
begin
  LDateTime := Now;
  LAccNo := EmptyStr;
  for Index := 0 to 7 do
    LAccNo := LAccNo + IntToHex( LBytes[ Index ], 2 );
  // now you have a code in LAccNo, use it wisely (:
end;
猫弦 2024-12-04 04:25:46

我使用这个 PHP 代码片段生成一个像样的帐号:

$account_number = str_replace(array("0","O"),"D",strtoupper(substr(md5(time()),0,7)));

这将创建一个不包含 0 或 o 的 7 位 varchar 字符串(以避免电话上的错误或在电子邮件中抄写它们等),您会得到类似的内容EDB6DA6 或 76337D5 或 DB2E624。

I use this PHP snippet to generate a decent account number:

$account_number = str_replace(array("0","O"),"D",strtoupper(substr(md5(time()),0,7)));

This will create a 7 digit varchar string that doesn't contain 0's or o's (to avoid errors on the phone or transcribing them in e-mails, etc.) You get something like EDB6DA6 or 76337D5 or DB2E624.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文