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

标签 postgresql hash

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

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/

相关文章:

ruby-on-rails - Rails 3 迁移 : boolean (mysql vs postgreSQL)

sql - 在单个语句中选择 "many to many"和 "belongs to"

postgresql - Node.js Postgres 获取最后执行的查询

ruby-on-rails - Ruby on Rails 教程 : Defining a hash with symbol keys

ruby - 如何从两个大小相等的数组构建 Ruby 哈希?

python - 将 Django 部署到 Heroku(Psycopg2 错误)

python - Sqlalchemy 枚举迁移更新失败说不存在

excel - VBA:在 Excel 中创建 session 持久对象(哈希)

c# - 我如何(正确地)通过 .NET 使用 RSA 和 SHA256 验证文件?

security - 如何在 postgresql 中散列密码?