简体   繁体   English

删除PostgreSQL中索引的唯一性

[英]Remove uniqueness of index in PostgreSQL

In my PostgreSQL database I have a unique index created this way:在我的 PostgreSQL 数据库中,我有一个以这种方式创建的唯一索引:

CREATE UNIQUE INDEX <my_index> ON <my_table> USING btree (my_column)

Is there way to alter the index to remove the unique constraint?有没有办法改变索引以删除唯一约束? I looked at ALTER INDEX documentation but it doesn't seem to do what I need.我查看了ALTER INDEX 文档,但它似乎没有做我需要的。

I know I can remove the index and create another one, but I'd like to find a better way, if it exists.我知道我可以删除索引并创建另一个索引,但我想找到更好的方法(如果存在)。

You may be able to remove the unique CONSTRAINT , and not the INDEX itself.您可以删除唯一的CONSTRAINT ,而不是INDEX本身。

Check your CONSTRAINTS via select * from information_schema.table_constraints;通过select * from information_schema.table_constraints;检查你的CONSTRAINTS select * from information_schema.table_constraints;

Then if you find one, you should be able to drop it like:然后,如果你找到一个,你应该可以像这样删除它:

ALTER TABLE <my_table> DROP CONSTRAINT <constraint_name>

Edit: a related issue is described in this question编辑: 此问题中描述了相关问题

Assume you have the following:假设您有以下内容:

Indexes:
    "feature_pkey" PRIMARY KEY, btree (id, f_id)
    "feature_unique" UNIQUE, btree (feature, f_class)
    "feature_constraint" UNIQUE CONSTRAINT, btree (feature, f_class)

To drop the UNIQUE CONSTRAINT, you would use ALTER TABLE :要删除 UNIQUE CONSTRAINT,您可以使用ALTER TABLE

ALTER TABLE feature DROP CONSTRAINT feature_constraint;

To drop the PRIMARY KEY, you would also use ALTER TABLE :要删除 PRIMARY KEY,您还可以使用ALTER TABLE

ALTER TABLE feature DROP CONSTRAINT feature_pkey;

To drop the UNIQUE [index], you would use DROP INDEX :要删除 UNIQUE [index],您可以使用DROP INDEX

DROP INDEX feature_unique;

I don't think it's possible... even in the pgAdmin III UI, if you try to edit a constraint created with your statement, the "Unique" box is greyed-out;我不认为这是可能的......即使在 pgAdmin III UI 中,如果您尝试编辑用您的语句创建的约束,“唯一”框是灰色的; you can't change it through the UI.您无法通过 UI 更改它。 Combined with your research on the ALTER INDEX docs, I'd say it can't be done.结合您对 ALTER INDEX 文档的研究,我认为这是不可能的。

Searched for hours for the same quesiton and doesnt seem to get a right answer---- all the given answers just failed to work.为同一个问题搜索了几个小时,似乎没有得到正确的答案——所有给出的答案都没有奏效。

For not null, it also took me some time to find.对于not null,我也花了一些时间才找到。 Apparently for some reason, the majority-certified codes just dont work when I use it.显然,出于某种原因,多数人认证的代码在我使用时不起作用。

I got the not null version code, something like this我得到了非空版本代码,就像这样

ALTER TABLE tablename
ALTER COLUMN column_want_to_remove_constriant
DROP NOT NULL

Sadly changing 'not null' to 'unique' doesnt work.可悲的是,将“not null”更改为“unique”是行不通的。

这对我有用,无需指定表,因为索引在您的情况下是唯一的:

DROP INDEX if exists my_index_name;

If you have PgAdmin4 installed than it's very easy just follow below steps...如果您安装了 PgAdmin4,则非常简单,只需按照以下步骤操作...

  1. Go to table properties(right click on table and select properties).转到表属性(右键单击表并选择属性)。
  2. A window will open than navigate to Constraints in properties window.将打开一个窗口,然后导航到属性窗口中的约束
  3. In Constraints option you will see options as below screenshot just go to Unique there you will see columns that has unique constraints.约束选项中,您将看到如下屏幕截图所示的选项,只需转到唯一,您将看到具有唯一约束的列。 Click on delete button and save.单击删除按钮并保存。 that's it you are good to go.那你就可以走了。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM