Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle database, converting unique index to non-unique one

Tags:

oracle

I understand I can't do this straightforward from studying similar questions on stackoverflow and other sites.

However, I need to do this and I'm willing to go with workarounds.

I tried to create a non-unique index with online and parallel, and then drop the old unique index. However, it fails saying ORA-01408: such column list already indexed.

How to convert an unique index to a non-unique one?

like image 726
Haozhun Avatar asked Aug 16 '12 00:08

Haozhun


People also ask

How do you change a unique index to non-unique?

So you can swap a non-unique and unique index over doing something like this: create table t ( c1 int ); create index i on t ( c1 ); create unique index ui on t ( c1 ) invisible; alter index i invisible; alter index ui visible; Note this means there's a brief time when there's no visible indexes on c1!

Can unique index be dropped?

A primary key or unique key index cannot be explicitly dropped.

Can we create a unique and non-unique index on same column in Oracle?

Creating a Unique Index ExplicitlyIndexes can be unique or non-unique. Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns). Non-unique indexes do not impose this restriction on the column values. Use the CREATE UNIQUE INDEX statement to create a unique index.

How do you remove a unique index?

In Object Explorer, right-click the table with the unique constraint, and click Design. On the Table Designer menu, click Indexes/Keys. In the Indexes/Keys dialog box, select the unique key in the Selected Primary/Unique Key and Index list. Click Delete.

Can we alter unique index in Oracle?

Alter Index in Oracle 11g In Oracle ALTER INDEX statement is used to change or rebuild an existing index. Prerequisites : The index must be in your own schema or you must have to ALTER ANY INDEX system privilege. To execute the MONITORING USAGE clause, the index must be in your own schema.


2 Answers

If you don't want to drop the old index before creating the new one, you can cheat a bit by creating the new index with an additional useless column, e.g.:

Assuming a table with the following configuration:

create table mytable (id number);

create unique index myunique on mytable (id);

To convert the index to non unique:

create index temp on mytable (id, 1);

drop index myunique;

create index mynonunique on mytable (id);

drop index temp;

In practice I'm not sure how necessary this is - generally I'd just drop and recreate the index in some low-activity period, preferably take the application down.

like image 191
Jeffrey Kemp Avatar answered Nov 15 '22 05:11

Jeffrey Kemp


Oracle now supports multiple indexes applied to the same set of columns as long as they differ in uniqueness (and/or in some other properties such as bitmap vs. btree and partitioning) and as long as only one of them is visible.

Therefore you can alter the existing index - changing it to invisible without dropping it and then create a new non-unique index.

CREATE TABLE mytable (id NUMBER);

CREATE UNIQUE INDEX mytable_unique_idx ON mytable(id);

ALTER INDEX mytable_unique_idx INVISIBLE;

CREATE INDEX mytable_nonunique_idx ON mytable(id);

Note that invisible indexes are still maintained by the database and you can change between them by turning one of them to invisible and the second one to visible.

like image 24
Matus Dubrava Avatar answered Nov 15 '22 04:11

Matus Dubrava