【问题标题】:Fast and unique hash function for unique key用于唯一键的快速且唯一的散列函数
【发布时间】:2021-04-04 23:01:29
【问题描述】:

我目前有两个表,其中两个表的复合值都是唯一性所必需的,例如 我有一张表t1 如下:

id | name
---------
1  | test

还有一张桌子t2

t1_id | key | value
-------------------
1     | k1  | test1
1     | k2  | test2

现在我希望不可能创建新的表行,这些行具有name => testk1 => test1k2 => test2 的组合,但例如 name => testk1 => test1k2 => thisisatest 应该是可能的。

我想通过在t1 中创建一个值为name + each (key + value) pair 的哈希字段来解决这个问题,为此我需要一个在提供这么多数据时具有唯一性的哈希函数。

【问题讨论】:

    标签: sql hash unique


    【解决方案1】:

    你为什么要这样做?只需在表上定义一个唯一约束:

    alter table t2 add constraint unq_t2_t1_id_key_value
        unique (t1_id, key, value);
    

    让数据库强制执行数据完整性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-03
      • 2014-10-30
      • 2013-09-12
      • 2016-11-10
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多