Postgres索引名称在什么级别需要唯一? [英] At what level do Postgres index names need to be unique?

查看:425
本文介绍了Postgres索引名称在什么级别需要唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft SQL Server 和MySQL中,索引名称需要在表中唯一,而在数据库中则必须唯一。

In Microsoft SQL Server and MySQL, index names need to unique within the table, but not within the database. This doesn't seem to be the case for PostgreSQL.

这就是我正在做的事情:我使用 CREATE TABLE复制了一个表new_table AS SELECT * FROM old_table 等,并需要重新创建索引。

Here's what I'm doing: I made a copy of a table using CREATE TABLE new_table AS SELECT * FROM old_table etc and need to re-create the indexes.

运行类似 CREATE INDEX的查询idx_column_name在new_table上使用GIST(column_name)导致 ERROR:关系 idx_column_name已经存在

这是怎么回事?

推荐答案

索引和表(以及视图,序列和...)存储在 pg_class 目录,并且由于其上的唯一键,它们在每个架构中都是唯一的:

Indexes and tables (and views, and sequences, and...) are stored in the pg_class catalog, and they're unique per schema due to a unique key on it:

# \d pg_class
      Table "pg_catalog.pg_class"
     Column     |   Type    | Modifiers 
----------------+-----------+-----------
 relname        | name      | not null
 relnamespace   | oid       | not null
 ...
Indexes:
    "pg_class_oid_index" UNIQUE, btree (oid)
    "pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)

每个@wildplasser的注释,您可以在创建索引时省略该名称,PG会自动分配一个唯一的名称。

Per @wildplasser's comment, you can omit the name when creating the index, and PG will assign a unique name automatically.

这篇关于Postgres索引名称在什么级别需要唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆