Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: UNIQUE constraint without index

Is it possible to add a constraint like

ALTER TABLE `t1` ADD UNIQUE(`col1`, `col2`);

without creating an index? The index wouldn't be used for any queries so it would be a waste of space.

It wouldn't be a problem if inserts and updates would be way slower, because the table doesn't get updated very often.

like image 439
COMMANDER CAPSLOCK Avatar asked Nov 11 '13 13:11

COMMANDER CAPSLOCK


People also ask

Do I need an index if I have a unique constraint?

Under the hood a unique constraint is implemented the same way as a unique index - an index is needed to efficiently fulfill the requirement to enforce the constraint.

Does unique constraint automatically create index?

PRIMARY KEY or UNIQUE constraint When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index.

Are unique keys automatically indexed?

To answer to question in bold: Yes, making a field unique does index it like s primary key.

Does unique constraint create index MySQL?

If we use a UNIQUE constraint in the table, MySQL automatically creates a UNIQUE index behind the scenes. The following statement explains how to create a unique constraint when we create a table.


1 Answers

No, this is not possible. A UNIQUE constraint contains an index definition and I barely imagine how it might be implemented without creating an index (in DBMS terms).

You should realize that indexes are not just 'wizardy' - they are a real data structure, which takes space to be placed, special procedures to be handled e.t.c. A unique constraint, itself, means unique index values, not unique column values.

like image 171
Alma Do Avatar answered Oct 18 '22 23:10

Alma Do