Unique constraint without index

社会主义新天地 提交于 2019-12-11 17:08:47

问题


let's say I have a large table.

This table not need to be queried, I just want to save the data inside for a while.

I want to prevent duplicates rows in the table, so I want to add an unique

constraint (or PK) on the table.

But the auto-created unique index is realy unnecessary.

I don't need it, and it's just wasting space in disk and require a maintenance (regardless of the long time to create it).

Is there a way to create an unique constraint without index (any index - unique or nonunique)?

Thank you.


回答1:


No, you can't have a UNIQUE constraint in Oracle without a corresponding index. The index is created automatically when the constraint is added, and any attempt to drop the index results in the error

ORA-02429: cannot drop index used for enforcement of unique/primary key

Best of luck.

EDIT

But you say "Let's say I have a large table". So how many rows are we talking about here? Look, 1TB SSD's are under $100. Quad-core laptops are under $400. If you're trying to minimize storage use or CPU burn by writing a bunch of code with minimal applicability to "save money" or "save time" my suggestion is that you're wasting both time and money. I repeat - ONE TERABYTE of storage costs the same as ONE HOUR of programmer time. A BRAND SPANKING NEW COMPUTER costs the same as FOUR LOUSY HOURS of programmer time. You are far, far better off doing whatever you can to minimize CODING TIME, rather than the traditional optimization targets of CPU time or disk space. Thus, I submit that the UNIQUE index is the low cost solution.




回答2:


But the auto-created unique index is really unnecessary.

In fact, UNIQUEness in an Oracle Database is enforced/guaranteed via an INDEX. That's why your primary key constraints come with a UNIQUE INDEX.

Per the Docs

UNIQUE Key Constraints and Indexes Oracle enforces unique integrity constraints with indexes.




回答3:


Maybe Index-Organized Tables is what you need ?.
But strictly the index organized table is the table stored in the structure of the index - one can say that there is the index alone without the table, while yor requirement is to have the table without the index, so this is the opposite :)

CREATE TABLE some_name
(
 col1           NUMBER(10)    NOT NULL,
 col2           NUMBER(10)    NOT NULL,
 col3           VARCHAR2(50)  NOT NULL,
 col4           VARCHAR2(50)  NOT NULL,
 CONSTRAINT pk_locations PRIMARY KEY (col1, col2)
)
ORGANIZATION INDEX 


来源:https://stackoverflow.com/questions/56562176/unique-constraint-without-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!