gpt4 book ai didi

postgresql - 如何在postgresql中使用hash方法创建主键

转载 作者:行者123 更新时间:2023-11-29 11:26:14 25 4
gpt4 key购买 nike

有什么方法可以使用哈希方法创建主键吗?以下语句均无效:

oid char(30) primary key using hash

primary key(oid) using hash

最佳答案

我假设,您打算使用 hash index method / type .

主键是 constraints . 一些 约束可以创建索引以便正常工作(但不应依赖此事实)。 F.ex. UNIQUE 约束将创建一个唯一索引。请注意,目前只有 B-tree 支持唯一索引PRIMARY KEY 约束是 UNIQUENOT NULL 约束的组合,因此(目前)它仅支持 B 树。

如果需要,您也可以设置哈希索引(除了 PRIMARY KEY 约束之外)——但您不能使其唯一。

CREATE INDEX name ON table USING hash (column);

但是,如果您愿意这样做,您应该意识到哈希索引存在一些限制(直到 PostgreSQL 10):

Hash index operations are not presently WAL-logged, so hash indexes might need to be rebuilt with REINDEX after a database crash if there were unwritten changes. Also, changes to hash indexes are not replicated over streaming or file-based replication after the initial base backup, so they give wrong answers to queries that subsequently use them. For these reasons, hash index use is presently discouraged.

还有:

Currently, only the B-tree, GiST and GIN index methods support multicolumn indexes.

注意:不幸的是,oid 不是 PostgreSQL 中列的最佳名称,因为它也可以是 system column and type 的名称。 .

注释 2:char(n) type is also discouraged .您可以使用 varchartext 来代替,使用 CHECK 约束——或者(如果 id 非常像 uuid)uuid type本身。

关于postgresql - 如何在postgresql中使用hash方法创建主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24568157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com