我在同一数据库 (PostgreSQL) 中的两个单独的表上创建索引,但我收到一条错误消息,指出索引已存在。这是事实,但是索引存在于不同的表上。改了名字之后就生效了。
我想知道为什么会这样?为什么数据库设计成两个表不能有相同的索引名称?
我找到了两个来源来回答这个问题,尽管答案不同。一个用于 MySQL,另一个用于 Postgres:
最佳答案
您可以有两个同名的索引。他们只是不能在同一个模式中。就像您可以拥有两个同名但不在同一架构中的表一样。
sandbox=# create schema test;
CREATE SCHEMA
sandbox=# create table public.a (a_id integer not null);
CREATE TABLE
sandbox=# create table test.a (a_id integer not null);
CREATE TABLE
sandbox=# create index a_idx on public.a (a_id);
CREATE INDEX
sandbox=# create index a_idx on test.a (a_id);
CREATE INDEX
这反射(reflect)了 PostgreSQL 设计者的决定。 SQL 标准不涉及创建索引。
关于database - 为什么两个表不能有同名索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997441/