简体   繁体   English

替代ORA_HASH?

[英]Alternative to ORA_HASH?

We are working with a table in a 3rd party database that does not have a primary key but does have a unique index. 我们正在与第三方数据库中的表不具有主键,但确实有一个唯一索引工作。

I have therefore been looking at using the ORA_HASH function to produce a de facto unique Id by passing in the values of the columns in the unique index. 因此,我一直在考虑使用ORA_HASH函数通过将唯一索引中的列值传入来产生实际的唯一ID。

Unfortunately, I can already see that we have a few collisions, which means that we can't derive a unique id using this method. 不幸的是,我已经看到我们有一些冲突,这意味着我们无法使用此方法派生唯一的ID。

Is there an alternative to ORA_HASH that would provide a unique id for a unique input? 是否有ORA_HASH的替代方法,可以为唯一输入提供唯一ID?

I suppose I could generate an Id using DBMS_CRYPTO.Hash but I'd ideally like to get a numeric value. 我想我可以使用DBMS_CRYPTO.Hash生成一个ID,但理想情况下我想获取一个数字值。

Edit 编辑

The added complication is that I then need to store these records in another (SQL Server) database and then compare the records from the original and the replica tables. 更为复杂的是,我随后需要将这些记录存储在另一个(SQL Server)数据库中,然后比较原始表和副本表中的记录。 So rank doesn't help me here since records can be added or deleted in the original table. 由于在原始表中可以添加或删除记录,因此rank对此无济于事。

DBMS_CRYPTO.HASH could be used to generate a high-bit hash (high enough to give you a very low, but not zero, chance of collisions), but it returns 'RAW' not 'NUMBER'. DBMS_CRYPTO.HASH可以用于生成高位哈希(足够高,使您有非常低但不为零的冲突机会),但是它返回“ RAW”而不是“ NUMBER”。

To guarantee no collisions ever, you need a one-to-one hash function. 为了确保永远不会发生冲突,您需要一对一的哈希函数。 As far as I know, Oracle does not provide one. 据我所知,Oracle没有提供。

A practical approach would be to create a new table to map unique keys to a newly generated primary key. 一种实用的方法是创建一个新表,以将唯一键映射到新生成的主键。 Eg, unique value ("ABC",123, 888) maps to 838491 (where you generated 838491 using a sequence). 例如,唯一值(“ ABC”,123,888)映射到838491(您在其中使用序列生成了838491)。

You'd have to update the mapping table periodically, to account for inserted rows, and that would be a pain, but it would let you generate your own PKs and keep track of them without a lot of complication. 您必须定期更新映射表,以解决插入的行,这很麻烦,但是它可以让您生成自己的PK并跟踪它们而不会造成很多麻烦。

Have you tried: 你有没有尝试过:

DBMS_UTILITY.GET_HASH_VALUE (
   name      VARCHAR2, 
   base      NUMBER, 
   hash_size NUMBER)
  RETURN NUMBER;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM