Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating indexes on columns where primary key or unique key constraints already present

I am currently learning indexes in Oracle. In Oracle documentation, following can be found :

Although the database creates an index for you on a column with an integrity constraint, explicitly creating an index on such a column is recommended.

Can anybody here tell me the rationale behind this ? If database creates an index automatically, why should we create another index on those columns explicitly ?

like image 587
CourseTriangle Avatar asked May 19 '17 13:05

CourseTriangle


People also ask

Why we need to create an index if the primary key is already present in a table?

32) Why we need to create an index if the primary key is already present in a table? Primary key can store null value, whereas a unique key cannot store null value.

Does creating a unique constraint create an index?

When you create a UNIQUE constraint, a unique nonclustered index is created to enforce a UNIQUE constraint by default. You can specify a unique clustered index if a clustered index on the table does not already exist.

What happens when you try to add unique constraint for a column which already has duplicate values?

If a UNIQUE constraint is added to a column that has duplicated values, the Database Engine returns an error and does not add the constraint. The Database Engine automatically creates a UNIQUE index to enforce the uniqueness requirement of the UNIQUE constraint.

Do indexed columns need to be unique?

The column you want to index does not need to have unique values.


1 Answers

At the very least, if you create an index explicitly, you can give it a meaningful name. If you then need to refer to the index by name, it will be in a human-readable form.

Note that you can create an explicit index WHILE you are creating the constraint, in the USING INDEX clause. The documentation has a few examples: https://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#ADMIN11725

like image 69
mathguy Avatar answered Sep 20 '22 17:09

mathguy